在wordnet中查找nouns的同义词

时间:2015-03-18 03:41:11

标签: python nltk wordnet synonym

我想知道是否有一种简单的方法可以在wordnet中获取名词的同义词。似乎形容词的同义词很容易获得。

for ss in wn.synsets('beautiful'):
    print(ss)
    for sim in ss.similar_tos():
        print('    {}'.format(sim))

我从另一个SO问题中找到了上面的代码,它适用于形容词。但是,当我的话是“汽油”时。或者' fire'结果很可怕。理想情况下,我会得到一个与this网站非常相似的单词列表。

我尝试过的其他方法效果很好,但速度极慢:

def syn(word, lch_threshold=2.26):
for net1 in wn.all_synsets():
    try:
        lch = net1.lch_similarity(wn.synset(word))
    except:
        continue
    # The value to compare the LCH to was found empirically.
    # (The value is very application dependent. Experiment!)
    if lch >= lch_threshold:
        yield (net1, lch)

for x in syn('gasoline.n.1'):
    print  x

另一个SO问题也发现了这个问题。是否有更简单的方法来获取名词的同义词,如上面提供的链接?

2 个答案:

答案 0 :(得分:2)

这是一种获取同义词的hacky方式。我尝试了一些词库API,但没有得到我想要的。

def get_syns(old_words):
    new_words = dict()
    for word, score in old_words.iteritems():
       new_words[word] = score
       for syn in get_web_syns(word):
           new_words[syn] = 1
    return new_words

def get_web_syns(word):
    req = requests.get('http://www.thesaurus.com/browse/' + word)
    soup = BeautifulSoup(req.text, 'html.parser')
    all_syns = soup.find('div', {'class' : 'relevancy-list'})
    syns = []
    for ul in all_syns.findAll('ul'):
        for li in ul.findAll('span', {'class':'text'}):
            syns.append(li.text.split()[0])
    return syns

cold = {'icy':2, 'ice':1, 'snow':1}
get_syns(cold)

返回: {u'algific':1,  u'antarctic':1,  u'arctic':1,  u'biting':1,  u'bitter':1,  u'blizzard':1,  你好':1,  u'chilled':1,  u'chilling':1,  u'chilly':1,  你好':1,  你好':1,  u'crystal':1,  u'cube':1,  u'diamonds':1,  u'dry':1,  u'floe':1,  u'freezing':1,  u'frigid':1,  u'frigorific':1,  u'frost-bound':1,  u'frosty':1,  u'frozen':1,  u'gelid':1,  u'glacial':1,  u'glacier':1,  u'glaring':1,  u'glaze':1,  u'hail':1,  u'hailstone':1,  '冰':1,  u'iceberg':1,  u'iced':1,  u'icicle':1,  '冰冷':2,  u'permafrost':1,  u'polar':1,  u'raw':1,  你好吗':1,  u'rimy':1,  你的'hivering':1,  你的':'1,  u'sleet':1,  u'sleeted':1,  你顺利':1,  '雪':1,  u'snowfall':1}

dict用于为我的特定应用程序分配单词分数。

答案 1 :(得分:0)

无论您是处理名词,动词还是形容词:您始终可以通过Synset.lemma()获取同义词的同义词,例如wn.synsets('gasoline')[0].lemmas()

相关问题