-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
neologdn.pyx
205 lines (176 loc) · 7.95 KB
/
neologdn.pyx
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# distutils: language=c++
# cython: language_level=3
# -*- coding: utf-8 -*-
import itertools
from sys import version_info
from libc.stdlib cimport malloc, free
from libcpp.unordered_map cimport unordered_map
from libcpp.unordered_set cimport unordered_set
VERSION = (0, 5, 3)
__version__ = '0.5.3'
cdef extern from "Python.h":
object PyUnicode_DecodeUTF32(const char *s, Py_ssize_t size, const char *errors, int *byteorder)
cdef py_ucs4_to_unicode(Py_UCS4 *ucs4_ptr, Py_ssize_t length):
return PyUnicode_DecodeUTF32(<char*>ucs4_ptr, sizeof(Py_UCS4)*length, NULL, NULL)
ASCII = (
('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd'), ('e', 'e'),
('f', 'f'), ('g', 'g'), ('h', 'h'), ('i', 'i'), ('j', 'j'),
('k', 'k'), ('l', 'l'), ('m', 'm'), ('n', 'n'), ('o', 'o'),
('p', 'p'), ('q', 'q'), ('r', 'r'), ('s', 's'), ('t', 't'),
('u', 'u'), ('v', 'v'), ('w', 'w'), ('x', 'x'), ('y', 'y'),
('z', 'z'),
('A', 'A'), ('B', 'B'), ('C', 'C'), ('D', 'D'), ('E', 'E'),
('F', 'F'), ('G', 'G'), ('H', 'H'), ('I', 'I'), ('J', 'J'),
('K', 'K'), ('L', 'L'), ('M', 'M'), ('N', 'N'), ('O', 'O'),
('P', 'P'), ('Q', 'Q'), ('R', 'R'), ('S', 'S'), ('T', 'T'),
('U', 'U'), ('V', 'V'), ('W', 'W'), ('X', 'X'), ('Y', 'Y'),
('Z', 'Z'),
('!', '!'), ('”', '"'), ('#', '#'), ('$', '$'), ('%', '%'),
('&', '&'), ('’', '\''), ('(', '('), (')', ')'), ('*', '*'),
('+', '+'), (',', ','), ('−', '-'), ('.', '.'), ('/', '/'),
(':', ':'), (';', ';'), ('<', '<'), ('=', '='), ('>', '>'),
('?', '?'), ('@', '@'), ('[', '['), ('¥', '\\'), (']', ']'),
('^', '^'), ('_', '_'), ('‘', '`'), ('{', '{'), ('|', '|'),
('}', '}')
)
KANA = (
('ア', 'ア'), ('イ', 'イ'), ('ウ', 'ウ'), ('エ', 'エ'), ('オ', 'オ'),
('カ', 'カ'), ('キ', 'キ'), ('ク', 'ク'), ('ケ', 'ケ'), ('コ', 'コ'),
('サ', 'サ'), ('シ', 'シ'), ('ス', 'ス'), ('セ', 'セ'), ('ソ', 'ソ'),
('タ', 'タ'), ('チ', 'チ'), ('ツ', 'ツ'), ('テ', 'テ'), ('ト', 'ト'),
('ナ', 'ナ'), ('ニ', 'ニ'), ('ヌ', 'ヌ'), ('ネ', 'ネ'), ('ノ', 'ノ'),
('ハ', 'ハ'), ('ヒ', 'ヒ'), ('フ', 'フ'), ('ヘ', 'ヘ'), ('ホ', 'ホ'),
('マ', 'マ'), ('ミ', 'ミ'), ('ム', 'ム'), ('メ', 'メ'), ('モ', 'モ'),
('ヤ', 'ヤ'), ('ユ', 'ユ'), ('ヨ', 'ヨ'),
('ラ', 'ラ'), ('リ', 'リ'), ('ル', 'ル'), ('レ', 'レ'), ('ロ', 'ロ'),
('ワ', 'ワ'), ('ヲ', 'ヲ'), ('ン', 'ン'),
('ァ', 'ァ'), ('ィ', 'ィ'), ('ゥ', 'ゥ'), ('ェ', 'ェ'), ('ォ', 'ォ'),
('ッ', 'ッ'), ('ャ', 'ャ'), ('ュ', 'ュ'), ('ョ', 'ョ'),
('。', '。'), ('、', '、'), ('・', '・'), ('゛', '゙'), ('゜', '゚'),
('「', '「'), ('」', '」'), ('ー', 'ー')
)
DIGIT = (
('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'),
('5', '5'), ('6', '6'), ('7', '7'), ('8', '8'), ('9', '9')
)
KANA_TEN = (
('カ', 'ガ'), ('キ', 'ギ'), ('ク', 'グ'), ('ケ', 'ゲ'), ('コ', 'ゴ'),
('サ', 'ザ'), ('シ', 'ジ'), ('ス', 'ズ'), ('セ', 'ゼ'), ('ソ', 'ゾ'),
('タ', 'ダ'), ('チ', 'ヂ'), ('ツ', 'ヅ'), ('テ', 'デ'), ('ト', 'ド'),
('ハ', 'バ'), ('ヒ', 'ビ'), ('フ', 'ブ'), ('ヘ', 'ベ'), ('ホ', 'ボ'),
('ウ', 'ヴ'), ('う', 'ゔ')
)
KANA_MARU = (
('ハ', 'パ'), ('ヒ', 'ピ'), ('フ', 'プ'), ('ヘ', 'ペ'), ('ホ', 'ポ'),
('は', 'ぱ'), ('ひ', 'ぴ'), ('ふ', 'ぷ'), ('へ', 'ぺ'), ('ほ', 'ぽ')
)
HIPHENS = ('˗', '֊', '‐', '‑', '‒', '–', '⁃', '⁻', '₋', '−')
CHOONPUS = ('﹣', '-', 'ー', '—', '―', '─', '━', 'ー')
TILDES = ('~', '∼', '∾', '〜', '〰', '~')
SPACE = (' ', ' ')
cdef unordered_map[Py_UCS4, Py_UCS4] conversion_map, kana_ten_map, kana_maru_map
cdef unordered_set[Py_UCS4] blocks, basic_latin
for (before, after) in (ASCII + DIGIT + KANA):
conversion_map[before] = after
for (before, after) in KANA_TEN:
kana_ten_map[before] = after
for (before, after) in KANA_MARU:
kana_maru_map[before] = after
char_codes = itertools.chain(
range(19968, 40960), # CJK UNIFIED IDEOGRAPHS
range(12352, 12448), # HIRAGANA
range(12448, 12544), # KATAKANA
range(12289, 12352), # CJK SYMBOLS AND PUNCTUATION
range(65280, 65520) # HALFWIDTH AND FULLWIDTH FORMS
)
for c in map(chr, char_codes):
blocks.insert(c)
for c in map(chr, range(128)):
basic_latin.insert(c)
del ASCII, KANA, DIGIT, KANA_TEN, KANA_MARU, char_codes, version_info
cpdef unicode shorten_repeat(unicode text, int repeat_threshould, int max_repeat_substr_length=8):
cdef int text_length, i, repeat_length, right_start, right_end, num_repeat_substrs
cdef int upper_repeat_substr_length
cdef unicode substr, right_substr
i = 0
while i < len(text):
text_length = len(text)
upper_repeat_substr_length = (text_length - i) // 2
if max_repeat_substr_length and max_repeat_substr_length < upper_repeat_substr_length:
upper_repeat_substr_length = max_repeat_substr_length + 1
for repeat_length in range(1, upper_repeat_substr_length):
substr = text[i:i+repeat_length]
right_start = i + repeat_length
right_end = right_start + repeat_length
right_substr = text[right_start:right_end]
num_repeat_substrs = 1
while substr == right_substr and right_end <= text_length:
num_repeat_substrs += 1
right_start += repeat_length
right_end += repeat_length
right_substr = text[right_start:right_end]
if num_repeat_substrs > repeat_threshould:
text = text[:i+repeat_length*repeat_threshould] + text[i+repeat_length*num_repeat_substrs:]
i += 1
return text
cpdef unicode normalize(unicode text, int repeat=0, bint remove_space=True,
int max_repeat_substr_length=8, unicode tilde='remove'):
cdef Py_UCS4 *buf = <Py_UCS4 *>malloc(sizeof(Py_UCS4) * (len(text) + 1))
cdef Py_UCS4 c, prev = '\0'
cdef int pos = 0
cdef bint lattin_space = False
for c in text:
if c in SPACE:
c = ' '
if (prev == ' ' or blocks.count(prev)) and remove_space:
continue
elif prev != '*' and pos > 0 and basic_latin.count(prev):
lattin_space = True
buf[pos] = c
elif remove_space:
pos -= 1
else:
buf[pos] = c
else:
if c in HIPHENS:
if prev == '-':
continue
else:
buf[pos] = c = '-'
elif c in CHOONPUS:
if prev == 'ー':
continue
else:
buf[pos] = c = 'ー'
elif c in TILDES:
if tilde == 'ignore':
buf[pos] = c
elif tilde == 'normalize':
buf[pos] = c = '~'
elif tilde == 'normalize_zenkaku':
buf[pos] = c = '〜'
else:
continue
else:
if conversion_map.count(c):
c = conversion_map[c]
if c == '゙' and kana_ten_map.count(prev):
pos -= 1
c = kana_ten_map[prev]
elif c == '゚' and kana_maru_map.count(prev):
pos -= 1
c = kana_maru_map[prev]
if lattin_space and blocks.count(c) and remove_space:
pos -= 1
lattin_space = False
buf[pos] = c
prev = c
pos += 1
if buf[pos-1] == ' ':
pos -= 1
buf[pos] = '\0'
cdef unicode ret = py_ucs4_to_unicode(buf, pos)
free(buf)
if repeat:
return shorten_repeat(ret, repeat, max_repeat_substr_length)
return ret