highlight
模块¶
突出显示模块包含类和函数,用于在显示给用户的搜索结果中显示点击文档的简短摘录,并突出显示查询词。
突出显示系统有四个主要元素。
Fragmenters 根据文本中匹配词的位置,将原始文本切碎为片段。
Scorers 为每个片段分配一个分数,允许系统根据任何标准对最佳片段进行排序。
Order functions 控制向用户显示得分最高的片段的顺序。例如,您可以按照片段在文档中出现的顺序(第一个)显示片段,或者先显示得分较高的片段(得分)
Formatters 将片段对象转换为人类可读的输出,例如HTML字符串。
见 如何创建突出显示的搜索结果摘要 更多信息。
见 how to highlight terms in search results .
手动突出显示¶
-
class
whoosh.highlight.
Highlighter
(fragmenter=None, scorer=None, formatter=None, always_retokenize=False, order=<function FIRST>)¶
-
whoosh.highlight.
highlight
(text, terms, analyzer, fragmenter, formatter, top=3, scorer=None, minscore=1, order=<function FIRST>, mode='query')¶
碎片化¶
-
class
whoosh.highlight.
Fragmenter
¶ -
fragment_matches
(text, matched_tokens)¶ 生成
Fragment
基于文本和匹配项的对象。- 参数
text -- 突出显示的字符串。
matched_tokens -- 一览表
analysis.Token
对象表示字符串中的术语匹配。
-
fragment_tokens
(text, all_tokens)¶ 生成
Fragment
基于标记化文本的对象。- 参数
text -- 突出显示的字符串。
all_tokens -- 一个迭代器
analysis.Token
字符串中的对象。
-
must_retokenize
()¶ 如果此碎片需要重新录制文本,则返回true。
如果此方法返回true,则片段器的
fragment_tokens
方法将使用文本中所有标记的迭代器调用,匹配项的标记具有matched
属性设置为true。If this method returns False, the fragmenter's
fragment_matches
将使用匹配的标记列表调用方法。
-
-
class
whoosh.highlight.
WholeFragmenter
(charlimit=32768)¶ 不分割令牌流。这个对象只是将整个流作为一个“片段”返回。如果要突出显示整个文本,这很有用。
请注意,即使使用 WholeFragmenter, 如果给定字段中没有匹配的术语,突出显示代码将不返回任何片段。要返回整个片段,即使在这种情况下,也要调用 highlights() with minscore=0: :
# Query where no terms match in the "text" field q = query.Term("tag", "new") r = mysearcher.search(q) r.fragmenter = highlight.WholeFragmenter() r.formatter = highlight.UppercaseFormatter() # Since no terms in the "text" field matched, we get no fragments back assert r[0].highlights("text") == "" # If we lower the minimum score to 0, we get a fragment even though it # has no matching terms assert r[0].highlights("text", minscore=0) == "This is the text field."
-
class
whoosh.highlight.
SentenceFragmenter
(maxchars=200, sentencechars='.!?', charlimit=32768)¶ 在句尾标点字符(“.”“,!”上分隔文本。还是“?”。此对象通过在原始文本中查找句尾作为每个标记的“endchar”后的下一个字符来工作。
使用此碎片突出显示时,应使用不删除停止词的分析器,例如:
sa = StandardAnalyzer(stoplist=None)
- 参数
maxchars -- 片段中允许的最大字符数。
-
class
whoosh.highlight.
ContextFragmenter
(maxchars=200, surround=20, charlimit=32768)¶ 查找匹配的术语,并将它们与周围的上下文进行聚合。
- 参数
maxchars -- 片段中允许的最大字符数。
surround -- 要在第一个匹配项之前和最后一个匹配项之后添加的上下文的额外字符数。
-
class
whoosh.highlight.
PinpointFragmenter
(maxchars=200, surround=20, autotrim=False, charlimit=32768)¶ 这是一个不可修复的碎片。它从匹配项的位置构建片段。
- 参数
maxchars -- 片段中允许的最大字符数。
surround -- 要在第一个匹配项之前和最后一个匹配项之后添加的上下文的额外字符数。
autotrim -- 自动修剪片段中第一个空格之前和最后一个空格之后的文本,以避免在开头和结尾处截断单词。对于间隔较长的短片段或碎片,这可能会产生奇怪的结果。
使用格式器¶
-
class
whoosh.highlight.
UppercaseFormatter
(between='...')¶ 返回匹配项为大写的字符串。
- 参数
between -- 要在片段之间添加的文本。
-
class
whoosh.highlight.
HtmlFormatter
(tagname='strong', between='...', classname='match', termclass='term', maxclasses=5, attrquote='"')¶ Returns a string containing HTML formatting around the matched terms.
此格式化程序用两个类名将匹配的术语包装在一个HTML元素中。第一个类名(用构造函数参数设置
classname
)每场比赛都是一样的。第二个类名(用构造函数参数设置termclass
根据匹配的术语不同。这允许您对摘录中的不同术语使用不同的格式(例如,不同的背景色)。>>> hf = HtmlFormatter(tagname="span", classname="match", termclass="term") >>> hf(mytext, myfragments) "The <span class="match term0">template</span> <span class="match term1">geometry</span> is..."
此对象维护将术语映射到HTML类名(例如
term0
和term1
上面),这样多个摘录将在同一个术语中使用相同的类。如果要在不同的搜索中重复使用相同的htmlformatter对象,则应在搜索之间调用htmlformatter.clear()以清除映射。- 参数
tagname -- 要环绕匹配项的标记。
between -- 要在片段之间添加的文本。
classname -- 要添加到围绕匹配项包装的元素中的类名。
termclass -- 第二个类的类名前缀,对于每个匹配的术语来说都是不同的。
maxclasses -- 要生成的术语类的最大数目。这通过回收术语类名来限制必须在CSS中定义的类的数量。例如,如果将MaxClasses设置为3并有5个术语,则5个术语将使用CSS类
term0
,term1
,term2
,term0
,term1
.
-
class
whoosh.highlight.
GenshiFormatter
(qname='strong', between='...')¶ 返回一个genshi事件流,其中包含匹配项周围的HTML格式。
- 参数
qname -- 标签的QName,用于环绕匹配的术语。
between -- 要在片段之间添加的文本。
实用类¶
-
class
whoosh.highlight.
Fragment
(text, matches, startchar=0, endchar=-1)¶ 表示命中文档的片段(提取)。此对象主要用于跟踪片段的起点和终点以及其中的“匹配”字符范围;它不包含片段的文本或执行其他许多操作。
有用的属性有:
Fragment.text
从中提取此片段的整个原始文本。
Fragment.matches
表示片段中匹配项的对象的有序列表。这些物体
startchar
和endchar
属性。Fragment.startchar
片段中第一个字符的索引。
Fragment.endchar
片段中最后一个字符的索引。
Fragment.matched_terms
A
set
的text
片段中匹配的术语(如果可用)。
- 参数
text -- 片段的源文本。
matches -- 对象列表
startchar
和endchar
属性,以及可选的text
属性。startchar -- 索引进入
text
片段从这里开始。默认值为0。endchar -- 索引进入
text
碎片的末端。默认值为-1,解释为text
.