-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiTask.pyx
521 lines (440 loc) · 22.2 KB
/
multiTask.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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
from cython.parallel cimport parallel, prange
cimport openmp
import analysis
import time
from functools import reduce
import sys
import CaboCha
import xml.etree.ElementTree as ET
import analysis
def divide_list(xs, n):
q = len(xs) // n
m = len(xs) % n
return reduce(
lambda acc, i:
(lambda fr = sum([ len(x) for x in acc ]):
acc + [ xs[fr:(fr + q + (1 if i < m else 0))] ]
)()
,
range(n),
[]
)
def multiList(lines, keywords, num_threads):
cdef int thread_id
cdef int thread_max = num_threads
cdef lists = divide_list(lines, num_threads)
answers = []
for i in range(thread_max):
answers.append('')
#並列化
with nogil, parallel(num_threads=thread_max):
with gil:
thread_id = openmp.omp_get_thread_num()
list = lists[thread_id]
#print(list)
answer = []
#analysiser = analysis.AnalysisContent()
keyword = keywords
#print("thread:%s\n" % thread_id + "%s" % lists[thread_id])
for sentence in list:
for key in keyword:
if key in sentence:
leadID, chunkdic, keychunkID, keytokenID, RelateGroupes, TokenGroupes = analysis.ReceivedObj(sentence, key)
"""
c = CaboCha.Parser()
tree = c.parse(sentence)
#print("%s" % tree.toString(CaboCha.FORMAT_TREE)) #簡易 Tree 表示での出力
#print("%s" % tree.toString(CaboCha.FORMAT_LATTICE)) #計算機に処理しやすいフォーマットで出力
sent = ET.fromstring(tree.toString(CaboCha.FORMAT_XML))
keychunkID = []
keytokenID = []
leadID = []
chunkdic = {}
AlreadyID = []
TokenGroupes = {}
RelateGroupes = []
for chunk in sent.findall('.//chunk'):
#print("%s" % sent.findall(".//chunk[@id='0']"))
if chunk.attrib['id'] not in AlreadyID:
leadID.append(chunk.attrib['id'])
linkid = int(chunk.attrib['id'])
RelateID = []
while True:
linkchunk = sent.find(".//chunk[@id='{linkid}']".format(**locals()))
if linkid == '-1':
RelateGroupes.append(RelateID)
#print("listed!")
#print("RelateGroupes:%s" % RelateGroupes)
else:
RelateID.append(int(linkid))
#print("appendet!")
#print("RelateID:%s" % RelateID)
if linkchunk != None:
if linkchunk.attrib['id'] not in AlreadyID:
chunkText = ""
AlreadyID.append(linkchunk.attrib['id'])
for tok in linkchunk:
chunkText = chunkText + tok.text
chunkdic.setdefault(int(linkchunk.attrib['id']), {})[int('{tok.attrib[id]}'.format(**locals()))] = [tok.attrib['feature'], tok.text]
TokenGroupes.setdefault(int(linkchunk.attrib['id']), []).append(int('{tok.attrib[id]}'.format(**locals())))
#print("text:%s" % tok.text)
#print("id:%s" % tok.attrib['id'])
if chunkText.endswith(key.replace(" ","")) is True:
keychunkID.append(int(linkchunk.attrib['id']))
keytokenID.append(int(tok.attrib['id']))
chunkdic.setdefault(int(linkchunk.attrib['id']), {})[int(sys.maxsize)] = int(linkchunk.attrib['link'])
#for id,text in sorted(chunkdic[int(linkchunk.attrib['id'])].items()):
#print("chunk:" + str(id) + "," + str(text))
linkid = linkchunk.attrib['link']
#print("linkid:%s" % linkid)
#print("keywordID:%s" % keywordID)
else:
break
"""
upSentences = analysis.stepFourteen(leadID, chunkdic, keychunkID, keytokenID, RelateGroupes, TokenGroupes)
"""
i = 0
upSentences = []
for id in keychunkID:
#print("keychunkID:%s" % id)
keychunk = chunkdic[id]
keytokenEnd = TokenGroupes[id][-1]
keytokenFirst = TokenGroupes[id][0]
try:
keyrear = keychunk[int(keytokenID[i]) + 1]
except:
if keychunk[int(sys.maxsize)] == -1:
#print("%s" % keychunk[int(sys.maxsize)])
keyrear = []
else:
keychunk = chunkdic[int(id) + 1]
keyrear = keychunk[int(keytokenID[i]) + 1]
#print("keyrear:%s" % keyrear)
if len(keyrear) != 0:
endtokenid = 0
if keyrear[1] == 'の':
#print("Yes:%s" % keyrear[1])
for Groupe in RelateGroupes:
if id in Groupe:
for groupeid in range(1, len(Groupe)):
if Groupe[groupeid] - Groupe[groupeid - 1] > 1 and Groupe[groupeid] > id and endtokenid == 0:
#print("%s" % int(Groupe[groupeid] - Groupe[groupeid - 1]))
lastid = Groupe[groupeid]
beginid = Groupe[groupeid - 1]
rearLast = []
for RelateGroupe in RelateGroupes:
#RelateGroupe_set = set(RelateGroupe)
#matching = list(RelateGroupe_set & rearGroupe_set)
rearLast.extend(list(filter((lambda x: beginid < x < lastid), RelateGroupe)))
#print("%s" % sorted(set(rearLast), reverse=True))
for chunkid in sorted(set(rearLast), reverse=True):
if endtokenid == 0:
#endtokenid,sentenceEnd=AnalysisContent.setEndToken(chunkid, chunkdic)
endtokenid = 0
sentenceEnd = 0
alltext = ""
endflag = 0
basicflag = 0
for id,text in sorted(chunkdic[chunkid].items()):
if isinstance(text, list):
alltext = alltext + text[1]
sentenceEnd = id
#print("%s" % type(text))
#print("%s" % alltext)
#print("%s" % text[1])
if "動詞" in text[0] and "自立" in text[0] and "非自立" not in text[0] and endflag == 0 and basicflag == 0:
endflag = 1
#print("自立a%s" % endflag)
elif ("助動詞" or "助詞" or "動詞" in text[0]) and endflag == 1 and basicflag == 0:
endflag = 1
#print("自立b%s" % endflag)
else:
endflag = 0
#print("自立%s" % endflag)
if "基本形" in text[0]:
basicflag = 1
if endflag == 1:
#print("endtoken:%s" % alltext)
#print("%s" % endflag)
endtokenid = id
elif alltext.endswith("だ") is True:
#print("endtoken:%s" % alltext)
endtokenid = id
elif alltext.endswith("である") is True:
#print("endtoken:%s" % alltext)
endtokenid = id
#print("%s" % endtokenid)
else:
#print("endtokenid:%s" % endtokenid)
#print("sentenceEnd:%s" % sentenceEnd)
break
if endtokenid == 0:
for Groupe in RelateGroupes:
if id in Groupe:
for chunkid in sorted(Groupe, reverse=True):
if endtokenid == 0:
#endtokenid,sentenceEnd=AnalysisContent.setEndToken(chunkid, chunkdic)
endtokenid = 0
sentenceEnd = 0
alltext = ""
endflag = 0
basicflag = 0
for id,text in sorted(chunkdic[chunkid].items()):
if isinstance(text, list):
alltext = alltext + text[1]
sentenceEnd = id
#print("%s" % type(text))
#print("%s" % alltext)
#print("%s" % text[1])
if "動詞" in text[0] and "自立" in text[0] and "非自立" not in text[0] and endflag == 0 and basicflag == 0:
endflag = 1
#print("自立a%s" % endflag)
elif ("助動詞" or "助詞" or "動詞" in text[0]) and endflag == 1 and basicflag == 0:
endflag = 1
#print("自立b%s" % endflag)
else:
endflag = 0
#print("自立%s" % endflag)
if "基本形" in text[0]:
basicflag = 1
if endflag == 1:
#print("endtoken:%s" % alltext)
#print("%s" % endflag)
endtokenid = id
elif alltext.endswith("だ") is True:
#print("endtoken:%s" % alltext)
endtokenid = id
elif alltext.endswith("である") is True:
#print("endtoken:%s" % alltext)
endtokenid = id
else:
#print("endtokenid:%s" % endtokenid)
#print("sentenceEnd:%s" % sentenceEnd)
break
else:
#print("not:%s" % keyrear[1])
#endtokenid = 0
for Groupe in RelateGroupes:
if id in Groupe:
for chunkid in sorted(Groupe, reverse=True):
if endtokenid == 0:
#endtokenid,sentenceEnd=AnalysisContent.setEndToken(chunkid, chunkdic)
endtokenid = 0
sentenceEnd = 0
alltext = ""
endflag = 0
basicflag = 0
for id,text in sorted(chunkdic[chunkid].items()):
if isinstance(text, list):
alltext = alltext + text[1]
sentenceEnd = id
#print("%s" % type(text))
#print("%s" % alltext)
#print("%s" % text[1])
if "動詞" in text[0] and "自立" in text[0] and "非自立" not in text[0] and endflag == 0 and basicflag == 0:
endflag = 1
#print("自立a%s" % endflag)
elif ("助動詞" or "助詞" or "動詞" in text[0]) and endflag == 1 and basicflag == 0:
endflag = 1
#print("自立b%s" % endflag)
else:
endflag = 0
#print("自立%s" % endflag)
if "基本形" in text[0]:
basicflag = 1
if endflag == 1:
#print("endtoken:%s" % alltext)
#print("%s" % endflag)
endtokenid = id
elif alltext.endswith("だ") is True:
#print("endtoken:%s" % alltext)
endtokenid = id
elif alltext.endswith("である") is True:
#print("endtoken:%s" % alltext)
endtokenid = id
else:
#print("endtokenid:%s" % endtokenid)
#print("sentenceEnd:%s" % sentenceEnd)
break
if endtokenid == 0:
for Groupe in RelateGroupes:
if id not in Groupe:
for chunkid in sorted(Groupe, reverse=True):
if endtokenid == 0:
#endtokenid,sentenceEnd=AnalysisContent.setEndToken(chunkid, chunkdic)
endtokenid = 0
sentenceEnd = 0
alltext = ""
endflag = 0
basicflag = 0
for id,text in sorted(chunkdic[chunkid].items()):
if isinstance(text, list):
alltext = alltext + text[1]
sentenceEnd = id
#print("%s" % type(text))
#print("%s" % alltext)
#print("%s" % text[1])
if "動詞" in text[0] and "自立" in text[0] and "非自立" not in text[0] and endflag == 0 and basicflag == 0:
endflag = 1
#print("自立a%s" % endflag)
elif ("助動詞" or "助詞" or "動詞" in text[0]) and endflag == 1 and basicflag == 0:
endflag = 1
#print("自立b%s" % endflag)
else:
endflag = 0
#print("自立%s" % endflag)
if "基本形" in text[0]:
basicflag = 1
if endflag == 1:
#print("endtoken:%s" % alltext)
#print("%s" % endflag)
endtokenid = id
elif alltext.endswith("だ") is True:
#print("endtoken:%s" % alltext)
endtokenid = id
elif alltext.endswith("である") is True:
#print("endtoken:%s" % alltext)
endtokenid = id
else:
#print("endtokenid:%s" % endtokenid)
#print("sentenceEnd:%s" % sentenceEnd)
break
if endtokenid != 0:
upToken = {}
#print("%s" % chunkdic)
#print("keytokenEnd:%s" % keytokenEnd)
#print("keytokenFirst:%s" % keytokenFirst)
for chunk in chunkdic.values():
#print("%s" % chunk)
for tokenid in chunk.keys():
#print("token:%s" % chunk[tokenid])
if isinstance(chunk[tokenid], list) is True:
if tokenid == endtokenid:
if chunk[tokenid][1].endswith("だ") is True:
chunk[tokenid][1] = "である"
if keytokenEnd < tokenid <= int(endtokenid):
setid = tokenid + keytokenFirst - keytokenEnd - 1
elif keytokenFirst <= tokenid <= keytokenID[i]:
setid = endtokenid + tokenid - keytokenEnd
elif tokenid < keytokenFirst:
setid = tokenid
else:
continue
upToken[setid] = chunk[tokenid][1]
upSentence = ''
for key in sorted(upToken.keys()):
upSentence = upSentence + upToken[key]
#print("upSentence:%s" % upSentence)
upSentences.append(upSentence)
i = i + 1
"""
answer.append(upSentences)
#print("thread:%s\n" % thread_id + "%s\n" % answer + "%s\n" % key)
answers[thread_id] = answer
return answers
def multiTask(lines, keyword, num_threads):
cdef int thread_id
cdef int thread_max = num_threads
cdef int lines_num = len(lines)
upSentencedic = [ {} for j in range(lines_num)]
leadID = []
chunkdic = []
keychunkID = []
keytokenID = []
RelateGroupes = []
TokenGroupes = []
keyword_num = []
num = []
analysiser = []
keywords = []
for i in range(thread_max):
analysiser.append(analysis.AnalysisContent())
keyword_num.append('')
num.append('')
leadID.append([])
chunkdic.append([])
keychunkID.append([])
keytokenID.append([])
RelateGroupes.append([])
TokenGroupes.append([])
keywords.append(keyword)
#start = time.time()
with nogil, parallel(num_threads=thread_max):
with gil:
thread_id = openmp.omp_get_thread_num()
for keyword_num[thread_id] in range(len(keyword)):
for num[thread_id] in range(thread_id, lines_num, thread_max):
#print("num:%s" % num[thread_id] + "%s" % lines[num[thread_id]])
if keyword[keyword_num[thread_id]] in lines[num[thread_id]]:
#print("num%s:" % num[thread_id] + "%s" % lines[num[thread_id]])
#print("thread:%s" % thread_id)
#elapsed_time = time.time() - start
#print("ReceivedObj_start:{0}".format(elapsed_time) + "[sec]" + ":%s" % thread_id)
leadID[thread_id], chunkdic[thread_id], keychunkID[thread_id], keytokenID[thread_id], RelateGroupes[thread_id], TokenGroupes[thread_id] = analysiser[thread_id].ReceivedObj(lines[num[thread_id]], keywords[thread_id][keyword_num[thread_id]])
#print(tree)
#print(print_format)
#print("leadID:%s" % leadID)
#print("keywordID:%s" % keywordID)
#print("%s" % chunkdic)
#elapsed_time = time.time() - start
#print("stepFourteen_start:{0}".format(elapsed_time) + "[sec]" + ":%s" % thread_id)
upSentencedic[num[thread_id]][keyword[keyword_num[thread_id]]] = analysiser[thread_id].stepFourteen(leadID[thread_id], chunkdic[thread_id], keychunkID[thread_id], keytokenID[thread_id], RelateGroupes[thread_id], TokenGroupes[thread_id])
#if len(upSentencedic[num]) == 0:
#upSentencedic[num] = ""
#print("num:%s" % num)
#print("thread:%s" % thread_id)
#print("\n本文:%s\n" % lines[num])
#print("%s" % num + ":%s" % upSentencedic[num])
#for upSentence in upSentencedic]:
#print(upSentence)
#print(upSentencedic)
print(openmp.omp_get_max_threads())
return upSentencedic
def multiPrange(lines, keyword, num_threads):
cdef int i
cdef int thread_max = num_threads
cdef int lines_num = len(lines)
cdef upSentencedic = [ {} for j in range(lines_num) ]
#key = []
#keywordlist = []
#num = []
#for k in range(thread_max):
#num.append('')
#key.append('')
#keywordlist.append(keyword)
analysiser = analysis.AnalysisContent()
start = time.time()
for key in keyword:
with nogil, parallel(num_threads=thread_max):
for i in prange(lines_num, schedule='static'):
with gil:
#thread_id = openmp.omp_get_thread_num()
#num[thread_id] = 0
#for key[thread_id] in keywordlist[thread_id]:
#print("num:%s" % num[thread_id] + "%s" % lines[num[thread_id]])
if key in lines[i]:
#print("num%s:" % num[thread_id] + "%s" % lines[num[thread_id]])
#print("thread:%s" % thread_id)
#elapsed_time = time.time() - start
#print("ReceivedObj_start:{0}".format(elapsed_time) + "[sec]" + ":key%s" % key + ":th%s" % thread_id + ":l%s" % i)
leadID, chunkdic, keychunkID, keytokenID, RelateGroupes, TokenGroupes = analysiser.ReceivedObj(lines[i], key)
#print(tree)
#print(print_format)
#print("leadID:%s" % leadID)
#print("keywordID:%s" % keywordID)
#print("%s" % chunkdic)
#elapsed_time = time.time() - start
#print("stepFourteen_start:{0}".format(elapsed_time) + "[sec]" + ":key%s" % key + ":th%s" % thread_id + ":l%s" % i)# + ":num%s" % num)
upSentencedic[i][key] = analysiser.stepFourteen(leadID, chunkdic, keychunkID, keytokenID, RelateGroupes, TokenGroupes)#, upSentencedic, i, num[thread_id])
#num[thread_id] =+ 1
#if len(upSentencedic[num]) == 0:
#upSentencedic[num] = ""
#print("num:%s" % num)
#print("thread:%s" % thread_id)
#print("\n本文:%s\n" % lines[num])
#print("%s" % num + ":%s" % upSentencedic[num])
#for upSentence in upSentencedic]:
#print(upSentence)
#print(upSentencedic)
#print(openmp.omp_get_max_threads())
return upSentencedic