在 Python 中使用 Whoosh 进行模糊字符串搜索
我在 MongoDB 中建立了一个大型银行数据库。我可以轻松地获取这些信息并用它快速创建索引。例如,我希望能够匹配银行名称“Eagle Bank &”密苏里州信托公司”和“密苏里州伊格尔银行和信托公司”。下面的代码适用于简单的模糊,但无法实现上面的匹配:
from whoosh.index import create_in
from whoosh.fields import *
schema = Schema(name=TEXT(stored=True))
ix = create_in("indexdir", schema)
writer = ix.writer()
test_items = [u"Eagle Bank and Trust Company of Missouri"]
writer.add_document(name=item)
writer.commit()
from whoosh.qparser import QueryParser
from whoosh.query import FuzzyTerm
with ix.searcher() as s:
qp = QueryParser("name", schema=ix.schema, termclass=FuzzyTerm)
q = qp.parse(u"Eagle Bank & Trust Co of Missouri")
results = s.search(q)
print results
给我:
<Top 0 Results for And([FuzzyTerm('name', u'eagle', boost=1.000000, minsimilarity=0.500000, prefixlength=1), FuzzyTerm('name', u'bank', boost=1.000000, minsimilarity=0.500000, prefixlength=1), FuzzyTerm('name', u'trust', boost=1.000000, minsimilarity=0.500000, prefixlength=1), FuzzyTerm('name', u'co', boost=1.000000, minsimilarity=0.500000, prefixlength=1), FuzzyTerm('name', u'missouri', boost=1.000000, minsimilarity=0.500000, prefixlength=1)]) runtime=0.00166392326355>
是否可以用 Whoosh 实现我想要的?如果没有,我还有哪些其他基于 python 的解决方案?
I've built up a large database of banks in MongoDB. I can easily take this information and create indexes with it in whoosh. For example I'd like to be able to match the bank names 'Eagle Bank & Trust Co of Missouri' and 'Eagle Bank and Trust Company of Missouri'. The following code works with simple fuzzy such, but cannot achieve a match on the above:
from whoosh.index import create_in
from whoosh.fields import *
schema = Schema(name=TEXT(stored=True))
ix = create_in("indexdir", schema)
writer = ix.writer()
test_items = [u"Eagle Bank and Trust Company of Missouri"]
writer.add_document(name=item)
writer.commit()
from whoosh.qparser import QueryParser
from whoosh.query import FuzzyTerm
with ix.searcher() as s:
qp = QueryParser("name", schema=ix.schema, termclass=FuzzyTerm)
q = qp.parse(u"Eagle Bank & Trust Co of Missouri")
results = s.search(q)
print results
gives me:
<Top 0 Results for And([FuzzyTerm('name', u'eagle', boost=1.000000, minsimilarity=0.500000, prefixlength=1), FuzzyTerm('name', u'bank', boost=1.000000, minsimilarity=0.500000, prefixlength=1), FuzzyTerm('name', u'trust', boost=1.000000, minsimilarity=0.500000, prefixlength=1), FuzzyTerm('name', u'co', boost=1.000000, minsimilarity=0.500000, prefixlength=1), FuzzyTerm('name', u'missouri', boost=1.000000, minsimilarity=0.500000, prefixlength=1)]) runtime=0.00166392326355>
Is it possible to achieve what I want with Whoosh? If not what other python based solutions do I have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 Whoosh 中的模糊搜索将
Co
与Company
匹配,但您不应该这样做,因为 < code>Co 和Company
很大。Co
类似于Company
,因为Be
类似于Beast
,ny
类似于 < code>Company,你可以想象搜索结果会有多糟糕、有多大。但是,如果您想将
Compan
或compani
或Companee
与Company
匹配,您可以使用个性化FuzzyTerm
类,默认maxdist
等于 2 或更大:然后:
您可以通过将
maxdist
设置为5
来将Co
与Company
匹配,但正如我所说,这会给出错误的搜索结果。我建议将maxdist
保持在1
到3
之间。如果您正在寻找匹配的单词语言变体,您最好使用
whoosh .query.Variations
。注意:较旧的 Whoosh 版本具有
minsimilarity
而不是maxdist
。You could match
Co
withCompany
using Fuzzy Search in Whoosh but You shouldn't do because the difference betweenCo
andCompany
is large.Co
is similar toCompany
asBe
is similar toBeast
andny
toCompany
, You can imagine how bad and how large will be the search results.However, if you want to match
Compan
orcompani
orCompanee
toCompany
you could do it by using a Personalized Class ofFuzzyTerm
with defaultmaxdist
equal to 2 or more :Then:
You could match
Co
withCompany
by settingmaxdist
to5
but this as I said give bad search results. I suggest to keepmaxdist
from1
to3
.If you are looking for matching a word linguistic variations, you better use
whoosh.query.Variations
.Note: older Whoosh versions has
minsimilarity
instead ofmaxdist
.供将来参考,并且必须有更好的方法以某种方式做到这一点,但这是我的镜头。
For future reference, and there must be a better way to do this somehow, but here's my shot.
也许其中一些东西可能会有所帮助(由 Seatgeek 人员开源的字符串匹配):
https://github.com/seatgeek /fuzzywuzzy
Perhaps some of this stuff might help (string matching open sourced by the seatgeek guys):
https://github.com/seatgeek/fuzzywuzzy
对于最近遇到这个问题的任何人来说,看起来他们已经原生添加了模糊支持,尽管需要做一些工作才能满足此处概述的特定用例:https://whoosh.readthedocs.io/en/latest/parsing.html
For anyone stumbling across this question more recently, it looks like they've added fuzzy support natively, though it'd take a bit of work to satisfy the particular use case outlined here: https://whoosh.readthedocs.io/en/latest/parsing.html
您可以使用下面的此函数根据短语对一组单词进行模糊搜索:
You could use this function below to fuzz search a set of words against a phrase: