-
Notifications
You must be signed in to change notification settings - Fork 11
/
mecab.py
executable file
·222 lines (192 loc) · 7.62 KB
/
mecab.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
216
217
218
219
220
221
222
#!/usr/bin/python3 -u
# Copyright (C) 2019 siikamiika
# Author: siikamiika
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import json
import sys
import os
import re
import struct
import subprocess
import threading
if sys.version_info[0] == 3:
import queue
from itertools import zip_longest
elif sys.version_info[0] == 2:
import Queue as queue
from itertools import izip_longest as zip_longest
DIR = os.path.realpath(os.path.dirname(__file__))
def read_stdin(length):
if sys.version_info[0] == 3:
return sys.stdin.buffer.read(length)
elif sys.version_info[0] == 2:
return sys.stdin.read(length)
def write_stdout(data):
if sys.version_info[0] == 3:
return sys.stdout.buffer.write(data)
elif sys.version_info[0] == 2:
return sys.stdout.write(data)
def flush_stdout():
if sys.version_info[0] == 3:
sys.stdout.buffer.flush()
elif sys.version_info[0] == 2:
sys.stdout.flush()
def get_message():
raw_length = read_stdin(4)
if not raw_length:
sys.exit(0)
message_length = struct.unpack('@I', raw_length)[0]
message = read_stdin(message_length).decode('utf-8')
return json.loads(message)
def send_message(message_content):
encoded_content = json.dumps(message_content).encode('utf-8')
encoded_length = struct.pack('@I', len(encoded_content))
write_stdout(encoded_length)
write_stdout(encoded_content)
flush_stdout()
class Mecab:
dictionaries = {
'ipadic': ['pos', 'pos2', '_', '_', '_', '_', 'expression', 'reading', 'pron'],
'ipadic-neologd': ['pos', 'pos2', '_', '_', '_', '_', 'expression', 'reading', 'pron'],
'unidic-mecab-translate': [
'pos', 'pos2', 'pos3', 'pos4', 'inflection_type', 'inflection_form',
'lemma_reading', 'lemma', 'expression', 'reading', 'expression_base', 'reading_base'
],
}
skip_patt = u'[\s\u30fb]'
def __init__(self, dictionary_name):
self.dictionary_name = dictionary_name
self.dictionary = Mecab.dictionaries[dictionary_name]
args = [self.get_executable_path(), '-d', os.path.join(DIR, 'data', dictionary_name), '-r', os.path.join(DIR, 'mecabrc')]
self.process = subprocess.Popen(
args,
stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
bufsize=1
)
self.process_output_queue = queue.Queue()
self.stdout_thread = threading.Thread(target=self.bg_handle_stdout)
self.stdout_thread.daemon = True
self.stdout_thread.start()
def get_executable_path(self):
if os.name == 'nt':
return self.get_nt_executable_path()
return 'mecab'
def get_nt_executable_path(self):
# look up from registry
if sys.version_info[0] == 3:
import winreg
elif sys.version_info[0] == 2:
import _winreg as winreg
try:
reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'SOFTWARE\\MeCab', 0, winreg.KEY_READ)
path, _ = winreg.QueryValueEx(reg_key, 'mecabrc')
for _ in range(2):
path = os.path.dirname(path)
path = os.path.join(path, 'bin', 'mecab.exe')
if os.path.isfile(path):
return path
except OSError:
pass
# look up from program files
path = os.path.join(os.getenv("programfiles(x86)"), 'MeCab', 'bin', 'mecab.exe')
if os.path.isfile(path):
return path
# assume it exists in %PATH%
return 'mecab.exe'
def parse(self, text):
parsed_lines = []
for text_line in text.splitlines():
parsed_line = []
for text_line_part in re.findall(u'{0}|.*?(?={0})|.*'.format(Mecab.skip_patt), text_line):
if re.match(Mecab.skip_patt, text_line_part):
parsed_line.append(self.gen_dummy_output(text_line_part))
continue
self.process.stdin.write((text_line_part + '\n').encode('utf-8'))
self.process.stdin.flush()
for output_part in iter(self.process_output_queue.get, 'EOS'):
parsed_part = {}
try:
parsed_part['source'], output_part_info = output_part.split('\t', 1)
output_part_info_parsed = [None if i == '*'
else re.sub(Mecab.skip_patt, '', i.split('-')[0])
for i in output_part_info.split(',')]
parsed_part.update(zip_longest(self.dictionary, output_part_info_parsed))
parsed_line.append(parsed_part)
except Exception as e:
print(e, file=sys.stderr)
parsed_lines.append(parsed_line)
return parsed_lines
def gen_dummy_output(self, text):
output = {'source': text}
for key in self.dictionary:
if key not in output:
output[key] = None
return output
def bg_handle_stdout(self):
for line in iter(self.process.stdout.readline, b''):
self.process_output_queue.put(line.decode('utf-8').strip())
self.process.stdout.close()
class MecabOrchestrator:
def __init__(self):
self.mecabs = {}
self.start_mecabs()
def parse(self, text, dictionaries=None, retry=True):
try:
output = {}
if not dictionaries:
for mecab_name in self.mecabs:
output[mecab_name] = self.mecabs[mecab_name].parse(text)
else:
for dictionary_name in dictionaries:
output[dictionary_name] = self.mecabs[dictionary_name].parse(text)
return output
except Exception as e:
print(e, file=sys.stderr)
if retry:
self.reload_mecabs()
self.parse(text, dictionaries, False)
def reload_mecabs(self):
self.stop_mecabs()
self.start_mecabs()
def stop_mecabs(self):
for mecab_name in list(self.mecabs):
mecab = self.mecabs[mecab_name]
mecab.process.kill()
del self.mecabs[mecab_name]
def start_mecabs(self):
for dictionary_name in Mecab.dictionaries:
if os.path.isdir(os.path.join(DIR, 'data', dictionary_name)):
self.mecabs[dictionary_name] = Mecab(dictionary_name)
def main():
mecabs = MecabOrchestrator()
while True:
msg = get_message()
if msg['action'] == 'get_version':
send_message({
'sequence': msg['sequence'],
'data': {'version': 1},
})
elif msg['action'] == 'parse_text':
text = msg['params']['text']
dictionaries = msg['params'].get('dictionaries')
response = mecabs.parse(text, dictionaries)
send_message({
'sequence': msg['sequence'],
'data': response,
})
if __name__ == '__main__':
main()