2017-07-28 51 views
0

我有兩個列表進行比較:爲什麼tmp仍然打印1,雖然沒有相同的synsets?

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

然後,我有這個功能查找列表中的每個字同義集:

def getSynonyms(word): 
synonymList1 = [] 
for data1 in word: 
    wordnetSynset1 = wn.synsets(data1) 
    tempList1=[] 
    for synset1 in wordnetSynset1: 
     synLemmas = synset1.lemma_names() 
     for i 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) for e in ds1) 
newds2 = ",".join(repr(e) for e in ds2) 

print newds1 
print newds2 

,這是輸出:

[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'] 

然後我有另一個功能。該功能檢查word1word2之間是否有類似的同義詞。如果找到類似的單詞,那麼該函數將返回1,這意味着它被發現。 :

def cekSynonyms(word1, word2): 
    tmp = 0 
    ds1 = getSynonyms([word1[0]]) 
    ds2 = getSynonyms([word2[0]]) 
    newds1 = ",".join(repr(e) for e in ds1) 
    newds2 = ",".join(repr(e) for e in ds2) 

    for i in newds1: 
     for j in newds2: 
      if i == j: 
       tmp = 1 
      else: 
       tmp = 0 
    return tmp 

print cekSynonyms(word1, word2) 

但輸出是1

看起來在word1word2之間沒有類似的同義詞。但爲什麼它仍然結果1而不是0

+1

'newds1'(lol)是一個字符串。迭代它給字符。如果有任何字符被共享,它會是1 –

回答

0

您的循環始終檢查完整列表並在每次迭代中覆蓋tmp。所以列表中的最後一個元素決定了返回的值。也許你應該停止檢查,如

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

對不起,但它仍然是'1'雖然沒有類似synset – sang

+0

在返回語句之前添加一個print(i,j)來看看會發生什麼 – Joe

+0

哪些返回語句以及如何返回關於縮進? – sang

相關問題