From ada107749d0e195dd966a2d4afe94136cdb230ab Mon Sep 17 00:00:00 2001 From: Francisco Aranda Date: Wed, 9 Jun 2021 12:31:40 +0200 Subject: [PATCH] enable github ci actions pipeline (#11) * enable github ci actions pipeline * install wordnet data --- .github/workflows/ci.yml | 91 ++++++++++++++++++++++++++++++ spacy_wordnet/wordnet_annotator.py | 17 ++++-- tests/test_wordnet_annotator.py | 29 ++++++---- 3 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..afcd4d9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,91 @@ + +name: CI + +on: + push: + branches: + - 'master' + tags: + - 'v*' + pull_request: + branches: + - 'master' + release: + types: [published] + +jobs: + build: + name: Build python package + runs-on: ubuntu-latest + defaults: + run: + shell: bash -l {0} + steps: + - name: Checkout Code 🛎 + uses: actions/checkout@v2 + - name: Cache pip 👜 + uses: actions/cache@v2 + env: + # Increase this value to reset cache if setup.cfg has not changed + CACHE_NUMBER: 0 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ env.CACHE_NUMBER }}-${{ hashFiles('setup.cfg') }} + - name: Install package 🧊 + run: pip install .[testing] + - name: Run tests 📈 + run: | + python -m nltk.downloader wordnet + python -m nltk.downloader omw + pip install spacy~=2.0 + python -m spacy download en_core_web_sm + python -m spacy download es_core_news_sm + pytest tests + pip install spacy~=3.0.0 + python -m spacy download en_core_web_sm + python -m spacy download es_core_news_sm + pytest tests + - name: Build Package 🍟 + run: | + pip install -U build + python -m build + - name: Upload package artifact + uses: actions/upload-artifact@v2 + with: + name: python-package + path: dist + + # This job will upload a Python Package using Twine when a release is created + # For more information see: + # https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries + deploy_release: + name: Deploy Release + runs-on: ubuntu-latest + if: ${{ github.event_name == 'release' }} + needs: + - build + defaults: + run: + shell: bash -l {0} + steps: + - name: Checkout Code 🛎 + uses: actions/checkout@v2 + + - name: Download python package + uses: actions/download-artifact@v2 + with: + name: python-package + path: dist + - name: Publish Package to TestPyPI 🥪 + uses: pypa/gh-action-pypi-publish@master + with: + user: __token__ + password: ${{ secrets.TEST_PYPI_API_TOKEN }} + repository_url: https://test.pypi.org/legacy/ + - name: Test Installing 🍿 + run: pip install --index-url https://test.pypi.org/simple --no-deps spacy-wordnet==${GITHUB_REF#refs/*/v} + - name: Publish Package to PyPI 🥩 + uses: pypa/gh-action-pypi-publish@master + with: + user: __token__ + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/spacy_wordnet/wordnet_annotator.py b/spacy_wordnet/wordnet_annotator.py index 376ee66..91c4c1a 100644 --- a/spacy_wordnet/wordnet_annotator.py +++ b/spacy_wordnet/wordnet_annotator.py @@ -4,14 +4,21 @@ 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) +try: + + @Language.factory("spacy_wordnet", default_config={"lang": "en"}) + def wordnet_annotator(nlp, name, lang: str): + return WordnetAnnotator(lang=lang) + + +except AttributeError: + pass # spacy 2.x + class WordnetAnnotator(object): - __FIELD = 'wordnet' + __FIELD = "wordnet" - def __init__(self, lang: str = 'es'): + def __init__(self, lang: str = "es"): Token.set_extension(WordnetAnnotator.__FIELD, default=None, force=True) load_wordnet_domains() self.__lang = lang diff --git a/tests/test_wordnet_annotator.py b/tests/test_wordnet_annotator.py index a9fa60a..18a3ac0 100644 --- a/tests/test_wordnet_annotator.py +++ b/tests/test_wordnet_annotator.py @@ -11,21 +11,24 @@ class WordnetAnnotatorTest(unittest.TestCase): - def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.nlp_en = spacy.load('en_core_web_sm') - self.nlp_es = spacy.load('es_core_news_sm') + 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("spacy_wordnet", config={'lang': self.nlp_en.lang}) - self.nlp_es.add_pipe("spacy_wordnet", config={'lang': self.nlp_es.lang}) + try: + # Add wordnet component + 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}) + except TypeError: # spacy 2.x + self.nlp_en.add_pipe(WordnetAnnotator(self.nlp_en.lang)) + self.nlp_es.add_pipe(WordnetAnnotator(self.nlp_es.lang)) def test_english_annotations(self): - token = self.nlp_en('contracts')[0] + token = self.nlp_en("contracts")[0] assert token._.wordnet.synsets() assert token._.wordnet.lemmas() @@ -33,10 +36,10 @@ def test_english_annotations(self): def test_generate_variants_from_domain_list(self): - economy_domains = ['finance', 'banking'] + economy_domains = ["finance", "banking"] enriched_sentence = [] - sentence = self.nlp_en('I want to withdraw 5,000 euros') + sentence = self.nlp_en("I want to withdraw 5,000 euros") for token in sentence: synsets = token._.wordnet.wordnet_synsets_for_domain(economy_domains) @@ -45,13 +48,15 @@ def test_generate_variants_from_domain_list(self): lemmas_for_synset = [] for s in synsets: lemmas_for_synset.extend(s.lemma_names()) - enriched_sentence.append('({})'.format('|'.join(set(lemmas_for_synset)))) + enriched_sentence.append( + "({})".format("|".join(set(lemmas_for_synset))) + ) else: enriched_sentence.append(token.text) - print(' '.join(enriched_sentence)) + print(" ".join(enriched_sentence)) def test_spanish_annotations(self): - token = self.nlp_es('contratos')[0] + token = self.nlp_es("contratos")[0] assert token._.wordnet.synsets() assert token._.wordnet.lemmas()