Skip to content

Instantly share code, notes, and snippets.

@jaidevd
Created February 10, 2020 02:42
Show Gist options
  • Save jaidevd/6c46de8c4eed81f57adf3919aa298a7b to your computer and use it in GitHub Desktop.
Save jaidevd/6c46de8c4eed81f57adf3919aa298a7b to your computer and use it in GitHub Desktop.
Wordnet antonyms
#!/usr/bin/env python
# coding: utf-8
# In[1]:
get_ipython().run_line_magic('load_ext', 'autoreload')
get_ipython().run_line_magic('autoreload', '2')
# In[1]:
from nlg.utils import load_spacy_model
import pandas as pd
from nlg.search import templatize
# In[41]:
df = pd.read_csv('nlg/tests/data/actors.csv')
fh_args = {'_sort': ['-rating']}
# In[2]:
nlp = load_spacy_model()
# In[42]:
doc = nlp('James Stewart is the actor with the highest rating.')
# In[43]:
nugget = templatize(doc, fh_args, df)
# In[44]:
print(nugget)
# In[9]:
[(c.pos_, c.text) for c in doc]
# In[6]:
import lemminflect
# In[11]:
[(c.pos_, c._.lemma(), c.text) for c in doc]
# In[4]:
high = doc[-3]
# In[14]:
get_ipython().run_line_magic('pinfo', 'high._.get')
# In[8]:
lemminflect.getInflection('high', 'JJR')
# In[10]:
high
# In[11]:
high.tag_
# In[12]:
from spacy_wordnet.wordnet_annotator import WordnetAnnotator
# In[13]:
nlp.add_pipe(WordnetAnnotator(nlp.lang), after='tagger')
# In[14]:
doc = nlp(doc.text)
# In[25]:
get_ipython().run_line_magic('timeit', 'nlp(doc.text)')
# In[16]:
t = doc[-3]
# In[34]:
ants = [c.antonyms()[0] for c in t._.wordnet.lemmas() if len(c.antonyms()) > 0]
# In[37]:
[c.name() for c in ants]
# In[39]:
lemminflect.getInflection('low', 'JJS')
# In[24]:
type(ant)
# In[30]:
# In[28]:
t._.wordnet.lemmas()
# In[ ]:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment