Skip to content

Commit e1d0641

Browse files
Add compatibility with spacy 3.0 (#10)
* Add component factory to support spacy 3.0 * Update readme to reflect spacy 3.0 usage * show spacy 2.x initialization Co-authored-by: Francisco Aranda <frascuchon@gmail.com>
1 parent 6425cb2 commit e1d0641

File tree

5 files changed

+14
-7
lines changed

5 files changed

+14
-7
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ from spacy_wordnet.wordnet_annotator import WordnetAnnotator
4444

4545
# Load an spacy model (supported models are "es", "en" and "pt")
4646
nlp = spacy.load('en')
47-
nlp.add_pipe(WordnetAnnotator(nlp.lang), after='tagger')
47+
# Spacy 3.x
48+
nlp.add_pipe("spacy_wordnet", after='tagger', config={'lang': nlp.lang})
49+
# Spacy 2.x
50+
# self.nlp_en.add_pipe(WordnetAnnotator(self.nlp_en.lang))
4851
token = nlp('prices')[0]
4952

5053
# wordnet object link spacy token with nltk wordnet interface by giving acces to

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
# scipy==1.0
1717

1818
nltk>=3.4.5,<3.6
19-
spacy>=2.0,<3.0
19+
spacy>=2.0,<4.0

spacy_wordnet/wordnet_annotator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
from spacy.tokens.doc import Doc
22
from spacy.tokens.token import Token
3+
from spacy.language import Language
34

45
from spacy_wordnet.wordnet_domains import Wordnet, load_wordnet_domains
56

7+
@Language.factory("spacy_wordnet", default_config={"lang": "en"})
8+
def wordnet_annotator(nlp, name, lang: str):
9+
return WordnetAnnotator(lang=lang)
610

711
class WordnetAnnotator(object):
812
__FIELD = 'wordnet'

tests/example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# Load an spacy model (supported models are "es" and "en")
1010
nlp = spacy.load('en')
11-
nlp.add_pipe(WordnetAnnotator(nlp.lang), after='tagger')
11+
nlp.add_pipe("spacy_wordnet", after='tagger')
1212
token = nlp('prices')[0]
1313

1414
# wordnet object link spacy token with nltk wordnet interface by giving acces to

tests/test_wordnet_annotator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def __init__(self, *args, **kwargs):
1616

1717
super().__init__(*args, **kwargs)
1818

19-
self.nlp_en = spacy.load('en')
20-
self.nlp_es = spacy.load('es')
19+
self.nlp_en = spacy.load('en_core_web_sm')
20+
self.nlp_es = spacy.load('es_core_news_sm')
2121

2222
# Add wordnet component
23-
self.nlp_en.add_pipe(WordnetAnnotator(self.nlp_en.lang))
24-
self.nlp_es.add_pipe(WordnetAnnotator(self.nlp_es.lang))
23+
self.nlp_en.add_pipe("spacy_wordnet", config={'lang': self.nlp_en.lang})
24+
self.nlp_es.add_pipe("spacy_wordnet", config={'lang': self.nlp_es.lang})
2525

2626
def test_english_annotations(self):
2727

0 commit comments

Comments
 (0)