I need to work on a project that require NLTK so I started learning Python two weeks ago but struggling to understand Python and NLTK.
From the NLTK documentation, I can understand the following codes and they work well if I manually add the word apple and pear into the codes below.
from nltk.corpus import wordnet as wn
apple = wn.synset('apple.n.01')
pear = wn.synset('pear.n.01')
print apple.lch_similarity(pear)
Output: 2.53897387106
However, I need to use the NLTK to work with a list of items. For example, I have a list of items below and I would like to compare the items from list1 with list2 – for example: compare word1 from list1 with every word in list 2, then word2 from list1 with every word from list2 until all words in list1 is compared.
list1 = ["apple", "honey", "drinks", "flowers", "paper"]
list2 = ["pear", "shell", "movie", "fire", "tree", "candle"]
wordFromList1 = list1[0]
wordFromList2 = list2[0]
wordFromList1 = wn.synset(wordFromList1)
wordFromList2 = wn.synset(wordFromList2)
print wordFromList1.lch_similarity(wordFromList2)
The codes above will of course gives an error. Can anyone show me how I can pass a variable into synset method [wn.synset(*pass_variable_in_here*)] so that I can use a double loop to get the lch_similarity values for them. Thank you.
wordnet.synsetexpects a 3-partname string of the form:
word.pos.nn.You did not specify the
pos.nnpart for each word inlist1andlist2.It seems reasonable to assume that all the words are nouns, so we could try
appending the string
'.n.01'to each string inlist1andlist2:That does not work, however.
wordnet.synset('drinks.n.01')raises aWordNetError.On the other hand, the same doc
page shows you can
lookup similar words using the
synsetsmethod:For example,
wordnet.synsets('drinks')returns the list:So at this point, you need to give some thought to what you want the program to do. If you are okay with just picking the first item in this list as a proxy for
drinks,then you could use
which would result in a program that looks like this:
which yields