PyLucene 3.4.0-1 import
[pylucene.git] / samples / LuceneInAction / lia / analysis / keyword / KeywordAnalyzerTest.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, SimpleAnalyzer, PerFieldAnalyzerWrapper, \
19      RAMDirectory, Document, Field, IndexSearcher, TermQuery, \
20      QueryParser, Analyzer, StringReader, Token, JavaError, \
21      Version
22
23 from lia.analysis.keyword.KeywordAnalyzer import KeywordAnalyzer
24 from lia.analysis.keyword.SimpleKeywordAnalyzer import SimpleKeywordAnalyzer
25
26
27 class KeywordAnalyzerTest(TestCase):
28
29     def setUp(self):
30
31         self.directory = RAMDirectory()
32         writer = IndexWriter(self.directory, SimpleAnalyzer(), True,
33                              IndexWriter.MaxFieldLength.UNLIMITED)
34
35         doc = Document()
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)
41         writer.close()
42
43         self.searcher = IndexSearcher(self.directory, True)
44
45     def testTermQuery(self):
46
47         query = TermQuery(Term("partnum", "Q36"))
48         scoreDocs = self.searcher.search(query, 50).scoreDocs
49         self.assertEqual(1, len(scoreDocs))
50
51     def testBasicQueryParser(self):
52
53         analyzer = SimpleAnalyzer()
54         query = QueryParser(Version.LUCENE_CURRENT, "description",
55                             analyzer).parse("partnum:Q36 AND SPACE")
56
57         scoreDocs = self.searcher.search(query, 50).scoreDocs
58         self.assertEqual("+partnum:q +space", query.toString("description"),
59                          "note Q36 -> q")
60         self.assertEqual(0, len(scoreDocs), "doc not found :(")
61
62     def testPerFieldAnalyzer(self):
63
64         analyzer = PerFieldAnalyzerWrapper(SimpleAnalyzer())
65         analyzer.addAnalyzer("partnum", KeywordAnalyzer())
66
67         query = QueryParser(Version.LUCENE_CURRENT, "description",
68                             analyzer).parse("partnum:Q36 AND SPACE")
69         scoreDocs = self.searcher.search(query, 50).scoreDocs
70
71         #self.assertEqual("+partnum:Q36 +space", query.toString("description"))
72         self.assertEqual(1, len(scoreDocs), "doc found!")