2011-07-05 22 views
6

说我有电影名称与拼写错误和小的变化像这样的列表 -什么是一个好的策略来分类相似的单词?

"Pirates of the Caribbean: The Curse of the Black Pearl" 
"Pirates of the carribean" 
"Pirates of the Caribbean: Dead Man's Chest" 
"Pirates of the Caribbean trilogy" 
"Pirates of the Caribbean" 
"Pirates Of The Carribean" 

如何组或找到这样套的话,最好使用python和/或Redis的?

+1

你想得到什么结果?你想要在整个字符串中查找所有这些变体? – JMax

+0

我想将这些组合成一个组合对象,并在添加到数据库时执行检查。 –

回答

14

看看“模糊匹配”。下面的线程中的一些很棒的工具可以计算字符串之间的相似度。

我特别喜欢difflib模块

>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy']) 
['apple', 'ape'] 
>>> import keyword 
>>> get_close_matches('wheel', keyword.kwlist) 
['while'] 
>>> get_close_matches('apple', keyword.kwlist) 
[] 
>>> get_close_matches('accept', keyword.kwlist) 
['except'] 

https://stackoverflow.com/questions/682367/good-python-modules-for-fuzzy-string-comparison

+0

链接的问题似乎被删除。看起来好像是 – hardmooth

+0

。当你达到一定程度的分数时,你仍然可以看到已删除的问题,因此我将链接保持原样。 –

+0

@FredrikPihl可以请你在这里发布'get_close_matches'的定义(或者编辑它以答复)不配得名声低的农民? –

1

为了另一个提示添加到弗雷德里克的答案,你也可以得到来自搜索引擎如代码,像这样的启发:

def dosearch(terms, searchtype, case, adddir, files = []): 
    found = [] 
    if files != None: 
     titlesrch = re.compile('>title<.*>/title<') 
     for file in files: 
      title = "" 
      if not (file.lower().endswith("html") or file.lower().endswith("htm")): 
       continue 
      filecontents = open(BASE_DIR + adddir + file, 'r').read() 
      titletmp = titlesrch.search(filecontents) 
      if titletmp != None: 
       title = filecontents.strip()[titletmp.start() + 7:titletmp.end() - 8] 
      filecontents = remove_tags(filecontents) 
      filecontents = filecontents.lstrip() 
      filecontents = filecontents.rstrip() 
      if dofind(filecontents, case, searchtype, terms) > 0: 
       found.append(title) 
       found.append(file) 
    return found 

来源和更多信息:http://www.zackgrossbart.com/hackito/search-engine-python/

问候,

最大

0

我相信其实也有两个不同的问题。

首先是拼写纠正。你可以有一个在Python这里

http://norvig.com/spell-correct.html

二是更多的功能。这是我在拼写更正后要做的事情。我会做一个关系函数。

相关(句子1,句子2)当且仅当句子1和句子2有罕见的常用词。难得的是,我的意思是不同于(The,what,is等等)。您可以查看TF/IDF系统,以确定两个文档是否使用他们的文字相关。只是google搜索了一下,我发现这一点:

https://code.google.com/p/tfidf/

3

您可能注意到了类似的字符串有大的公共子,例如:

“唧唧歪歪”和“喇嘛喇嘛胸罩” =>常见子字符串是“Bla bla ba”(请注意第三个字)

要找到常见子字符串,您可以使用动态编程算法。算法变体之一是Levenshtein距离(大多数相似字符串之间的距离非常小,并且更多不同字符串之间的距离更大) - http://en.wikipedia.org/wiki/Levenshtein_distance

也为了快速表现,您可以尝试适应Soundex算法 - http://en.wikipedia.org/wiki/Soundex

所以在计算所有字符串之间的距离之后,必须对它们进行聚类。最简单的方法是k-means(但它需要你定义数量的簇)。如果您实际上不知道集群的数量,则必须使用分层集群。 请注意,在您的情况下群集的数量是不同电影标题的数量+ 1(对于完全不好的拼写字符串)。

+0

你所谓的子字符串“Bla bla ba”是而不是传统定义中的子字符串,因为“ba”不在您的字符串中。我会称之为“缺口子串”。从常见的有缺陷的子字符串中,您可以获得最长的无字符串子串,从而获得最长的公共子字符串。 – hardmooth

0

一种方法是在比较它们之前预处理所有字符串:将所有字符串转换为小写字母,标准化空格(例如,用单个空格替换任何空格)。如果标点符号对您的最终目标不重要,则也可以删除所有标点符号。

Levenshtein distance通常用于确定字符串的相似性,这应该可以帮助您将由于小的拼写错误而不同的字符串组合起来。

相关问题