PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / tools / HighlightIt.py
1 # ====================================================================
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13 # ====================================================================
14
15 from sys import stdout
16
17 from lucene import \
18      StandardAnalyzer, Term, TermQuery, StringReader, Version, \
19      Fragmenter, Highlighter, QueryScorer, SimpleFragmenter, SimpleHTMLFormatter
20
21
22 class HighlightIt(object):
23
24     # from http://www.lipsum.com
25     text = \
26       """
27       Contrary to popular belief, Lorem Ipsum is
28       not simply random text. It has roots in a piece of
29       classical Latin literature from 45 BC, making it over
30       2000 years old. Richard McClintock, a Latin professor
31       at Hampden-Sydney College in Virginia, looked up one
32       of the more obscure Latin words, consectetur, from
33       a Lorem Ipsum passage, and going through the cites
34       of the word in classical literature, discovered the
35       undoubtable source. Lorem Ipsum comes from sections
36       1.10.32 and 1.10.33 of "de Finibus Bonorum et
37       Malorum" (The Extremes of Good and Evil) by Cicero,
38       written in 45 BC. This book is a treatise on the
39       theory of ethics, very popular during the
40       Renaissance. The first line of Lorem Ipsum, "Lorem
41       ipsum dolor sit amet..", comes from a line in
42       section 1.10.32.
43       """
44
45     def main(cls, argv):
46
47         query = TermQuery(Term("f", "ipsum"))
48         scorer = QueryScorer(query)
49         formatter = SimpleHTMLFormatter("<span class=\"highlight\">", "</span>")
50         highlighter = Highlighter(formatter, scorer)
51         fragmenter = SimpleFragmenter(50)
52         highlighter.setTextFragmenter(fragmenter)
53
54         analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
55         tokenStream = analyzer.tokenStream("f", StringReader(cls.text))
56         result = highlighter.getBestFragments(tokenStream, cls.text, 5, "...")
57
58         stdout.write("<html>")
59         stdout.write("<style>\n")
60         stdout.write(".highlight {\n")
61         stdout.write(" background: yellow\n")
62         stdout.write("}\n")
63         stdout.write("</style>")
64
65         stdout.write("<body>")
66         stdout.write(result)
67         stdout.write("</body></html>\n")
68         stdout.flush()
69         
70     main = classmethod(main)