本文整理汇总了Python中whoosh.highlight.SentenceFragmenter方法的典型用法代码示例。如果您正苦于以下问题:Python highlight.SentenceFragmenter方法的具体用法?Python highlight.SentenceFragmenter怎么用?Python highlight.SentenceFragmenter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类whoosh.highlight
的用法示例。
在下文中一共展示了highlight.SentenceFragmenter方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_highlight_ngrams
# 需要导入模块: from whoosh import highlight [as 别名]
# 或者: from whoosh.highlight import SentenceFragmenter [as 别名]
def test_highlight_ngrams():
schema = fields.Schema(text=fields.NGRAMWORDS(stored=True))
ix = RamStorage().create_index(schema)
with ix.writer() as w:
w.add_document(text=u("Multiplication and subtraction are good"))
with ix.searcher() as s:
qp = qparser.QueryParser("text", ix.schema)
q = qp.parse(u("multiplication"))
r = s.search(q)
assert r.scored_length() == 1
r.fragmenter = highlight.SentenceFragmenter()
r.formatter = highlight.UppercaseFormatter()
snippet = r[0].highlights("text")
assert snippet == "MULTIPLICATIon and subtracTION are good"
示例2: test_snippets
# 需要导入模块: from whoosh import highlight [as 别名]
# 或者: from whoosh.highlight import SentenceFragmenter [as 别名]
def test_snippets():
ana = analysis.StemmingAnalyzer()
schema = fields.Schema(text=fields.TEXT(stored=True, analyzer=ana))
ix = RamStorage().create_index(schema)
w = ix.writer()
w.add_document(text=u("Lay out the rough animation by creating the important poses where they occur on the timeline."))
w.add_document(text=u("Set key frames on everything that's key-able. This is for control and predictability: you don't want to accidentally leave something un-keyed. This is also much faster than selecting the parameters to key."))
w.add_document(text=u("Use constant (straight) or sometimes linear transitions between keyframes in the channel editor. This makes the character jump between poses."))
w.add_document(text=u("Keying everything gives quick, immediate results. But it can become difficult to tweak the animation later, especially for complex characters."))
w.add_document(text=u("Copy the current pose to create the next one: pose the character, key everything, then copy the keyframe in the playbar to another frame, and key everything at that frame."))
w.commit()
target = ["Set KEY frames on everything that's KEY-able",
"Copy the current pose to create the next one: pose the character, KEY everything, then copy the keyframe in the playbar to another frame, and KEY everything at that frame",
"KEYING everything gives quick, immediate results"]
with ix.searcher() as s:
qp = qparser.QueryParser("text", ix.schema)
q = qp.parse(u("key"))
r = s.search(q, terms=True)
r.fragmenter = highlight.SentenceFragmenter()
r.formatter = highlight.UppercaseFormatter()
assert sorted([hit.highlights("text", top=1) for hit in r]) == sorted(target)
示例3: search
# 需要导入模块: from whoosh import highlight [as 别名]
# 或者: from whoosh.highlight import SentenceFragmenter [as 别名]
def search(ix, text):
with ix.searcher() as searcher:
qp = QueryParser("body", schema=ix.schema)
q = qp.parse(text)
search_hits = searcher.search(q, limit=None, terms=True)
search_hits.fragmenter = highlight.SentenceFragmenter(maxchars=450)
res = []
for hit in search_hits:
tmp = dict(hit)
tmp['matching_terms'] = [x[1] for x in hit.matched_terms()]
tmp['highlights'] = hit.highlights("body")
res.append(tmp)
return {"hits": res, "matched_terms": [x[1] for x in search_hits.matched_terms()]}
示例4: highlights
# 需要导入模块: from whoosh import highlight [as 别名]
# 或者: from whoosh.highlight import SentenceFragmenter [as 别名]
def highlights(self, fieldname, text=None, top=3, minscore=1):
"""Returns highlighted snippets from the given field::
r = searcher.search(myquery)
for hit in r:
print(hit["title"])
print(hit.highlights("content"))
See :doc:`/highlight`.
To change the fragmeter, formatter, order, or scorer used in
highlighting, you can set attributes on the results object::
from whoosh import highlight
results = searcher.search(myquery, terms=True)
results.fragmenter = highlight.SentenceFragmenter()
...or use a custom :class:`whoosh.highlight.Highlighter` object::
hl = highlight.Highlighter(fragmenter=sf)
results.highlighter = hl
:param fieldname: the name of the field you want to highlight.
:param text: by default, the method will attempt to load the contents
of the field from the stored fields for the document. If the field
you want to highlight isn't stored in the index, but you have
access to the text another way (for example, loading from a file or
a database), you can supply it using the ``text`` parameter.
:param top: the maximum number of fragments to return.
:param minscore: the minimum score for fragments to appear in the
highlights.
"""
hliter = self.results.highlighter
return hliter.highlight_hit(self, fieldname, text=text, top=top,
minscore=minscore)
示例5: test_sentence_fragment
# 需要导入模块: from whoosh import highlight [as 别名]
# 或者: from whoosh.highlight import SentenceFragmenter [as 别名]
def test_sentence_fragment():
text = u("This is the first sentence. This one doesn't have the word. " +
"This sentence is the second. Third sentence here.")
terms = ("sentence",)
sa = analysis.StandardAnalyzer(stoplist=None)
sf = highlight.SentenceFragmenter()
uc = highlight.UppercaseFormatter()
htext = highlight.highlight(text, terms, sa, sf, uc)
assert htext == "This is the first SENTENCE...This SENTENCE is the second...Third SENTENCE here"