forked from explosion/spaCy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
95 changed files
with
5,101 additions
and
765 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,30 @@ | ||
language: python | ||
|
||
sudo: required | ||
sudo: false | ||
dist: trusty | ||
group: edge | ||
|
||
python: | ||
- "2.7" | ||
- "3.4" | ||
- "3.5" | ||
|
||
os: | ||
- linux | ||
|
||
env: | ||
- VIA="compile" | ||
- VIA="sdist" | ||
|
||
install: | ||
- "pip install -r requirements.txt" | ||
- "pip install -e ." | ||
- "mkdir -p corpora/en" | ||
- "cd corpora/en" | ||
- "wget --no-check-certificate http://wordnetcode.princeton.edu/3.0/WordNet-3.0.tar.gz" | ||
- "tar -xzf WordNet-3.0.tar.gz" | ||
- "mv WordNet-3.0 wordnet" | ||
- "cd ../../" | ||
- "python bin/init_model.py en lang_data/ corpora/ data" | ||
- "cp package.json data" | ||
- "sputnik build data en_default.sputnik" | ||
- "sputnik --name spacy install en_default.sputnik" | ||
- "./travis.sh" | ||
|
||
script: | ||
- "pip install pytest" | ||
- "python -m pytest spacy" | ||
|
||
- if [[ "${VIA}" == "compile" ]]; then SPACY_DATA=models/en python -m pytest spacy; fi | ||
- if [[ "${VIA}" == "pypi" ]]; then python -m pytest `python -c "import pathlib; import spacy; print(pathlib.Path(spacy.__file__).parent.resolve())"`; fi | ||
- if [[ "${VIA}" == "sdist" ]]; then python -m pytest `python -c "import pathlib; import spacy; print(pathlib.Path(spacy.__file__).parent.resolve())"`; fi | ||
|
||
notifications: | ||
slack: | ||
secure: F8GvqnweSdzImuLL64TpfG0i5rYl89liyr9tmFVsHl4c0DNiDuGhZivUz0M1broS8svE3OPOllLfQbACG/4KxD890qfF9MoHzvRDlp7U+RtwMV/YAkYn8MGWjPIbRbX0HpGdY7O2Rc9Qy4Kk0T8ZgiqXYIqAz2Eva9/9BlSmsJQ= | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import unicode_literals, print_function | ||
import json | ||
import pathlib | ||
import random | ||
|
||
import spacy | ||
from spacy.pipeline import EntityRecognizer | ||
from spacy.gold import GoldParse | ||
|
||
|
||
def train_ner(nlp, train_data, entity_types): | ||
ner = EntityRecognizer(nlp.vocab, entity_types=entity_types) | ||
for itn in range(5): | ||
random.shuffle(train_data) | ||
for raw_text, entity_offsets in train_data: | ||
doc = nlp.make_doc(raw_text) | ||
gold = GoldParse(doc, entities=entity_offsets) | ||
ner.update(doc, gold) | ||
ner.model.end_training() | ||
return ner | ||
|
||
|
||
def main(model_dir=None): | ||
if model_dir is not None: | ||
model_dir = pathlb.Path(model_dir) | ||
if not model_dir.exists(): | ||
model_dir.mkdir() | ||
assert model_dir.isdir() | ||
|
||
nlp = spacy.load('en', parser=False, entity=False, vectors=False) | ||
|
||
train_data = [ | ||
( | ||
'Who is Shaka Khan?', | ||
[(len('Who is '), len('Who is Shaka Khan'), 'PERSON')] | ||
), | ||
( | ||
'I like London and Berlin.', | ||
[(len('I like '), len('I like London'), 'LOC'), | ||
(len('I like London and '), len('I like London and Berlin'), 'LOC')] | ||
) | ||
] | ||
ner = train_ner(nlp, train_data, ['PERSON', 'LOC']) | ||
|
||
doc = nlp.make_doc('Who is Shaka Khan?') | ||
nlp.tagger(doc) | ||
ner(doc) | ||
for word in doc: | ||
print(word.text, word.tag_, word.ent_type_, word.ent_iob) | ||
|
||
if model_dir is not None: | ||
with (model_dir / 'config.json').open('wb') as file_: | ||
json.dump(ner.cfg, file_) | ||
ner.model.dump(str(model_dir / 'model')) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
# Who "" 2 | ||
# is "" 2 | ||
# Shaka "" PERSON 3 | ||
# Khan "" PERSON 1 | ||
# ? "" 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from __future__ import unicode_literals, print_function | ||
import json | ||
import pathlib | ||
import random | ||
|
||
import spacy | ||
from spacy.pipeline import DependencyParser | ||
from spacy.gold import GoldParse | ||
from spacy.tokens import Doc | ||
|
||
|
||
def train_parser(nlp, train_data, left_labels, right_labels): | ||
parser = DependencyParser( | ||
nlp.vocab, | ||
left_labels=left_labels, | ||
right_labels=right_labels) | ||
for itn in range(1000): | ||
random.shuffle(train_data) | ||
loss = 0 | ||
for words, heads, deps in train_data: | ||
doc = Doc(nlp.vocab, words=words) | ||
gold = GoldParse(doc, heads=heads, deps=deps) | ||
loss += parser.update(doc, gold) | ||
parser.model.end_training() | ||
return parser | ||
|
||
|
||
def main(model_dir=None): | ||
if model_dir is not None: | ||
model_dir = pathlb.Path(model_dir) | ||
if not model_dir.exists(): | ||
model_dir.mkdir() | ||
assert model_dir.isdir() | ||
|
||
nlp = spacy.load('en', tagger=False, parser=False, entity=False, vectors=False) | ||
|
||
train_data = [ | ||
( | ||
['They', 'trade', 'mortgage', '-', 'backed', 'securities', '.'], | ||
[1, 1, 4, 4, 5, 1, 1], | ||
['nsubj', 'ROOT', 'compound', 'punct', 'nmod', 'dobj', 'punct'] | ||
), | ||
( | ||
['I', 'like', 'London', 'and', 'Berlin', '.'], | ||
[1, 1, 1, 2, 2, 1], | ||
['nsubj', 'ROOT', 'dobj', 'cc', 'conj', 'punct'] | ||
) | ||
] | ||
left_labels = set() | ||
right_labels = set() | ||
for _, heads, deps in train_data: | ||
for i, (head, dep) in enumerate(zip(heads, deps)): | ||
if i < head: | ||
left_labels.add(dep) | ||
elif i > head: | ||
right_labels.add(dep) | ||
parser = train_parser(nlp, train_data, sorted(left_labels), sorted(right_labels)) | ||
|
||
doc = Doc(nlp.vocab, words=['I', 'like', 'securities', '.']) | ||
parser(doc) | ||
for word in doc: | ||
print(word.text, word.dep_, word.head.text) | ||
|
||
if model_dir is not None: | ||
with (model_dir / 'config.json').open('wb') as file_: | ||
json.dump(parser.cfg, file_) | ||
parser.model.dump(str(model_dir / 'model')) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
# I nsubj like | ||
# like ROOT like | ||
# securities dobj like | ||
# . cc securities |
Oops, something went wrong.