检查两个词是否彼此相关 [英] check if two words are related to each other

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

问题描述

我有两个列表:一,用户的兴趣;第二,关于一本书的关键词.我想根据用户给定的兴趣列表向用户推荐这本书.我正在使用 Python 库 difflibSequenceMatcher 类来匹配类似的词,如游戏"、游戏"、游戏"、玩家"等.ratio 函数给了我一个 [0,1] 之间的数字,说明两个字符串的相似程度.但是我陷入了一个例子,我计算了循环"和射击"之间的相似性.结果是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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆