2017-07-29 71 views
0

我试图执行苏丹单语对齐找到使用NLTK wordnet synsets的同义词。为什么打印的单词对齐列表重复?

我有两个列表:基于对准规则

word1 = ['move', 'buy','learn'] 
word2 = ['study', 'purchase'] 

,如果的word1word1[i]同义集是的word2word2[j],然后word1[i]word2[j]同义词集合类似将对齐。

这里是我的代码:

from nltk.corpus import wordnet as wn 

def getSynonyms(word): 
    synonymList1 = [] 
    wordnetSynset1 = wn.synsets(word) 
    tempList1=[] 
    for synset1 in wordnetSynset1: 
     synLemmas = synset1.lemma_names() 
     for i in xrange(len(synLemmas)): 
      word = synLemmas[i].replace('_',' ') 
      if word not in tempList1: 
       tempList1.append(word) 
    synonymList1.append(tempList1) 
    return synonymList1 

def cekSynonyms(word1, word2): 
    newlist = [] 
    for i in xrange(len(word1)): 
     for j in xrange(len(word2)): 
      getsyn1 = getSynonyms(word1[i]) 
      getsyn2 = getSynonyms(word2[j]) 
      ds1 = [x for y in getsyn1 for x in y] 
      ds2 = [x for y in getsyn2 for x in y] 
      print ds1,"---align to--->",ds2,"\n" 
      for k in xrange(len(ds1)): 
       for l in xrange(len(ds2)): 
        if ds1[k] == ds2[l]: 
         #newsim = [ds1[k], ds2[l]] 
         newsim = [word1[i], word2[j]] 
         newlist.append(newsim) 
    return newlist 

word1 = ['move', 'buy','learn'] 
word2 = ['study', 'purchase'] 
print cekSynonyms(word1, word2) 

是的,我能找到的每一个字同义集。这里的输出:上述

[u'move', u'relocation', u'motion', u'movement', u'motility', u'travel', u'go', u'locomote', u'displace', u'proceed', u'be active', u'act', u'affect', u'impress', u'strike', u'motivate', u'actuate', u'propel', u'prompt', u'incite', u'run', u'make a motion'] ---align to---> [u'survey', u'study', u'work', u'report', u'written report', u'discipline', u'subject', u'subject area', u'subject field', u'field', u'field of study', u'bailiwick', u'sketch', u'cogitation', u'analyze', u'analyse', u'examine', u'canvass', u'canvas', u'consider', u'learn', u'read', u'take', u'hit the books', u'meditate', u'contemplate'] 

[u'move', u'relocation', u'motion', u'movement', u'motility', u'travel', u'go', u'locomote', u'displace', u'proceed', u'be active', u'act', u'affect', u'impress', u'strike', u'motivate', u'actuate', u'propel', u'prompt', u'incite', u'run', u'make a motion'] ---align to---> [u'purchase', u'leverage', u'buy'] 

[u'bargain', u'buy', u'steal', u'purchase', u'bribe', u'corrupt', u"grease one's palms"] ---align to---> [u'survey', u'study', u'work', u'report', u'written report', u'discipline', u'subject', u'subject area', u'subject field', u'field', u'field of study', u'bailiwick', u'sketch', u'cogitation', u'analyze', u'analyse', u'examine', u'canvass', u'canvas', u'consider', u'learn', u'read', u'take', u'hit the books', u'meditate', u'contemplate'] 

[u'bargain', u'buy', u'steal', u'purchase', u'bribe', u'corrupt', u"grease one's palms"] ---align to---> [u'purchase', u'leverage', u'buy'] 

[u'learn', u'larn', u'acquire', u'hear', u'get word', u'get wind', u'pick up', u'find out', u'get a line', u'discover', u'see', u'memorize', u'memorise', u'con', u'study', u'read', u'take', u'teach', u'instruct', u'determine', u'check', u'ascertain', u'watch'] ---align to---> [u'survey', u'study', u'work', u'report', u'written report', u'discipline', u'subject', u'subject area', u'subject field', u'field', u'field of study', u'bailiwick', u'sketch', u'cogitation', u'analyze', u'analyse', u'examine', u'canvass', u'canvas', u'consider', u'learn', u'read', u'take', u'hit the books', u'meditate', u'contemplate'] 

[u'learn', u'larn', u'acquire', u'hear', u'get word', u'get wind', u'pick up', u'find out', u'get a line', u'discover', u'see', u'memorize', u'memorise', u'con', u'study', u'read', u'take', u'teach', u'instruct', u'determine', u'check', u'ascertain', u'watch'] ---align to---> [u'purchase', u'leverage', u'buy'] 

[['buy', 'purchase'], ['buy', 'purchase'], ['learn', 'study'], ['learn', 'study'], ['learn', 'study'], ['learn', 'study']] 

的6行是内两者的word1word2每个单词通过同义词集进行比较。

底行是对齐的单词。

正如我们通过synsets所看到的,['buy','purchase']['learn','study']是对齐的单词。

但为什么输出打印不止一次?像这样>>[['buy', 'purchase'], ['buy', 'purchase'], ['learn', 'study'], ['learn', 'study'], ['learn', 'study'], ['learn', 'study']]

如何在没有重复的情况下只打印一次?像这样>>[['buy','purchase'], ['learn','study']]

回答

1

您可以将其转换为一组删除这样的名单副本,虽然因为列表不是可哈希的,你必须要经过的元组的方式:

a = [['buy', 'purchase'], ['buy', 'purchase'], ['learn', 'study'], \\ 
    ['learn', 'study'], ['learn', 'study'], ['learn', 'study']] 
a = [list(x) for x in set([tuple(x) for x in a])] 
print(a) 

给:

[['buy', 'purchase'], ['learn', 'study']] 
+0

哇哇!谢谢先生 – sang

0

基于先生。 nbubis回答,这里我编码一个元组函数:

def tupleSynonyms(word1, word2): 
    a = cekSynonyms(word1, word2) 
    anew = [list(x) for x in set([tuple(x) for x in a])] 
    return anew 

print tupleSynonyms(word1, word2) 
相关问题