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, SimpleAnalyzer, PerFieldAnalyzerWrapper, \
19 RAMDirectory, Document, Field, IndexSearcher, TermQuery, \
20 QueryParser, Analyzer, StringReader, Token, JavaError, \
23 from lia.analysis.keyword.KeywordAnalyzer import KeywordAnalyzer
24 from lia.analysis.keyword.SimpleKeywordAnalyzer import SimpleKeywordAnalyzer
27 class KeywordAnalyzerTest(TestCase):
31 self.directory = RAMDirectory()
32 writer = IndexWriter(self.directory, SimpleAnalyzer(), True,
33 IndexWriter.MaxFieldLength.UNLIMITED)
36 doc.add(Field("partnum", "Q36",
37 Field.Store.YES, Field.Index.NOT_ANALYZED))
38 doc.add(Field("description", "Illidium Space Modulator",
39 Field.Store.YES, Field.Index.ANALYZED))
40 writer.addDocument(doc)
43 self.searcher = IndexSearcher(self.directory, True)
45 def testTermQuery(self):
47 query = TermQuery(Term("partnum", "Q36"))
48 scoreDocs = self.searcher.search(query, 50).scoreDocs
49 self.assertEqual(1, len(scoreDocs))
51 def testBasicQueryParser(self):
53 analyzer = SimpleAnalyzer()
54 query = QueryParser(Version.LUCENE_CURRENT, "description",
55 analyzer).parse("partnum:Q36 AND SPACE")
57 scoreDocs = self.searcher.search(query, 50).scoreDocs
58 self.assertEqual("+partnum:q +space", query.toString("description"),
60 self.assertEqual(0, len(scoreDocs), "doc not found :(")
62 def testPerFieldAnalyzer(self):
64 analyzer = PerFieldAnalyzerWrapper(SimpleAnalyzer())
65 analyzer.addAnalyzer("partnum", KeywordAnalyzer())
67 query = QueryParser(Version.LUCENE_CURRENT, "description",
68 analyzer).parse("partnum:Q36 AND SPACE")
69 scoreDocs = self.searcher.search(query, 50).scoreDocs
71 #self.assertEqual("+partnum:Q36 +space", query.toString("description"))
72 self.assertEqual(1, len(scoreDocs), "doc found!")