-
Notifications
You must be signed in to change notification settings - Fork 56
/
command_line.py
394 lines (338 loc) · 12 KB
/
command_line.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# coding: utf8
from multiprocessing import Pool, cpu_count
from pathlib import Path
import plac
import spacy
import sys
from .sudachipy_tokenizer import init_dict, SudachipyTokenizer, SUDACHIPY_DEFAULT_SPLIT_MODE
MINI_BATCH_SIZE = 100
def ex_attr(token):
return token._
def run(
model_path=None,
sudachipy_mode=SUDACHIPY_DEFAULT_SPLIT_MODE,
use_sentence_separator=False,
hash_comment='print',
output_path=None,
output_format='0',
require_gpu=False,
parallel=1,
init_resource=False,
files=None,
):
if init_resource:
init_dict()
if require_gpu:
print("GPU enabled", file=sys.stderr)
if sudachipy_mode != SUDACHIPY_DEFAULT_SPLIT_MODE:
print("sudachipy mode is {}".format(sudachipy_mode), file=sys.stderr)
if use_sentence_separator:
print("enabling sentence separator", file=sys.stderr)
analyzer = Analyzer(
model_path,
sudachipy_mode,
use_sentence_separator,
hash_comment,
output_format,
require_gpu,
)
if parallel <= 0:
parallel = max(1, cpu_count() + parallel)
pool = None
if output_path:
output = open(str(output_path), 'w')
else:
output = sys.stdout
try:
if not files:
if sys.stdin.isatty():
parallel = 1
else:
files = [0]
if not files:
analyzer.set_nlp()
while True:
line = input()
for ol in analyzer.analyze_line(line):
print(ol, file=output)
elif parallel == 1:
analyzer.set_nlp()
for path in files:
with open(path, 'r') as f:
for line in f:
for ol in analyzer.analyze_line(line):
print(ol, file=output)
else:
buffer = []
for file_idx, path in enumerate(files):
with open(path, 'r') as f:
while True:
eof, buffer = fill_buffer(f, MINI_BATCH_SIZE * parallel, buffer)
if eof and (file_idx + 1 < len(files) or len(buffer) == 0):
break # continue to next file
if not pool:
if len(buffer) <= MINI_BATCH_SIZE: # enough for single process
analyzer.set_nlp()
for line in buffer:
for ol in analyzer.analyze_line(line):
print(ol, file=output)
break # continue to next file
parallel = (len(buffer) - 1) // MINI_BATCH_SIZE + 1
pool = Pool(parallel)
mini_batch_size = (len(buffer) - 1) // parallel + 1
mini_batches = [
buffer[idx * mini_batch_size:(idx + 1) * mini_batch_size] for idx in range(parallel)
]
for mini_batch_result in pool.map(analyzer.analyze_lines_mp, mini_batches):
for lines in mini_batch_result:
for ol in lines:
print(ol, file=output)
buffer.clear() # process remaining part of current file
except EOFError:
pass
except KeyboardInterrupt:
pass
finally:
try:
if pool:
pool.close()
finally:
output.close()
def fill_buffer(f, batch_size, buffer=None):
if buffer is None:
buffer = []
for line in f:
buffer.append(line)
if len(buffer) == batch_size:
return False, buffer
return True, buffer
class Analyzer:
def __init__(
self,
model_path,
sudachipy_mode,
use_sentence_separator,
hash_comment,
output_format,
require_gpu,
):
self.model_path = model_path
self.sudachipy_mode = sudachipy_mode
self.use_sentence_separator = use_sentence_separator
self.hash_comment = hash_comment
self.output_format = output_format
self.require_gpu = require_gpu
self.nlp = None
def set_nlp(self):
if self.nlp:
return
if self.require_gpu:
spacy.require_gpu()
if self.output_format in ['2', 'mecab']:
nlp = SudachipyTokenizer(mode=self.sudachipy_mode).tokenizer
else:
# TODO: Work-around for pickle error. Need to share model data.
if self.model_path:
nlp = spacy.load(self.model_path)
else:
nlp = spacy.load('ja_ginza')
nlp.tokenizer.set_mode(self.sudachipy_mode)
if not self.use_sentence_separator:
nlp.tokenizer.use_sentence_separator = False
self.nlp = nlp
def analyze_lines_mp(self, lines):
self.set_nlp()
return tuple(self.analyze_line(line) for line in lines)
def analyze_line(self, line):
return analyze(self.nlp, self.hash_comment, self.output_format, line)
def analyze(nlp, hash_comment, output_format, line):
line = line.rstrip('\n')
if line.startswith('#'):
if hash_comment == 'print':
return line,
elif hash_comment == 'skip':
return (),
if line == '':
return '',
if output_format in ['0', 'conllu']:
doc = nlp(line)
return analyze_conllu(doc)
elif output_format in ['1', 'cabocha']:
doc = nlp(line)
return analyze_cabocha(doc)
elif output_format in ['2', 'mecab']:
doc = nlp.tokenize(line)
return analyze_mecab(doc)
else:
raise Exception(output_format + ' is not supported')
def analyze_conllu(doc, print_origin=True):
np_tokens = {}
for chunk in doc.noun_chunks:
np_tokens[chunk.start] = 'NP_B'
for i in range(chunk.start + 1, chunk.end):
np_tokens[i] = 'NP_I'
if print_origin:
return ('# text = {}'.format(doc.text),) + tuple(conllu_token_line(token, np_tokens) for token in doc) + ('',)
else:
return tuple(conllu_token_line(token, np_tokens) for token in doc) + ('',)
def conllu_token_line(token, np_tokens):
bunsetu_bi = ex_attr(token).bunsetu_bi_label
position_type = ex_attr(token).bunsetu_position_type
ne = ex_attr(token).ne
info = '|'.join(filter(lambda s: s, [
'' if not bunsetu_bi else 'BunsetuBILabel={}'.format(bunsetu_bi),
'' if not position_type else 'BunsetuPositionType={}'.format(position_type),
'SpaceAfter=Yes' if token.whitespace_ else 'SpaceAfter=No',
np_tokens.get(token.i, ''),
'' if not token.ent_type else 'ENE7={}_{}'.format(token.ent_iob_, token.ent_type_),
'' if not ne else 'NE={}'.format(ne),
]))
return '{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}'.format(
token.i + 1,
token.orth_,
token.lemma_,
token.pos_,
token.tag_.replace(',*', '').replace(',', '-'),
'NumType=Card' if token.pos_ == 'NUM' else '_',
0 if token.head.i == token.i else token.head.i + 1,
token.dep_.lower() if token.dep_ else '_',
'_',
info if info else '_',
)
def analyze_cabocha(doc):
lines = []
for t in doc:
if ex_attr(t).bunsetu_bi_label == 'B':
lines.append(cabocha_bunsetu_line(t, doc))
lines.append(cabocha_token_line(t))
lines.append('EOS')
lines.append('')
return lines
def cabocha_bunsetu_line(token, doc):
bunsetu_index = ex_attr(token).bunsetu_index
bunsetu_head_index = None
bunsetu_dep_index = None
bunsetu_func_index = None
dep_type = 'D'
for t in doc[token.i:]:
if bunsetu_index != ex_attr(t).bunsetu_index:
if bunsetu_func_index is None:
bunsetu_func_index = t.i - token.i
break
tbi = ex_attr(t.head).bunsetu_index
if bunsetu_index != tbi:
bunsetu_head_index = t.i - token.i
bunsetu_dep_index = tbi
if bunsetu_func_index is None and ex_attr(t).bunsetu_position_type in {'FUNC', 'SYN_HEAD'}:
bunsetu_func_index = t.i - token.i
else:
if bunsetu_func_index is None:
bunsetu_func_index = len(doc) - token.i
if bunsetu_head_index is None:
bunsetu_head_index = 0
if bunsetu_dep_index is None:
bunsetu_dep_index = -1
return '* {} {}{} {}/{} 0.000000'.format(
bunsetu_index,
bunsetu_dep_index,
dep_type,
bunsetu_head_index,
bunsetu_func_index,
)
def cabocha_token_line(token):
sudachi = ex_attr(token).sudachi
if isinstance(sudachi, list):
part_of_speech = sudachi[0].part_of_speech()
else:
part_of_speech = sudachi.part_of_speech()
return '{}\t{},{},{},{}\t{}'.format(
token.orth_,
','.join(part_of_speech),
token.lemma_,
ex_attr(token).reading if ex_attr(token).reading else token.orth_,
'*',
'O' if token.ent_iob_ == 'O' else '{}-{}'.format(token.ent_iob_, token.ent_type_),
)
def analyze_mecab(sudachipy_tokens):
return tuple(mecab_token_line(t) for t in sudachipy_tokens) + ('EOS', '')
def mecab_token_line(token):
reading = token.reading_form()
return '{}\t{},{},{},{}'.format(
token.surface(),
','.join(token.part_of_speech()),
token.normalized_form(),
reading if reading else token.surface(),
'*',
)
@plac.annotations(
model_path=("model directory path", "option", "b", str),
sudachipy_mode=("sudachipy mode", "option", "m", str),
hash_comment=("hash comment", "option", "c", str, ['print', 'skip', 'analyze']),
output_path=("output path", "option", "o", Path),
parallel=("parallel level (default=-1, all_cpus=0)", "option", "p", int),
init_resource=("initialize resources", "flag", "i"),
files=("input files", "positional"),
)
def run_ginzame(
model_path=None,
sudachipy_mode=SUDACHIPY_DEFAULT_SPLIT_MODE,
hash_comment='print',
output_path=None,
parallel=-1,
init_resource=False,
*files,
):
run(
model_path=model_path,
sudachipy_mode=sudachipy_mode,
use_sentence_separator=False,
hash_comment=hash_comment,
output_path=output_path,
output_format='mecab',
require_gpu=False,
parallel=parallel,
init_resource=init_resource,
files=files,
)
def main_ginzame():
plac.call(run_ginzame)
@plac.annotations(
model_path=("model directory path", "option", "b", str),
sudachipy_mode=("sudachipy mode", "option", "m", str),
use_sentence_separator=("enable sentence separator", "flag", "s"),
hash_comment=("hash comment", "option", "c", str, ['print', 'skip', 'analyze']),
output_path=("output path", "option", "o", Path),
output_format=("output format", "option", "f", str, ['0', 'conllu', '1', 'cabocha', '2', 'mecab']),
require_gpu=("enable require_gpu", "flag", "g"),
parallel=("parallel level (default=1, all_cpus=0)", "option", "p", int),
init_resource=("initialize resources", "flag", "i"),
files=("input files", "positional"),
)
def run_ginza(
model_path=None,
sudachipy_mode=SUDACHIPY_DEFAULT_SPLIT_MODE,
use_sentence_separator=False,
hash_comment='print',
output_path=None,
output_format='conllu',
require_gpu=False,
parallel=1,
init_resource=False,
*files,
):
run(
model_path=model_path,
sudachipy_mode=sudachipy_mode,
use_sentence_separator=use_sentence_separator,
hash_comment=hash_comment,
output_path=output_path,
output_format=output_format,
require_gpu=require_gpu,
parallel=parallel,
init_resource=init_resource,
files=files,
)
def main_ginza():
plac.call(run_ginza)
if __name__ == '__main__':
plac.call(run_ginza)