PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / analysis / AnalyzerDemo.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
16 from lia.analysis.AnalyzerUtils import AnalyzerUtils
17 from lucene import Version, \
18      StopAnalyzer, SimpleAnalyzer, WhitespaceAnalyzer, StandardAnalyzer
19
20
21 class AnalyzerDemo(object):
22
23     examples = ["The quick brown fox jumped over the lazy dogs",
24                 "XY&Z Corporation - xyz@example.com"]
25     
26     analyzers = [WhitespaceAnalyzer(),
27                  SimpleAnalyzer(),
28                  StopAnalyzer(Version.LUCENE_CURRENT),
29                  StandardAnalyzer(Version.LUCENE_CURRENT)]
30
31     def main(cls, argv):
32
33         # Use the embedded example strings, unless
34         # command line arguments are specified, then use those.
35         strings = cls.examples
36
37         if len(argv) > 1:
38             strings = argv[1:]
39
40         for string in strings:
41             cls.analyze(string)
42
43     def analyze(cls, text):
44
45         print 'Analyzing "%s"' %(text)
46
47         for analyzer in cls.analyzers:
48             name = type(analyzer).__name__
49             print " %s:" %(name),
50             AnalyzerUtils.displayTokens(analyzer, text)
51             print
52         print
53
54     main = classmethod(main)
55     analyze = classmethod(analyze)
56
57
58 if __name__ == "__main__":
59     import sys
60     AnalyzerDemo.main(sys.argv)