【问题标题】:Fuzzy text search in PythonPython中的模糊文本搜索
【发布时间】:2015-05-26 04:25:56
【问题描述】:

我想知道是否有 Python 库可以进行模糊文本搜索。例如:

  • 我有三个关键字“letter”“stamp”“mail”
  • 我想要一个功能来检查这三个词是否在 相同的段落(或一定距离,一页)。
  • 此外,这些单词必须保持相同的顺序。这三个词之间出现其他词也没关系。

我已经尝试过fuzzywuzzy,但没有解决我的问题。另一个库 Whoosh,看起来很强大,但我没有找到合适的功能。

【问题讨论】:

标签: python full-text-search fuzzy-search


【解决方案1】:

{1} 您可以在Whoosh 2.7 中执行此操作。通过添加插件whoosh.qparser.FuzzyTermPlugin实现模糊搜索:

whoosh.qparser.FuzzyTermPlugin 可让您搜索“模糊”字词,即不必完全匹配的字词。模糊术语将匹配一定数量的“编辑”(字符插入、删除和/或换位 - 这称为“Damerau-Levenshtein 编辑距离”)内的任何相似术语。

添加模糊插件:

parser = qparser.QueryParser("fieldname", my_index.schema)
parser.add_plugin(qparser.FuzzyTermPlugin())

将模糊插件添加到解析器后,您可以通过添加~ 后跟可选的最大编辑距离来指定模糊术语。如果不指定编辑距离,则默认为 1。

例如,以下“模糊”术语查询:

letter~
letter~2
letter~2/3

{2}要保持单词顺序,请使用查询whoosh.query.Phrase,但您应该将Phrase 插件替换为whoosh.qparser.SequencePlugin,这样您就可以在短语中使用模糊术语:

"letter~ stamp~ mail~"

用序列插件替换默认的词组插件:

parser = qparser.QueryParser("fieldname", my_index.schema)
parser.remove_plugin_class(qparser.PhrasePlugin)
parser.add_plugin(qparser.SequencePlugin())

{3} 要允许单词之间出现,请将 Phrase 查询中的 slop arg 初始化为更大的数字:

whoosh.query.Phrase(fieldname, words, slop=1, boost=1.0, char_ranges=None)

slop – 短语中每个“单词”之间允许的单词数;默认值 1 表示短语必须完全匹配。

您也可以像这样在 Query 中定义 slop:

"letter~ stamp~ mail~"~10

{4}整体解决方案:

{4.a} Indexer 就像:

from whoosh.index import create_in
from whoosh.fields import *

schema = Schema(title=TEXT(stored=True), content=TEXT)
ix = create_in("indexdir", schema)
writer = ix.writer()
writer.add_document(title=u"First document", content=u"This is the first document we've added!")
writer.add_document(title=u"Second document", content=u"The second one is even more interesting!")
writer.add_document(title=u"Third document", content=u"letter first, stamp second, mail third")
writer.add_document(title=u"Fourth document", content=u"stamp first, mail third")
writer.add_document(title=u"Fivth document", content=u"letter first,  mail third")
writer.add_document(title=u"Sixth document", content=u"letters first, stamps second, mial third wrong")
writer.add_document(title=u"Seventh document", content=u"stamp first, letters second, mail third")
writer.commit()

{4.b} Searcher 就像:

from whoosh.qparser import QueryParser, FuzzyTermPlugin, PhrasePlugin, SequencePlugin

with ix.searcher() as searcher:
    parser = QueryParser(u"content", ix.schema)
    parser.add_plugin(FuzzyTermPlugin())
    parser.remove_plugin_class(PhrasePlugin)
    parser.add_plugin(SequencePlugin())
    query = parser.parse(u"\"letter~2 stamp~2 mail~2\"~10")
    results = searcher.search(query)
    print "nb of results =", len(results)
    for r in results:
        print r

这给出了结果:

nb of results = 2
<Hit {'title': u'Sixth document'}>
<Hit {'title': u'Third document'}>

{5}如果您想将模糊搜索设置为默认而不在查询的每个单词中使用语法word~n,您可以像这样初始化QueryParser

 from whoosh.query import FuzzyTerm
 parser = QueryParser(u"content", ix.schema, termclass = FuzzyTerm)

现在您可以使用查询"letter stamp mail"~10,但请记住FuzzyTerm 具有默认编辑距离maxdist = 1。如果您想要更大的编辑距离,请个性化课程:

class MyFuzzyTerm(FuzzyTerm):
     def __init__(self, fieldname, text, boost=1.0, maxdist=2, prefixlength=1, constantscore=True):
         super(D, self).__init__(fieldname, text, boost, maxdist, prefixlength, constantscore) 
         # super().__init__() for Python 3 I think

参考资料:

  1. whoosh.query.Phrase
  2. Adding fuzzy term queries
  3. Allowing complex phrase queries
  4. class whoosh.query.FuzzyTerm
  5. qparser module

【讨论】:

  • 哇,非常感谢您编写这个独立的示例!非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2016-06-12
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 1970-01-01
  • 2013-02-18
  • 2016-03-15
  • 2016-07-26
相关资源
最近更新 更多