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 unittest import TestCase
18 IndexWriter, Term, RAMDirectory, Document, Field, \
19 IndexSearcher, QueryParser, Version
21 from lia.analysis.AnalyzerUtils import AnalyzerUtils
22 from lia.analysis.positional.PositionalPorterStopAnalyzer import \
23 PositionalPorterStopAnalyzer
26 class PositionalPorterStopAnalyzerTest(TestCase):
28 porterAnalyzer = PositionalPorterStopAnalyzer()
32 self.directory = RAMDirectory()
33 writer = IndexWriter(self.directory, self.porterAnalyzer, True,
34 IndexWriter.MaxFieldLength.UNLIMITED)
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)
45 searcher = IndexSearcher(self.directory)
46 query = QueryParser(Version.LUCENE_CURRENT, "contents",
47 self.porterAnalyzer).parse("laziness")
48 topDocs = searcher.search(query, 50)
50 self.assertEqual(1, topDocs.totalHits, "lazi")
52 query = QueryParser(Version.LUCENE_CURRENT, "contents",
53 self.porterAnalyzer).parse('"fox jumped"')
54 topDocs = searcher.search(query, 50)
56 self.assertEqual(1, topDocs.totalHits, "jump jumps jumped jumping")
58 def testExactPhrase(self):
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)
65 self.assertEqual(0, topDocs.totalHits, "exact match not found!")
67 def testWithSlop(self):
69 searcher = IndexSearcher(self.directory, True)
71 parser = QueryParser(Version.LUCENE_CURRENT, "contents",
73 parser.setPhraseSlop(1)
75 query = parser.parse('"over the lazy"')
76 topDocs = searcher.search(query, 50)
78 self.assertEqual(1, topDocs.totalHits, "hole accounted for")
82 text = "The quick brown fox jumps over the lazy dogs"
83 AnalyzerUtils.displayTokensWithPositions(cls.porterAnalyzer, text)
86 main = classmethod(main)