PyLucene 3.4.0-1 import
[pylucene.git] / test / test_RegexQuery.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 class TestRegexQuery(TestCase):
20
21     FN = "field"
22
23     def setUp(self):
24
25         directory = RAMDirectory()
26
27         writer = IndexWriter(directory, SimpleAnalyzer(), True,
28                              IndexWriter.MaxFieldLength.LIMITED)
29         doc = Document()
30         doc.add(Field(self.FN, "the quick brown fox jumps over the lazy dog", Field.Store.NO, Field.Index.ANALYZED))
31         writer.addDocument(doc)
32         writer.optimize()
33         writer.close()
34         self.searcher = IndexSearcher(directory, True)
35
36     def tearDown(self):
37
38         self.searcher.close()
39
40     def newTerm(self, value):
41   
42         return Term(self.FN, value)
43
44     def regexQueryNrHits(self, regex):
45
46         query = RegexQuery(self.newTerm(regex))
47
48         return self.searcher.search(query, 50).totalHits
49
50     def spanRegexQueryNrHits(self, regex1, regex2, slop, ordered):
51
52         srq1 = SpanRegexQuery(self.newTerm(regex1))
53         srq2 = SpanRegexQuery(self.newTerm(regex2))
54         query = SpanNearQuery([srq1, srq2], slop, ordered)
55
56         return self.searcher.search(query, 50).totalHits
57
58     def testRegex1(self):
59
60         self.assertEqual(1, self.regexQueryNrHits("^q.[aeiou]c.*$"))
61
62     def testRegex2(self):
63
64         self.assertEqual(0, self.regexQueryNrHits("^.[aeiou]c.*$"))
65
66     def testRegex3(self):
67
68         self.assertEqual(0, self.regexQueryNrHits("^q.[aeiou]c$"))
69
70     def testSpanRegex1(self):
71
72         self.assertEqual(1, self.spanRegexQueryNrHits("^q.[aeiou]c.*$",
73                                                       "dog", 6, True))
74
75     def testSpanRegex2(self):
76
77         self.assertEqual(0, self.spanRegexQueryNrHits("^q.[aeiou]c.*$",
78                                                       "dog", 5, True))
79
80
81 if __name__ == "__main__":
82     import sys, lucene
83     lucene.initVM()
84     if '-loop' in sys.argv:
85         sys.argv.remove('-loop')
86         while True:
87             try:
88                 main()
89             except:
90                 pass
91     else:
92         main()