python - how to get a synsets for only one string in a list of strings? -
i'm trying find synsets of strings inside list. here's code :
from nltk.corpus import wordnet wn nltk import pos_tag word1 = ['orange','man','bench'] def getsynonyms(word1): synonymlist1 = [] data1 in word1: wordnetsynset1 = wn.synsets(data1) templist1=[] synset1 in wordnetsynset1: synlemmas = synset1.lemma_names() in xrange(len(synlemmas)): word = synlemmas[i].replace('_',' ') if pos_tag(word.split()) not in templist1: templist1.append(pos_tag(word.split())) synonymlist1.append(templist1) return synonymlist1 print getsynonyms(word1) print
and here's result :
[[[(u'orange', 'nn')], [(u'orangeness', 'nn')], [(u'orange', 'nn'), (u'tree', 'nn')], [(u'orange', 'nn')], [(u'orange', 'nnp'), (u'river', 'nnp')], [(u'orangish', 'jj')]], [[(u'man', 'nn')], [(u'adult', 'nn'), (u'male', 'nn')], [(u'serviceman', 'nn')], [(u'military', 'jj'), (u'man', 'nn')], [(u'military', 'jj'), (u'personnel', 'nns')], [(u'homo', 'nn')], [(u'human', 'jj'), (u'being', 'vbg')], [(u'human', 'nn')], [(u'valet', 'nn')], [(u'valet', 'nn'), (u'de', 'in'), (u'chambre', 'nn')], [(u'gentleman', 'nn')], [(u"gentleman's", 'nn'), (u'gentleman', 'nn')], [(u'man', 'nn')], [(u'isle', 'nnp'), (u'of', 'in'), (u'man', 'nnp')], [(u'piece', 'nn')], [(u'world', 'nn')], [(u'human', 'jj'), (u'race', 'nn')], [(u'humanity', 'nn')], [(u'humankind', 'nn')], [(u'human', 'jj'), (u'beings', 'nns')], [(u'humans', 'nns')], [(u'mankind', 'nn')]], [[(u'bench', 'nn')], [(u'terrace', 'nn')], [(u'judiciary', 'nn')], [(u'workbench', 'nn')], [(u'work', 'nn'), (u'bench', 'nn')], [(u'bench', 'nn')]]]
but if want synsets of 1 string?
for examples, if synsets 'orange'
, , print :
[(u'orange', 'nn')], [(u'orangeness', 'nn')], [(u'orange', 'nn'), (u'tree', 'nn')], [(u'orange', 'nn')], [(u'orange', 'nnp'), (u'river', 'nnp')], [(u'orangish', 'jj')]
if synsets 'man'
, , print :
[(u'man', 'nn')], [(u'adult', 'nn'), (u'male', 'nn')], [(u'serviceman', 'nn')], [(u'military', 'jj'), (u'man', 'nn')], [(u'military', 'jj'), (u'personnel', 'nns')], [(u'homo', 'nn')], [(u'human', 'jj'), (u'being', 'vbg')], [(u'human', 'nn')], [(u'valet', 'nn')], [(u'valet', 'nn'), (u'de', 'in'), (u'chambre', 'nn')], [(u'gentleman', 'nn')], [(u"gentleman's", 'nn'), (u'gentleman', 'nn')], [(u'man', 'nn')], [(u'isle', 'nnp'), (u'of', 'in'), (u'man', 'nnp')], [(u'piece', 'nn')], [(u'world', 'nn')], [(u'human', 'jj'), (u'race', 'nn')], [(u'humanity', 'nn')], [(u'humankind', 'nn')], [(u'human', 'jj'), (u'beings', 'nns')], [(u'humans', 'nns')], [(u'mankind', 'nn')]
and 'bench'
i've tried print getsynonyms(word1[0])
result weird.
anyone can help? thanks
that function wants array, why weird results passing single string. if pass single word synsets each letter of word. can pass array 1 value though:
print getsynonyms([word1[0]])
you rewrite function remove out loop allowing pass single words in like:
def getsynonyms(word): synonymlist1 = [] wordnetsynset1 = wn.synsets(word) templist1=[] synset1 in wordnetsynset1: synlemmas = synset1.lemma_names() in xrange(len(synlemmas)): word = synlemmas[i].replace('_',' ') if pos_tag(word.split()) not in templist1: templist1.append(pos_tag(word.split())) synonymlist1.append(templist1) return synonymlist1
Comments
Post a Comment