root/projects/whoosh/trunk/tests/test_highlighting.py @ 400

Revision 400, 2.8 KB (checked in by matt, 8 months ago)

Added ability to limit number of term classes generated by highlight.HtmlFormatter?.
Bumped revision.

Line 
1import unittest
2
3import whoosh.analysis as analysis
4import whoosh.highlight as highlight
5
6
7class TestHighlighting(unittest.TestCase):
8    _doc = u"alfa bravo charlie delta echo foxtrot golf hotel india juliet kilo lima"
9   
10    def test_null_fragment(self):
11        terms = frozenset(("bravo", "india"))
12        sa = analysis.StandardAnalyzer()
13        nf = highlight.NullFragmenter
14        uc = highlight.UppercaseFormatter()
15        htext = highlight.highlight(self._doc, terms, sa, nf, uc)
16        self.assertEqual(htext, "alfa BRAVO charlie delta echo foxtrot golf hotel INDIA juliet kilo lima")
17
18    def test_simple_fragment(self):
19        terms = frozenset(("bravo", "india"))
20        sa = analysis.StandardAnalyzer()
21        sf = highlight.SimpleFragmenter(size=20)
22        uc = highlight.UppercaseFormatter()
23        htext = highlight.highlight(self._doc, terms, sa, sf, uc)
24        self.assertEqual(htext, "alfa BRAVO charlie...hotel INDIA juliet kilo")
25       
26    def test_sentence_fragment(self):
27        text = u"This is the first sentence. This one doesn't have the word. This sentence is the second. Third sentence here."
28        terms = ("sentence", )
29        sa = analysis.StandardAnalyzer(stoplist=None)
30        sf = highlight.SentenceFragmenter()
31        uc = highlight.UppercaseFormatter()
32        htext = highlight.highlight(text, terms, sa, sf, uc)
33        self.assertEqual(htext, "This is the first SENTENCE...This SENTENCE is the second...Third SENTENCE here")
34
35    def test_context_fragment(self):
36        terms = frozenset(("bravo", "india"))
37        sa = analysis.StandardAnalyzer()
38        cf = highlight.ContextFragmenter(terms, surround=6)
39        uc = highlight.UppercaseFormatter()
40        htext = highlight.highlight(self._doc, terms, sa, cf, uc)
41        self.assertEqual(htext, "alfa BRAVO charlie...hotel INDIA juliet")
42   
43    def test_html_format(self):
44        terms = frozenset(("bravo", "india"))
45        sa = analysis.StandardAnalyzer()
46        cf = highlight.ContextFragmenter(terms, surround=6)
47        hf = highlight.HtmlFormatter()
48        htext = highlight.highlight(self._doc, terms, sa, cf, hf)
49        self.assertEqual(htext, 'alfa <strong class="match term0">bravo</strong> charlie...hotel <strong class="match term1">india</strong> juliet')
50
51    def test_maxclasses(self):
52        terms = frozenset(("alfa", "bravo", "charlie", "delta", "echo"))
53        sa = analysis.StandardAnalyzer()
54        cf = highlight.ContextFragmenter(terms, surround=6)
55        hf = highlight.HtmlFormatter(tagname="b", termclass="t", maxclasses=2)
56        htext = highlight.highlight(self._doc, terms, sa, cf, hf)
57        self.assertEqual(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')
58
59
60
61if __name__ == '__main__':
62    unittest.main()
Note: See TracBrowser for help on using the browser.