How to retrieve or create a list of things from wordnet - animals, household items or names?

Viewed 20

I currently have a Pandas dataframe scraped from reddit.com/r/cryptomoonshots with the following code:

df = pd.DataFrame([vars(post) for post in reddit.subreddit('cryptomoonshots').hot(limit=100)])
df = df[["title","score","url"]]
df.head()

That produces a readable df:

    title                                               score
3   Valor Game Token | Next X100 Gems | Insane Mar...   1135
4   Legends of Aragon token launch | NFT Game is a...   1085
5   TetheRhino Tomorrow Presale 16:00 UTC on DxSal...   833
6   GYM NETWORK The First DeFi Aggregator With Int...   442
7   Puli (PULI) is taking the BSC scene by storm! ...   1482

Since the first 1-3 words of any of these posts describes the coin itself being shilled, I want to pair them up with a list of things and then classify them accordingly. E.g., 'Beagle Coin' would be find as part of a string in a list containing called Dogs = ['Beagle', etc.]

The iteration part is not hard, but how would we generate a list of things to match these with?

I tried with wordnet and itertools:

from nltk.corpus import wordnet as wn
from itertools import chain
dogs = list(chain(*[i.lemma_names for i in wn.all_synsets() if "dog" in i.definition]))

but it gave me the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-7da682828270> in <module>
      1 from itertools import chain
----> 2 dogs = list(chain(*[i.lemma_names for i in wn.all_synsets() if "dog" in i.definition]))

<ipython-input-33-7da682828270> in <listcomp>(.0)
      1 from itertools import chain
----> 2 dogs = list(chain(*[i.lemma_names for i in wn.all_synsets() if "dog" in i.definition]))

TypeError: argument of type 'method' is not iterable
1 Answers

definition in the failing line of your code is a method, not an instance variable of some iterable type. The if "dog" in i.definition part of your code fails as the in clause expects an iterable. Invoking the definition method should fix the error. And if you are looking to get a list of strings as the dogs object, you would need to invoke the lemma_names method too. So your list comprehension line should be:

dogs = list(chain(*[i.lemma_names() for i in wn.all_synsets() if "dog" in i.definition()]))
Related