突出显示模块包含类和函数,用于在显示给用户的搜索结果中显示点击文档的简短摘录,并突出显示查询词。
突出显示系统有四个主要元素。
见 如何创建突出显示的搜索结果摘要 更多信息。
见 how to highlight terms in search results .
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')¶whoosh.highlight.
Fragmenter
¶fragment_matches
(text, matched_tokens)¶生成 Fragment
基于文本和匹配项的对象。
参数: |
|
---|
fragment_tokens
(text, all_tokens)¶生成 Fragment
基于标记化文本的对象。
参数: |
|
---|
must_retokenize
()¶如果此碎片需要重新录制文本,则返回true。
如果此方法返回true,则片段器的 fragment_tokens
方法将使用文本中所有标记的迭代器调用,匹配项的标记具有 matched
属性设置为true。
If this method returns False, the fragmenter's fragment_matches
将使用匹配的标记列表调用方法。
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."
whoosh.highlight.
SentenceFragmenter
(maxchars=200, sentencechars='.!?', charlimit=32768)¶在句尾标点字符(“.”“,!”上分隔文本。还是“?”。此对象通过在原始文本中查找句尾作为每个标记的“endchar”后的下一个字符来工作。
使用此碎片突出显示时,应使用不删除停止词的分析器,例如:
sa = StandardAnalyzer(stoplist=None)
参数: | maxchars -- 片段中允许的最大字符数。 |
---|
whoosh.highlight.
ContextFragmenter
(maxchars=200, surround=20, charlimit=32768)¶查找匹配的术语,并将它们与周围的上下文进行聚合。
参数: |
|
---|
whoosh.highlight.
PinpointFragmenter
(maxchars=200, surround=20, autotrim=False, charlimit=32768)¶这是一个不可修复的碎片。它从匹配项的位置构建片段。
参数: |
|
---|
whoosh.highlight.
UppercaseFormatter
(between='...')¶返回匹配项为大写的字符串。
参数: | between -- 要在片段之间添加的文本。 |
---|
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()以清除映射。
参数: |
|
---|
whoosh.highlight.
GenshiFormatter
(qname='strong', between='...')¶返回一个genshi事件流,其中包含匹配项周围的HTML格式。
参数: |
|
---|
whoosh.highlight.
Fragment
(text, matches, startchar=0, endchar=-1)¶表示命中文档的片段(提取)。此对象主要用于跟踪片段的起点和终点以及其中的“匹配”字符范围;它不包含片段的文本或执行其他许多操作。
有用的属性有:
Fragment.text
Fragment.matches
startchar
和 endchar
属性。Fragment.startchar
Fragment.endchar
Fragment.matched_terms
set
的 text
片段中匹配的术语(如果可用)。参数: |
|
---|