pylucene 3.5.0-3
[pylucene.git] / test / test_ReusableAnalyzerBase.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 unittest import TestCase, main
16 from lucene import *
17
18
19 # Test reusableTokenStream, using ReusableAnalyzerBase:
20 class MyAnalyzer(PythonReusableAnalyzerBase):
21
22     def initReader(self, reader):
23         return reader
24
25     def createComponents(self, field, reader):
26
27         first = LowerCaseTokenizer(Version.LUCENE_CURRENT, reader)
28         last = StopFilter(Version.LUCENE_CURRENT, first,
29                           StopAnalyzer.ENGLISH_STOP_WORDS_SET)
30
31         return ReusableAnalyzerBase.TokenStreamComponents(first, last)
32
33
34 class ReusableAnalyzerBaseTestCase(TestCase):
35
36     def testReusable(self):
37
38         analyzer = MyAnalyzer()
39
40         for method in (analyzer.reusableTokenStream, analyzer.tokenStream):
41             for x in xrange(2):
42                 reader = StringReader("This is a test of the english stop analyzer")
43                 stream = method("test", reader)
44
45                 termAtt = stream.getAttribute(TermAttribute.class_)
46                 count = 0
47                 while stream.incrementToken():
48                     self.assert_(termAtt.term() not in StopAnalyzer.ENGLISH_STOP_WORDS_SET)
49                     count += 1
50                 self.assertEquals(4, count)
51
52
53 if __name__ == "__main__":
54     import sys, lucene
55     lucene.initVM()
56     if '-loop' in sys.argv:
57         sys.argv.remove('-loop')
58         while True:
59             try:
60                 main()
61             except:
62                 pass
63     else:
64          main()