检查两个词是否相互关联

检查两个词是否相互关联

本文介绍了检查两个词是否相互关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个清单:一是用户的利益;二是用户的利益.第二,关于一本书的关键字.我想根据用户的兴趣列表向用户推荐这本书.我正在使用Python库difflibSequenceMatcher类来匹配类似的单词,例如游戏",游戏",游戏",游戏者"等.ratio函数为我提供了一个介于[0, 1]说明2个字符串的相似程度.但是我陷入了一个例子,在那里我计算了循环"和射击"之间的相似度.出来是0.6667.

I have two lists: one, the interests of the user; and second, the keywords about a book. I want to recommend the book to the user based on his given interests list. I am using the SequenceMatcher class of Python library difflib to match similar words like "game", "games", "gaming", "gamer", etc. The ratio function gives me a number between [0,1] stating how similar the 2 strings are. But I got stuck at one example where I calculated the similarity between "looping" and "shooting". It comes out to be 0.6667.

for interest in self.interests:
    for keyword in keywords:
       s = SequenceMatcher(None,interest,keyword)
       match_freq = s.ratio()
       if match_freq >= self.limit:
            #print interest, keyword, match_freq
            final_score += 1
            break

还有其他方法可以在Python中执行这种匹配吗?

Is there any other way to perform this kind of matching in Python?

推荐答案

首先,一个单词可以具有多种含义,当您尝试查找相似的单词时,可能需要消除某些单词的歧义 http://en.wikipedia.org/wiki/Word-sense_disambiguation .

Firstly a word can have many senses and when you try to find similar words you might need some word sense disambiguation http://en.wikipedia.org/wiki/Word-sense_disambiguation.

给出一对单词,如果我们以最相似的一对感觉作为衡量两个单词是否相似的标准,我们可以尝试以下方法:

Given a pair of words, if we take the most similar pair of senses as the gauge of whether two words are similar, we can try this:

from nltk.corpus import wordnet as wn
from itertools import product

wordx, wordy = "cat","dog"
sem1, sem2 = wn.synsets(wordx), wn.synsets(wordy)

maxscore = 0
for i,j in list(product(*[sem1,sem2])):
  score = i.wup_similarity(j) # Wu-Palmer Similarity
  maxscore = score if maxscore < score else maxscore

您还可以使用其他相似功能. http://nltk.googlecode.com/svn/trunk/doc/howto /wordnet.html .唯一的问题是,当您遇到不在wordnet中的单词时.然后,我建议您退回difflib.

There are other similarity functions that you can use. http://nltk.googlecode.com/svn/trunk/doc/howto/wordnet.html. The only problem is when you encounter words not in wordnet. Then i suggest you fallback on difflib.

这篇关于检查两个词是否相互关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:58