PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / analysis / positional / PositionalPorterStopAnalyzerTest.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
16
17 from lucene import \
18      IndexWriter, Term, RAMDirectory, Document, Field, \
19      IndexSearcher, QueryParser, Version
20
21 from lia.analysis.AnalyzerUtils import AnalyzerUtils
22 from lia.analysis.positional.PositionalPorterStopAnalyzer import \
23      PositionalPorterStopAnalyzer
24
25
26 class PositionalPorterStopAnalyzerTest(TestCase):
27
28     porterAnalyzer = PositionalPorterStopAnalyzer()
29     
30     def setUp(self):
31
32         self.directory = RAMDirectory()
33         writer = IndexWriter(self.directory, self.porterAnalyzer, True,
34                              IndexWriter.MaxFieldLength.UNLIMITED)
35
36         doc = Document()
37         doc.add(Field("contents",
38                       "The quick brown fox jumps over the lazy dogs",
39                        Field.Store.YES, Field.Index.ANALYZED))
40         writer.addDocument(doc)
41         writer.close()
42
43     def testStems(self):
44         
45         searcher = IndexSearcher(self.directory)
46         query = QueryParser(Version.LUCENE_CURRENT, "contents",
47                             self.porterAnalyzer).parse("laziness")
48         topDocs = searcher.search(query, 50)
49
50         self.assertEqual(1, topDocs.totalHits, "lazi")
51
52         query = QueryParser(Version.LUCENE_CURRENT, "contents",
53                             self.porterAnalyzer).parse('"fox jumped"')
54         topDocs = searcher.search(query, 50)
55
56         self.assertEqual(1, topDocs.totalHits, "jump jumps jumped jumping")
57
58     def testExactPhrase(self):
59
60         searcher = IndexSearcher(self.directory, True)
61         query = QueryParser(Version.LUCENE_24, "contents",
62                             self.porterAnalyzer).parse('"over the lazy"')
63         topDocs = searcher.search(query, 50)
64
65         self.assertEqual(0, topDocs.totalHits, "exact match not found!")
66
67     def testWithSlop(self):
68
69         searcher = IndexSearcher(self.directory, True)
70
71         parser = QueryParser(Version.LUCENE_CURRENT, "contents",
72                              self.porterAnalyzer)
73         parser.setPhraseSlop(1)
74
75         query = parser.parse('"over the lazy"')
76         topDocs = searcher.search(query, 50)
77
78         self.assertEqual(1, topDocs.totalHits, "hole accounted for")
79
80     def main(cls):
81
82         text = "The quick brown fox jumps over the lazy dogs"
83         AnalyzerUtils.displayTokensWithPositions(cls.porterAnalyzer, text)
84         print ''
85         
86     main = classmethod(main)