spelling 模块

correcting errors in user queries .

此模块包含用于更正用户查询中输入错误的帮助程序函数。

校正器对象

class whoosh.spelling.Corrector

拼写更正对象的基类。具体的子类应该实现 _suggestions 方法。

suggest(text, limit=5, maxdist=2, prefix=0)
参数
  • text -- the text to check. 这个词会 not 添加到建议中,即使它出现在单词图中。

  • limit -- 只回复这么多建议。如果字段中没有足够的术语 maxdist 对于给定的单词,返回的列表将短于此数字。

  • maxdist -- 从给定单词到要查看的最大编辑距离。大于2的值不是非常有效或有效。

  • prefix -- require suggestions to share a prefix of this length with the given word. This is often justifiable since most misspellings do not involve the first letter of the word. Using a prefix dramatically decreases the time it takes to generate the list of words.

class whoosh.spelling.ReaderCorrector(reader, fieldname, fieldobj)

根据读者中字段的内容建议更正。

按编辑距离排列建议,然后按最高频率到最低频率排列建议。

class whoosh.spelling.MultiCorrector(correctors, op)

合并子更正器列表中的建议。

QueryCorrector对象

class whoosh.spelling.QueryCorrector(fieldname)

用于更正用户查询中单词的对象的基类。

correct_query(q, qstring)

返回A Correction 对象,表示给定查询的正确形式。

参数
  • q -- 原文 whoosh.query.Query 要更正的树。

  • qstring -- 原始用户查询。如果原始查询字符串不可用,则此值可能为“无”,在这种情况下, Correction.string 属性也将为“无”。

返回类型

Correction

class whoosh.spelling.SimpleQueryCorrector(correctors, terms, aliases=None, prefix=0, maxdist=2)

基于字段名到的映射的简单查询更正器 Corrector 对象和 ("fieldname", "text") 要更正的元组。查询中出现在术语元组列表中的术语将使用适当的更正器进行更正。

参数
  • correctors -- 将字段名映射到的字典 Corrector 物体。

  • terms -- 一系列 ("fieldname", "text") 表示要更正的项的元组。

  • aliases -- 将查询中的字段名映射到字段名以获取拼写建议的词典。

  • prefix -- 建议的替换词必须与原始词共享这个数量的初始字符。甚至增加到 1 可以大大加快建议的速度,而且可能是合理的,因为拼写错误很少涉及单词的第一个字母。

  • maxdist -- 原始单词和任何建议之间允许的“编辑”的最大数目(插入、删除、替换或换位)。值高于 2 可能是缓慢的。

class whoosh.spelling.Correction(q, qstring, corr_q, tokens)

表示用户查询字符串的正确版本。具有以下属性:

query

被纠正的 whoosh.query.Query 对象。

string

已更正的用户查询字符串。

original_query

原版 whoosh.query.Query 已更正的对象。

original_string

原始用户查询字符串。

tokens

表示已更正单词的标记对象列表。

您也可以使用 Correction.format_string() 方法使用 whoosh.highlight.Formatter 班级。例如,要将更正后的查询字符串显示为HTML,并强调更改后的单词:

from whoosh import highlight

correction = mysearcher.correct_query(q, qstring)

hf = highlight.HtmlFormatter(classname="change")
html = correction.format_string(hf)