Skip to content

Commit

Permalink
enable github ci actions pipeline (#11)
Browse files Browse the repository at this point in the history
* enable github ci actions pipeline

* install wordnet data
  • Loading branch information
frascuchon authored Jun 9, 2021
1 parent e1d0641 commit ada1077
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 17 deletions.
91 changes: 91 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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 }}
17 changes: 12 additions & 5 deletions spacy_wordnet/wordnet_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 17 additions & 12 deletions tests/test_wordnet_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,35 @@


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()
assert token._.wordnet.wordnet_domains()

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)
Expand All @@ -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()
Expand Down

0 comments on commit ada1077

Please sign in to comment.