-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreading.py
215 lines (178 loc) · 7.42 KB
/
reading.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
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
206
207
208
209
210
211
212
213
214
215
# -*- coding: utf-8 -*-
# This file is based on the Japanese Support add-on's reading.py, which can be
# found at <https://github.com/ankitects/anki-addons>.
#
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
#
# Automatic reading generation with kakasi and mecab.
#
import sys
import os
import re
import subprocess
import platform
from typing import Optional
from anki.utils import is_win, is_mac
kakasiArgs = ["-isjis", "-osjis", "-u", "-JH", "-KH"]
mecabArgs = ['--node-format=%m[%f[7]] ', '--eos-format=\n',
'--unk-format=%m[] ']
mecabDir = os.path.join(os.path.dirname(__file__), "support")
HTML_REPLACER = '▦'
NEWLINE_REPLACER = '▧'
def htmlReplace(text):
pattern = r"(?:<[^<]+?>)"
matches = re.findall(pattern, text)
text = re.sub(r"<[^<]+?>", HTML_REPLACER, text)
return matches, text
def escapeText(text):
text = text.replace("\n", " ")
text = text.replace(u'\uff5e', "~")
text = re.sub("<br( /)?>", NEWLINE_REPLACER, text)
#showInfo(text)
matches, text = htmlReplace(text)
text = text.replace(NEWLINE_REPLACER, "<br>")
return matches, text
if sys.platform == "win32":
si = subprocess.STARTUPINFO()
try:
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
except:
si.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
else:
si = None
# Mecab
def mungeForPlatform(popen):
if is_win:
popen = [os.path.normpath(x) for x in popen]
popen[0] += ".exe"
elif not is_mac:
popen[0] += ".lin"
elif platform.machine() == "arm64":
popen[0] += ".arm"
return popen
class ReadingNode:
def __init__(self, text: str, reading: Optional[str]):
self.text = text
self.reading = reading
def format(self, useRubyTags: bool) -> str:
if self.reading is None:
return self.text
if useRubyTags:
return "<ruby>%s<rp>(</rp><rt>%s</rt><rp>)</rp></ruby>" % (self.text, self.reading)
else:
return '%s[%s]' % (self.text, self.reading)
class MecabController(object):
def __init__(self):
self.mecab = None
def setup(self):
self.mecabCmd = mungeForPlatform([os.path.join(mecabDir, "mecab")] + mecabArgs + ['-d', mecabDir, '-r', os.path.join(mecabDir, "mecabrc")])
os.environ['DYLD_LIBRARY_PATH'] = mecabDir
os.environ['LD_LIBRARY_PATH'] = mecabDir
if not is_win:
os.chmod(self.mecabCmd[0], 0o755)
def ensureOpen(self):
if not self.mecab:
self.setup()
try:
self.mecab = subprocess.Popen(self.mecabCmd, bufsize=-1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=si)
except OSError:
raise Exception(
"Please ensure your Linux system has 64 bit binary support.")
def reading(self, expr, ignoreNumbers = True, useRubyTags = False):
self.ensureOpen()
matches, expr = escapeText(expr)
self.mecab.stdin.write(expr.encode("utf-8", "ignore") + b'\n')
self.mecab.stdin.flush()
expr = self.mecab.stdout.readline().rstrip(b'\r\n').decode('utf-8', "ignore")
nodes: list[ReadingNode] = []
for node in expr.split(" "):
if not node:
break
(kanji, reading) = re.match(r"(.+)\[(.*)\]", node).groups()
# hiragana, punctuation, not japanese, or lacking a reading
if kanji == reading or not reading:
nodes.append(ReadingNode(kanji, None))
continue
# Text in sentence is katakana
if kanji == kakasi.reading(reading):
nodes.append(ReadingNode(kanji, None))
continue
# convert to hiragana
reading = kakasi.reading(reading)
# Text in sentence is hiragana
if reading == kanji:
nodes.append(ReadingNode(kanji, None))
continue
# don't add readings of numbers
if ignoreNumbers and kanji in u"一二三四五六七八九十0123456789":
nodes.append(ReadingNode(kanji, None))
continue
# strip matching characters and beginning and end of reading and kanji
# reading should always be at least as long as the kanji
placeL = 0
placeR = 0
for i in range(1, len(kanji)):
if kanji[-i] != reading[-i]:
break
placeR = i
for i in range(0, len(kanji)-1):
if kanji[i] != reading[i]:
break
placeL = i+1
if placeL == 0:
if placeR == 0:
# CASE: The entire word receives the entire reading (no affix)
# Example: 男【オトコ】
nodes.append(ReadingNode(kanji, reading))
else:
# CASE: Okurigana at the end of the kanji (reading only applies to a prefix)
# Example: 口走る【クチバシル】
nodes.append(ReadingNode(kanji[:-placeR], reading[:-placeR]))
nodes.append(ReadingNode(reading[-placeR:], None))
else:
if placeR == 0:
# CASE: Kanji has a prefix that's written in kana
# Example: お前[オマエ]
nodes.append(ReadingNode(reading[:placeL], None))
nodes.append(ReadingNode(kanji[placeL:], reading[placeL:]))
else:
# CASE: Kanji has a prefix that's written in kana AND a suffix
# Example: みじん切り[ミジンギリ]
nodes.append(ReadingNode(reading[:placeL], None))
nodes.append(ReadingNode(kanji[placeL:-placeR], reading[placeL:-placeR]))
nodes.append(ReadingNode(reading[-placeR:], None))
# Combine our nodes together into a single sentece
fin = ''.join(node.format(useRubyTags) for node in nodes)
# Finalize formatting
for match in matches:
fin = fin.replace(HTML_REPLACER, match, 1)
fin = re.sub(r'& ?nbsp ?;', ' ', re.sub(r"< ?br ?>", "<br>", re.sub(r"> ", ">", fin.strip())))
return fin
# Kakasi
class KakasiController(object):
def __init__(self):
self.kakasi = None
def setup(self):
self.kakasiCmd = mungeForPlatform([os.path.join(mecabDir, "kakasi")] + kakasiArgs)
os.environ['ITAIJIDICT'] = os.path.join(mecabDir, "itaijidict")
os.environ['KANWADICT'] = os.path.join(mecabDir, "kanwadict")
if not is_win:
os.chmod(self.kakasiCmd[0], 0o755)
def ensureOpen(self):
if not self.kakasi:
self.setup()
try:
self.kakasi = subprocess.Popen(self.kakasiCmd, bufsize=-1, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, startupinfo=si)
except OSError:
raise Exception("Please install kakasi")
def reading(self, expr):
self.ensureOpen()
_, expr = escapeText(expr)
self.kakasi.stdin.write(expr.encode("sjis", "ignore") + b'\n')
self.kakasi.stdin.flush()
res = self.kakasi.stdout.readline().rstrip(b'\r\n').decode("sjis")
return res
# Init
kakasi = KakasiController()
mecab = MecabController()