1 # -*- coding: utf-8 -*-
3 from __future__
import absolute_import
6 from jbparse
import kanjidic2
8 SRC_NAME
= "kanjidic2.xml"
9 SRC_DIR
= "../../dicts"
11 SRC_NAME
= "/".join((SRC_DIR
, SRC_NAME
))
13 class Kanjidic2Test(unittest
.TestCase
):
16 self
.parser
= kanjidic2
.Kanjidic2Parser(SRC_NAME
)
18 def test_single_kanji_search(self
):
19 """KANJIDIC2: Search for single kanji"""
21 l
= [entry
for entry
in self
.parser
.search(query
)]
22 self
.assertEqual(len(l
), 1)
23 self
.assertEqual(query
, l
[0].literal
)
25 def test_multi_kanji_search(self
):
26 """KANJIDIC2: Search for multiple kanji at once"""
28 l
= [entry
for entry
in self
.parser
.search(query
)]
29 self
.assertEqual(len(l
), 2)
31 self
.assertTrue(char
in [entry
.literal
for entry
in l
])
33 def test_mixed_search(self
):
34 """KANJIDIC2: Search query with kanji and non-kanji characters"""
35 query
= u
"天気はいいから、散歩しましょう。 Right?"
36 l
= [entry
for entry
in self
.parser
.search(query
)]
37 self
.assertEqual(len(l
), 4)
39 self
.assertTrue(char
in [entry
.literal
for entry
in l
])
41 def test_caching(self
):
42 """KANJIDIC2: Check that caching is working"""
43 self
.assertFalse(self
.parser
.cache
)
45 self
.test_single_kanji_search()
46 first_t
= time
.time() - t
48 self
.assertTrue(self
.parser
.cache
)
50 self
.test_single_kanji_search()
51 second_t
= time
.time() - t
53 print "\n\tFirst query time: %f" % first_t
54 print "\tSecond query time: %f" % second_t
55 self
.assertTrue(second_t
<= first_t
)
57 def test_no_cache(self
):
58 """KANJIDIC2: Check that parser works without caching."""
59 self
.parser
= kanjidic2
.Kanjidic2Parser(SRC_NAME
, use_cache
=False)
61 self
.assertFalse(self
.parser
.cache
)
63 self
.test_single_kanji_search()
64 first_t
= time
.time() - t
66 self
.assertFalse(self
.parser
.cache
)
68 self
.test_single_kanji_search()
69 second_t
= time
.time() - t
71 print "\n\tFirst query time: %f" % first_t
72 print "\tSecond query time: %f" % second_t
77 if __name__
== "__main__":