當前位置: 首頁>>代碼示例>>Python>>正文


Python highlight.highlight方法代碼示例

本文整理匯總了Python中whoosh.highlight.highlight方法的典型用法代碼示例。如果您正苦於以下問題:Python highlight.highlight方法的具體用法?Python highlight.highlight怎麽用?Python highlight.highlight使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在whoosh.highlight的用法示例。


在下文中一共展示了highlight.highlight方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_workflow_easy

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_workflow_easy():
    schema = fields.Schema(id=fields.ID(stored=True),
                           title=fields.TEXT(stored=True))
    ix = RamStorage().create_index(schema)

    w = ix.writer()
    w.add_document(id=u("1"), title=u("The man who wasn't there"))
    w.add_document(id=u("2"), title=u("The dog who barked at midnight"))
    w.add_document(id=u("3"), title=u("The invisible man"))
    w.add_document(id=u("4"), title=u("The girl with the dragon tattoo"))
    w.add_document(id=u("5"), title=u("The woman who disappeared"))
    w.commit()

    with ix.searcher() as s:
        # Parse the user query
        parser = qparser.QueryParser("title", schema=ix.schema)
        q = parser.parse(u("man"))
        r = s.search(q, terms=True)
        assert len(r) == 2

        r.fragmenter = highlight.WholeFragmenter()
        r.formatter = highlight.UppercaseFormatter()
        outputs = [hit.highlights("title") for hit in r]
        assert outputs == ["The invisible MAN", "The MAN who wasn't there"] 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:26,代碼來源:test_highlighting.py

示例2: test_pinpoint

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_pinpoint():
    domain = u("alfa bravo charlie delta echo foxtrot golf hotel india juliet "
               "kilo lima mike november oskar papa quebec romeo sierra tango")
    schema = fields.Schema(text=fields.TEXT(stored=True, chars=True))
    ix = RamStorage().create_index(schema)
    w = ix.writer()
    w.add_document(text=domain)
    w.commit()

    assert ix.schema["text"].supports("characters")
    with ix.searcher() as s:
        r = s.search(query.Term("text", "juliet"), terms=True)
        hit = r[0]
        hi = highlight.Highlighter()
        hi.formatter = highlight.UppercaseFormatter()

        assert not hi.can_load_chars(r, "text")
        assert hi.highlight_hit(hit, "text") == "golf hotel india JULIET kilo lima mike november"

        hi.fragmenter = highlight.PinpointFragmenter()
        assert hi.can_load_chars(r, "text")
        assert hi.highlight_hit(hit, "text") == "ot golf hotel india JULIET kilo lima mike nove"

        hi.fragmenter.autotrim = True
        assert hi.highlight_hit(hit, "text") == "golf hotel india JULIET kilo lima mike" 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:27,代碼來源:test_highlighting.py

示例3: test_whole_noterms

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_whole_noterms():
    schema = fields.Schema(text=fields.TEXT(stored=True), tag=fields.KEYWORD)
    ix = RamStorage().create_index(schema)
    with ix.writer() as w:
        w.add_document(text=u("alfa bravo charlie delta echo foxtrot golf"),
                       tag=u("foo"))

    with ix.searcher() as s:
        r = s.search(query.Term("text", u("delta")))
        assert len(r) == 1

        r.fragmenter = highlight.WholeFragmenter()
        r.formatter = highlight.UppercaseFormatter()
        hi = r[0].highlights("text")
        assert hi == u("alfa bravo charlie DELTA echo foxtrot golf")

        r = s.search(query.Term("tag", u("foo")))
        assert len(r) == 1
        r.fragmenter = highlight.WholeFragmenter()
        r.formatter = highlight.UppercaseFormatter()
        hi = r[0].highlights("text")
        assert hi == u("")

        hi = r[0].highlights("text", minscore=0)
        assert hi == u("alfa bravo charlie delta echo foxtrot golf") 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:27,代碼來源:test_highlighting.py

示例4: test_issue324

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_issue324():
    sa = analysis.StemmingAnalyzer()
    result = highlight.highlight(u("Indexed!\n1"), [u("index")], sa,
                                 fragmenter=highlight.ContextFragmenter(),
                                 formatter=highlight.UppercaseFormatter())
    assert result == "INDEXED!\n1" 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:8,代碼來源:test_highlighting.py

示例5: test_null_fragment

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_null_fragment():
    terms = frozenset(("bravo", "india"))
    sa = analysis.StandardAnalyzer()
    nf = highlight.WholeFragmenter()
    uc = highlight.UppercaseFormatter()
    htext = highlight.highlight(_doc, terms, sa, nf, uc)
    assert htext == "alfa BRAVO charlie delta echo foxtrot golf hotel INDIA juliet kilo lima" 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:9,代碼來源:test_highlighting.py

示例6: test_sentence_fragment

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [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" 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:11,代碼來源:test_highlighting.py

示例7: test_context_fragment

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_context_fragment():
    terms = frozenset(("bravo", "india"))
    sa = analysis.StandardAnalyzer()
    cf = highlight.ContextFragmenter(surround=6)
    uc = highlight.UppercaseFormatter()
    htext = highlight.highlight(_doc, terms, sa, cf, uc)
    assert htext == "alfa BRAVO charlie...hotel INDIA juliet" 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:9,代碼來源:test_highlighting.py

示例8: test_context_at_start

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_context_at_start():
    terms = frozenset(["alfa"])
    sa = analysis.StandardAnalyzer()
    cf = highlight.ContextFragmenter(surround=15)
    uc = highlight.UppercaseFormatter()
    htext = highlight.highlight(_doc, terms, sa, cf, uc)
    assert htext == "ALFA bravo charlie delta echo foxtrot" 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:9,代碼來源:test_highlighting.py

示例9: test_html_format

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_html_format():
    terms = frozenset(("bravo", "india"))
    sa = analysis.StandardAnalyzer()
    cf = highlight.ContextFragmenter(surround=6)
    hf = highlight.HtmlFormatter()
    htext = highlight.highlight(_doc, terms, sa, cf, hf)
    assert htext == 'alfa <strong class="match term0">bravo</strong> charlie...hotel <strong class="match term1">india</strong> juliet' 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:9,代碼來源:test_highlighting.py

示例10: test_maxclasses

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_maxclasses():
    terms = frozenset(("alfa", "bravo", "charlie", "delta", "echo"))
    sa = analysis.StandardAnalyzer()
    cf = highlight.ContextFragmenter(surround=6)
    hf = highlight.HtmlFormatter(tagname="b", termclass="t", maxclasses=2)
    htext = highlight.highlight(_doc, terms, sa, cf, hf)
    assert htext == '<b class="match t0">alfa</b> <b class="match t1">bravo</b> <b class="match t0">charlie</b>...<b class="match t1">delta</b> <b class="match t0">echo</b> foxtrot' 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:9,代碼來源:test_highlighting.py

示例11: test_highlight_wildcards

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_highlight_wildcards():
    schema = fields.Schema(text=fields.TEXT(stored=True))
    ix = RamStorage().create_index(schema)
    with ix.writer() as w:
        w.add_document(text=u("alfa bravo charlie delta cookie echo"))

    with ix.searcher() as s:
        qp = qparser.QueryParser("text", ix.schema)
        q = qp.parse(u("c*"))
        r = s.search(q)
        assert r.scored_length() == 1
        r.formatter = highlight.UppercaseFormatter()
        hit = r[0]
        assert hit.highlights("text") == "alfa bravo CHARLIE delta COOKIE echo" 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:16,代碼來源:test_highlighting.py

示例12: highlight

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def highlight(self, text, words):
        fragmenter = ContextFragmenter()
        formatter = HtmlFormatter()
        analyzer = self.project_schema['text'].analyzer
        return highlight(text, words, analyzer, fragmenter, formatter, top=1) 
開發者ID:devpi,項目名稱:devpi,代碼行數:7,代碼來源:whoosh_index.py

示例13: test_workflow_manual

# 需要導入模塊: from whoosh import highlight [as 別名]
# 或者: from whoosh.highlight import highlight [as 別名]
def test_workflow_manual():
    schema = fields.Schema(id=fields.ID(stored=True),
                           title=fields.TEXT(stored=True))
    ix = RamStorage().create_index(schema)

    w = ix.writer()
    w.add_document(id=u("1"), title=u("The man who wasn't there"))
    w.add_document(id=u("2"), title=u("The dog who barked at midnight"))
    w.add_document(id=u("3"), title=u("The invisible man"))
    w.add_document(id=u("4"), title=u("The girl with the dragon tattoo"))
    w.add_document(id=u("5"), title=u("The woman who disappeared"))
    w.commit()

    with ix.searcher() as s:
        # Parse the user query
        parser = qparser.QueryParser("title", schema=ix.schema)
        q = parser.parse(u("man"))

        # Extract the terms the user used in the field we're interested in
        terms = [text for fieldname, text in q.all_terms()
                 if fieldname == "title"]

        # Perform the search
        r = s.search(q)
        assert len(r) == 2

        # Use the same analyzer as the field uses. To be sure, you can
        # do schema[fieldname].analyzer. Be careful not to do this
        # on non-text field types such as DATETIME.
        analyzer = schema["title"].analyzer

        # Since we want to highlight the full title, not extract fragments,
        # we'll use WholeFragmenter.
        nf = highlight.WholeFragmenter()

        # In this example we'll simply uppercase the matched terms
        fmt = highlight.UppercaseFormatter()

        outputs = []
        for d in r:
            text = d["title"]
            outputs.append(highlight.highlight(text, terms, analyzer, nf, fmt))

        assert outputs == ["The invisible MAN", "The MAN who wasn't there"] 
開發者ID:securesystemslab,項目名稱:zippy,代碼行數:46,代碼來源:test_highlighting.py


注:本文中的whoosh.highlight.highlight方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。