forked from ikegami-yukino/jaconv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_jaconv.py
146 lines (119 loc) · 7.33 KB
/
test_jaconv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from nose.tools import assert_equal, nottest
import jaconv
from functools import partial
assert_equal.__self__.maxDiff = None
HIRAGANA = ('ぁあぃいぅうぇえぉおかがきぎくぐけげこごさざしじすずせぜそぞた',
'だちぢっつづてでとどなにぬねのはばぱひびぴふぶぷへべぺほぼぽま',
'みむめもゃやゅゆょよらりるれろわをんーゎゐゑゕゖゔゝゞ・「」。、')
FULL_KANA = ('ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタ',
'ダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマ',
'ミムメモャヤュユョヨラリルレロワヲンーヮヰヱヵヶヴヽヾ・「」。、')
HALF_KANA = ('ァアィイゥウェエォオカガキギクグケゲコゴサザシジスズセゼソゾタ',
'ダチヂッツヅテデトドナニヌネノハバパヒビピフブプヘベペホボポマ',
'ミムメモャヤュユョヨラリルレロワヲンーヮヰヱヵヶヴヽヾ・「」。、')
HALF_ASCII = ('!"#$%&\'()*+,-./:;<=>?@[\\]^_`~',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz{|} ')
HALF_DIGIT = '0123456789'
FULL_ASCII = ('!"#$%&'()*+,-./:;<=>?@[\]^_`~',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'abcdefghijklmnopqrstuvwxyz{|} ')
FULL_DIGIT = '0123456789'
@nottest
def _compare(mathod, lhs, rhs):
for i in range(len(lhs)):
assert_equal(mathod(lhs[i]), rhs[i])
@nottest
def _concat(*iterables):
result = ''
for iterable in iterables:
result += ''.join(iterable)
return result
def test_hira2kata():
assert_equal(jaconv.hira2kata('ともえまみ'), 'トモエマミ')
assert_equal(jaconv.hira2kata('まどまぎ', ignore='ど'), 'マどマギ')
_compare(jaconv.hira2kata, HIRAGANA, FULL_KANA)
def test_hira2hkata():
assert_equal(jaconv.hira2hkata('ともえまみ'), 'トモエマミ')
assert_equal(jaconv.hira2hkata('ともえまみ', ignore='み'), 'トモエマみ')
_compare(jaconv.hira2hkata, HIRAGANA, HALF_KANA)
def test_kata2hira():
assert_equal(jaconv.kata2hira('巴マミ'), '巴まみ')
assert_equal(jaconv.kata2hira('マミサン', ignore='ン'), 'まみさン')
_compare(jaconv.kata2hira, FULL_KANA, HIRAGANA)
def test_h2z():
assert_equal(jaconv.h2z('ティロフィナーレ'), 'ティロフィナーレ')
assert_equal(jaconv.h2z('ティロフィナーレ', ignore='ィ'), 'ティロフィナーレ')
_compare(jaconv.h2z, HALF_KANA, FULL_KANA)
_compare(partial(jaconv.h2z, ascii=True), HALF_ASCII, FULL_ASCII)
_compare(partial(jaconv.h2z, digit=True), HALF_DIGIT, FULL_DIGIT)
for ascii in (True, False):
for digit in (True, False):
for kana in (True, False):
before = _concat(FULL_KANA, HALF_KANA,
FULL_ASCII, HALF_ASCII,
FULL_DIGIT, HALF_DIGIT)
after = _concat(FULL_KANA, FULL_KANA if kana else HALF_KANA,
FULL_ASCII, FULL_ASCII if ascii else HALF_ASCII,
FULL_DIGIT, FULL_DIGIT if digit else HALF_DIGIT)
converted = jaconv.h2z(before,
ascii=ascii, digit=digit, kana=kana)
assert_equal(converted, after)
def test_z2h():
assert_equal(jaconv.z2h('ティロフィナーレ'), 'ティロフィナーレ')
assert_equal(jaconv.z2h('ティロフィナーレ', ignore='ィ'), 'ティロフィナーレ')
_compare(partial(jaconv.z2h, kana=True), FULL_KANA, HALF_KANA)
_compare(partial(jaconv.z2h, ascii=True), FULL_ASCII, HALF_ASCII)
_compare(partial(jaconv.z2h, digit=True), FULL_DIGIT, HALF_DIGIT)
for ascii in (True, False):
for digit in (True, False):
for kana in (True, False):
before = _concat(FULL_KANA, HALF_KANA,
FULL_ASCII, HALF_ASCII,
FULL_DIGIT, HALF_DIGIT)
after = _concat(HALF_KANA if kana else FULL_KANA, HALF_KANA,
HALF_ASCII if ascii else FULL_ASCII, HALF_ASCII,
HALF_DIGIT if digit else FULL_DIGIT, HALF_DIGIT)
converted = jaconv.z2h(before,
ascii=ascii, digit=digit, kana=kana)
assert_equal(converted, after)
def test_normalize():
assert_equal(jaconv.normalize('ティロ・フィナ〜レ', 'NFKC'), 'ティロ・フィナーレ')
assert_equal(jaconv.normalize(_concat(HALF_KANA, FULL_DIGIT), 'NFKC'),
''.join(FULL_KANA)+''.join(HALF_DIGIT))
def test_kana2alphabet():
assert_equal(jaconv.kana2alphabet('まみさん'), 'mamisan')
assert_equal(jaconv.kana2alphabet('はっとり'), 'hattori')
assert_equal(jaconv.kana2alphabet('はっ'), 'haxtsu')
assert_equal(jaconv.kana2alphabet('ぽっ'), 'poxtsu')
assert_equal(jaconv.kana2alphabet('ふぁふぃふぇふぉ'), 'fafifefo')
assert_equal(jaconv.kana2alphabet('っって'), 'xtsutte')
def test_alphabet2kana():
assert_equal(jaconv.alphabet2kana('mamisan'), 'まみさん')
assert_equal(jaconv.alphabet2kana('doggu doguu'), 'どっぐ どぐう')
assert_equal(jaconv.alphabet2kana('botchi'), 'ぼっち')
assert_equal(jaconv.alphabet2kana('fainarufantaji-'), 'ふぁいなるふぁんたじー')
assert_equal(jaconv.alphabet2kana('atsui'), 'あつい')
assert_equal(jaconv.alphabet2kana('itoh'), 'いとう')
assert_equal(jaconv.alphabet2kana('ohtaku'), 'おおたく')
assert_equal(jaconv.alphabet2kana('namba'), 'なんば')
def test_alphabet2julius():
assert_equal(jaconv.hiragana2julius('てんき'), 't e N k i')
assert_equal(jaconv.hiragana2julius('やったー'), 'y a q t a:')
assert_equal(jaconv.hiragana2julius('かわいいいいい'), 'k a w a i:')
assert_equal(jaconv.hiragana2julius('やろうぜ'), 'y a r o: z e')
assert_equal(jaconv.hiragana2julius('てんきすごくいいいいいい'), 't e N k i s u g o k u i:')
def test_enlargesmallkana():
assert_equal(jaconv.enlargesmallkana('キュゥべえ'), 'キユウべえ')
assert_equal(jaconv.enlargesmallkana('しゃえい'), 'しやえい')
assert_equal(jaconv.enlargesmallkana('しゅみ'), 'しゆみ')
assert_equal(jaconv.enlargesmallkana('きょういっぱい'), 'きよういつぱい')
assert_equal(jaconv.enlargesmallkana('霞ヶ関'), '霞ケ関')
assert_equal(jaconv.enlargesmallkana('一ヵ月'), '一カ月')
assert_equal(jaconv.enlargesmallkana('シャトー'), 'シヤトー')
assert_equal(jaconv.enlargesmallkana('チューリップ'), 'チユーリツプ')
assert_equal(jaconv.enlargesmallkana('ショート'), 'シヨート')
assert_equal(jaconv.enlargesmallkana('きょういっぱい', 'っ'), 'きよういっぱい')
assert_equal(jaconv.enlargesmallkana('きょういっぱい', 'ょっ'), 'きょういっぱい')