python - Why the tmp still print 1 although there is no same synsets? -


i have 2 lists compare:

word1 =  ['orange'] word2 =  ['woman'] 

and have function find synsets each word inside list :

def getsynonyms(word): synonymlist1 = [] data1 in word:     wordnetsynset1 = wn.synsets(data1)     templist1=[]     synset1 in wordnetsynset1:         synlemmas = synset1.lemma_names()         in xrange(len(synlemmas)):             word = synlemmas[i].replace('_',' ')             #templist1.append(pos_tag(word.split()))             #if pos_tag(word.split()) not in templist1:                 #templist1.append(pos_tag(word.split()))             if word not in templist1:                 templist1.append(word)     synonymlist1.append(templist1) return synonymlist1  ds1 = getsynonyms([word1[0]]) ds2 = getsynonyms([word2[0]]) newds1 = ",".join(repr(e) e in ds1) newds2 = ",".join(repr(e) e in ds2)  print newds1 print newds2 

and output :

[u'orange', u'orangeness', u'orange tree', u'orange', u'orange river', u'orangish'] [u'woman', u'adult female', u'charwoman', u'char', u'cleaning woman', u'cleaning lady', u'womanhood', u'fair sex'] 

and have function. function checks if there similar synset between word1 , word2. if similar word found, function return 1 means found. :

def ceksynonyms(word1, word2):     tmp = 0     ds1 = getsynonyms([word1[0]])     ds2 = getsynonyms([word2[0]])     newds1 = ",".join(repr(e) e in ds1)     newds2 = ",".join(repr(e) e in ds2)      in newds1:         j in newds2:             if == j:                 tmp = 1             else:                 tmp = 0     return tmp  print ceksynonyms(word1, word2) 

but output 1.

it looks there no similar synset between word1 , word2. why still results 1 not 0?

your loops check full lists , overwrites tmp in each iteration. last elements in lists determine returned value. maybe should stop checking like

for in newds1:     j in newds2:         if == j:             tmp = 1             print(i, j) #this tell why 1 returned             return tmp # <<<< return function          else:             tmp = 0 return tmp 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -