From e1d0641f31df03deb9889e108af8e2584cc0f25b Mon Sep 17 00:00:00 2001 From: Samuel Frazee Date: Tue, 13 Apr 2021 08:43:56 -0700 Subject: [PATCH] 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 --- README.md | 5 ++++- requirements.txt | 2 +- spacy_wordnet/wordnet_annotator.py | 4 ++++ tests/example.py | 2 +- tests/test_wordnet_annotator.py | 8 ++++---- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 860dec9..ea20fd1 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,10 @@ from spacy_wordnet.wordnet_annotator import WordnetAnnotator # Load an spacy model (supported models are "es", "en" and "pt") nlp = spacy.load('en') -nlp.add_pipe(WordnetAnnotator(nlp.lang), after='tagger') +# Spacy 3.x +nlp.add_pipe("spacy_wordnet", after='tagger', config={'lang': nlp.lang}) +# Spacy 2.x +# self.nlp_en.add_pipe(WordnetAnnotator(self.nlp_en.lang)) token = nlp('prices')[0] # wordnet object link spacy token with nltk wordnet interface by giving acces to diff --git a/requirements.txt b/requirements.txt index 7f1ccf0..7bb7107 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,4 +16,4 @@ # scipy==1.0 nltk>=3.4.5,<3.6 -spacy>=2.0,<3.0 \ No newline at end of file +spacy>=2.0,<4.0 \ No newline at end of file diff --git a/spacy_wordnet/wordnet_annotator.py b/spacy_wordnet/wordnet_annotator.py index 934d361..376ee66 100644 --- a/spacy_wordnet/wordnet_annotator.py +++ b/spacy_wordnet/wordnet_annotator.py @@ -1,8 +1,12 @@ from spacy.tokens.doc import Doc from spacy.tokens.token import Token +from spacy.language import Language from spacy_wordnet.wordnet_domains import Wordnet, load_wordnet_domains +@Language.factory("spacy_wordnet", default_config={"lang": "en"}) +def wordnet_annotator(nlp, name, lang: str): + return WordnetAnnotator(lang=lang) class WordnetAnnotator(object): __FIELD = 'wordnet' diff --git a/tests/example.py b/tests/example.py index 6ca24aa..f7ba635 100644 --- a/tests/example.py +++ b/tests/example.py @@ -8,7 +8,7 @@ # Load an spacy model (supported models are "es" and "en") nlp = spacy.load('en') - nlp.add_pipe(WordnetAnnotator(nlp.lang), after='tagger') + nlp.add_pipe("spacy_wordnet", after='tagger') token = nlp('prices')[0] # wordnet object link spacy token with nltk wordnet interface by giving acces to diff --git a/tests/test_wordnet_annotator.py b/tests/test_wordnet_annotator.py index cab1ed9..a9fa60a 100644 --- a/tests/test_wordnet_annotator.py +++ b/tests/test_wordnet_annotator.py @@ -16,12 +16,12 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.nlp_en = spacy.load('en') - self.nlp_es = spacy.load('es') + self.nlp_en = spacy.load('en_core_web_sm') + self.nlp_es = spacy.load('es_core_news_sm') # Add wordnet component - self.nlp_en.add_pipe(WordnetAnnotator(self.nlp_en.lang)) - self.nlp_es.add_pipe(WordnetAnnotator(self.nlp_es.lang)) + self.nlp_en.add_pipe("spacy_wordnet", config={'lang': self.nlp_en.lang}) + self.nlp_es.add_pipe("spacy_wordnet", config={'lang': self.nlp_es.lang}) def test_english_annotations(self):