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
6 # http://www.apache.org/licenses/LICENSE-2.0
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 # ====================================================================
15 from sys import stdout
18 StandardAnalyzer, Term, TermQuery, StringReader, Version, \
19 Fragmenter, Highlighter, QueryScorer, SimpleFragmenter, SimpleHTMLFormatter
22 class HighlightIt(object):
24 # from http://www.lipsum.com
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
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)
54 analyzer = StandardAnalyzer(Version.LUCENE_CURRENT)
55 tokenStream = analyzer.tokenStream("f", StringReader(cls.text))
56 result = highlighter.getBestFragments(tokenStream, cls.text, 5, "...")
58 stdout.write("<html>")
59 stdout.write("<style>\n")
60 stdout.write(".highlight {\n")
61 stdout.write(" background: yellow\n")
63 stdout.write("</style>")
65 stdout.write("<body>")
67 stdout.write("</body></html>\n")
70 main = classmethod(main)