-
Notifications
You must be signed in to change notification settings - Fork 0
/
YEDDA_Annotator.py
921 lines (822 loc) · 39.9 KB
/
YEDDA_Annotator.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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# -*- coding: utf-8 -*-
# @Author: Jie Yang from SUTD
# @Date: 2016-Jan-06 17:11:59
# @Last Modified by: Jie Yang, Contact: jieynlp@gmail.com
# @Last Modified time: 2018-03-05 17:41:03
#!/usr/bin/env python
# coding=utf-8
from tkinter import *
from tkinter.ttk import * # Frame, Button, Label, Style, Scrollbar
from tkinter import filedialog as tkFileDialog
from tkinter import font as tkFont
from tkinter import messagebox as tkMessageBox
import re
from collections import deque
import pickle
import os.path
import platform
import codecs
import regex
from utils.recommend import *
# import pdb
# pdb.set_trace()
Bert_path="/home/terry/dev/model/chinese_roberta_wwm_ext_pytorch/"
class Example(Frame):
"""TODO 换成更贴切的类名"""
def __init__(self, parent):
Frame.__init__(self, parent)
self.Version = u"YEDDA-V1.0 标注工具"
self.OS = platform.system().lower()
self.parent = parent
self.fileName = ""
self.debug = False
self.colorAllChunk = True
self.recommendFlag = True
self.history = deque(maxlen=200)
self.currentContent = deque(maxlen=1)
self.pressCommand = {'a':"Artifical",
'b':"Event",
'c':"Fin-Concept",
'd':"Location",
'e':"Organization",
'f':"Person",
'g':"Sector",
'h':"Other"
}
self.allKey = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
self.controlCommand = {'q':"unTag", 'ctrl+z':'undo'}
self.labelEntryList = []
self.shortcutLabelList = []
# 默认 GUI 显示参数
if len(self.pressCommand) > 20:
self.textRow = len(self.pressCommand)
else:
self.textRow = 15
self.textColumn = 5
self.tagScheme = "BMES" #BNES or BIO
self.onlyNP = False ## for exporting sequence
self.keepRecommend = True
'''
self.seged: 用于导出序列,
True 用于有词语之间有空格间隔,如英文或已分词的中文,
False 用于按字分隔,如没有分词的中文
'''
self.seged = True ## False 用于没有分词的中文,True 用于英文或已分词的中文 True 启用Bert模式分词
self.configFile = "config"
self.entityRe = r'\[\@.*?\#.*?\*\](?!\#)'
self.insideNestEntityRe = r'\[\@\[\@(?!\[\@).*?\#.*?\*\]\#'
self.recommendRe = r'\[\$.*?\#.*?\*\](?!\#)'
self.goldAndrecomRe = r'\[\@.*?\#.*?\*\](?!\#)'
if self.keepRecommend:
self.goldAndrecomRe = r'\[[\@\$)].*?\#.*?\*\](?!\#)'
## 配置颜色
self.entityColor = "SkyBlue1"
self.insideNestEntityColor = "light slate blue"
self.recommendColor = 'lightgreen'
self.selectColor = 'light salmon'
self.textFontStyle = "Times"
self.initUI()
def initUI(self):
"""初始化 UI"""
self.parent.title(self.Version)
self.pack(fill=BOTH, expand=True)
for idx in range(0,self.textColumn):
self.columnconfigure(idx, weight =2)
# self.columnconfigure(0, weight=2)
self.columnconfigure(self.textColumn+2, weight=1)
self.columnconfigure(self.textColumn+4, weight=1)
for idx in range(0,16):
self.rowconfigure(idx, weight =1)
self.lbl = Label(self, text=u"文件:没有打开的文件")
self.lbl.grid(row=self.textRow +2,column=1,sticky=W, pady=4, padx=5)
# abtn1 = Button(self, text="打开", command=self.onOpen)
# abtn1.grid(row=1, column=0,columnspan=1)
#默认文字信息
self.fnt = tkFont.Font(family=self.textFontStyle,size=self.textRow,weight="bold",underline=0)
#核心编辑区域
self.text = Text(self,spacing1=10,spacing2=10,spacing3=20, font=self.fnt, selectbackground=self.selectColor)
# self.text.grid(row=4, column=1, columnspan=self.textColumn, rowspan=self.textRow, padx=12, sticky=E+W+S+N)
self.text.grid(row=0, column=1, columnspan=self.textColumn, rowspan=self.textRow-3, padx=12, sticky=E+W+S+N)
# 滚动条
self.sb = Scrollbar(self)
self.sb.grid(row = 1, column = self.textColumn, rowspan = self.textRow, padx=0, sticky = E+W+S+N)
self.text['yscrollcommand'] = self.sb.set
self.sb['command'] = self.text.yview
# self.sb.pack()
# 按钮区域
abtn = Button(self, text="打开", command=self.onOpen)
abtn.grid(row=1, column=0)
recButton = Button(self, text="开启推荐", command=self.setInRecommendModel)
recButton.grid(row=2, column=0)
noRecButton = Button(self, text="关闭推荐", command=self.setInNotRecommendModel)
noRecButton.grid(row=3, column=0)
ubtn = Button(self, text="更新词典", command=self.renewPressCommand)
ubtn.grid(row=4, column=0)
exportbtn = Button(self, text="导出", command=self.generateSequenceFile)
exportbtn.grid(row=5, column=0)
cbtn = Button(self, text="退出", command=self.quit)
cbtn.grid(row=6, column=0)
self.cursorName = Label(self, text="光标位置: ", foreground="Blue", font=(self.textFontStyle, 14, "bold"))
self.cursorName.grid(row=9, column=0, pady=4,ipady=5)
self.cursorIndex = Label(self, text=("row: %s\ncol: %s" % (0, 0)), foreground="red", font=(self.textFontStyle, 14, "bold"))
self.cursorIndex.grid(row=10, column=0, pady=4)
self.RecommendModelName = Label(self, text="推荐状态: ", foreground="Blue", font=(self.textFontStyle, 14, "bold"))
self.RecommendModelName.grid(row=12, column=0, pady=4,ipady=5)
self.RecommendModelFlag = Label(self, text=str(self.recommendFlag), foreground="red", font=(self.textFontStyle, 14, "bold"))
self.RecommendModelFlag.grid(row=13, column=0, pady=4)
# recommend_value = StringVar()
# recommend_value.set("R")
# a = Radiobutton(self.parent, text="Recommend", width=12, variable=recommend_value, value="R")
# # a.grid(row =1 , column = 2)
# a.pack(side='left')
# b = Radiobutton(self.parent, text="NotRecommend", width=12, variable=recommend_value, value="N")
# # b.grid(row =1 , column = 3)
# b.pack(side='left')
lbl_entry = Label(self, text=u"命令:")
lbl_entry.grid(row = self.textRow +1, sticky = E+W+S+N, pady=4,padx=4)
self.entry = Entry(self)
self.entry.grid(row = self.textRow +1, columnspan=self.textColumn + 1, rowspan = 1, sticky = E+W+S+N, pady=4, padx=80)
self.entry.bind('<Return>', self.returnEnter)
# for press_key in self.pressCommand.keys():
for idx in range(0, len(self.allKey)):
press_key = self.allKey[idx]
# self.text.bind(press_key, lambda event, arg=press_key:self.textReturnEnter(event,arg))
self.text.bind(press_key, self.textReturnEnter)
simplePressKey = "<KeyRelease-" + press_key + ">"
self.text.bind(simplePressKey, self.deleteTextInput)
if self.OS != "windows":
controlPlusKey = "<Control-Key-" + press_key + ">"
self.text.bind(controlPlusKey, self.keepCurrent)
altPlusKey = "<Command-Key-" + press_key + ">"
self.text.bind(altPlusKey, self.keepCurrent)
self.text.bind('<Control-Key-z>', self.backToHistory)
## disable the default copy behaivour when right click. For MacOS, right click is button 2, other systems are button3
self.text.bind('<Button-2>', self.rightClick)
self.text.bind('<Button-3>', self.rightClick)
self.text.bind('<Double-Button-1>', self.doubleLeftClick)
self.text.bind('<ButtonRelease-1>', self.singleLeftClick)
self.setMapShow()
self.enter = Button(self, text="Enter", command=self.returnButton)
self.enter.grid(row=self.textRow +1, column=self.textColumn +1)
def singleLeftClick(self, event):
"""单击鼠标左键"""
if self.debug:
print(u"动作追踪:单击鼠标左键")
cursor_index = self.text.index(INSERT)
row_column = cursor_index.split('.')
cursor_text = ("row: %s\ncol: %s" % (row_column[0], row_column[-1]))
self.cursorIndex.config(text=cursor_text)
def doubleLeftClick(self, event):
"""双击鼠标左键,选择实体 (TODO 还没有实现)"""
if self.debug:
print(u"动作追踪:双击鼠标左键")
pass
# cursor_index = self.text.index(INSERT)
# start_index = ("%s - %sc" % (cursor_index, 5))
# end_index = ("%s + %sc" % (cursor_index, 5))
# self.text.tag_add('SEL', '1.0',"end-1c")
## Disable right click default copy selection behavior
def rightClick(self, event):
if self.debug:
print(u"动作追踪:点击右键")
try:
firstSelection_index = self.text.index(SEL_FIRST)
cursor_index = self.text.index(SEL_LAST)
content = self.text.get('1.0',"end-1c")
self.writeFile(self.fileName, content, cursor_index)
except TclError:
pass
def setInRecommendModel(self):
"""开启推荐标注模式"""
self.recommendFlag = True
self.RecommendModelFlag.config(text = str(self.recommendFlag))
tkMessageBox.showinfo("Recommend Model", "Recommend Model has been activated!")
def setInNotRecommendModel(self):
"""关闭推荐标注模式"""
self.recommendFlag = False
self.RecommendModelFlag.config(text = str(self.recommendFlag))
content = self.getText()
content = removeRecommendContent(content,self.recommendRe)
self.writeFile(self.fileName, content, '1.0')
tkMessageBox.showinfo("Recommend Model", "Recommend Model has been deactivated!")
def onOpen(self):
"""打开文件"""
ftypes = [('all files', '.*'), ('text files', '.txt'), ('ann files', '.ann')]
dlg = tkFileDialog.Open(self, filetypes = ftypes)
# file_opt = options = {}
# options['filetypes'] = [('all files', '.*'), ('text files', '.txt')]
# dlg = tkFileDialog.askopenfilename(**options)
fl = dlg.show()
if fl != '':
self.text.delete("1.0",END)
text = self.readFile(fl)
self.text.insert(END, text)
self.setNameLabel("File: " + fl)
self.autoLoadNewFile(self.fileName, "1.0")
# self.setDisplay()
# self.initAnnotate()
self.text.mark_set(INSERT, "1.0")
self.setCursorLabel(self.text.index(INSERT))
def readFile(self, filename):
"""读文件"""
with codecs.open(filename, "rU", encoding='utf-8') as f:
text = f.read()
self.fileName = filename
return text
def setFont(self, value):
"""设置字体"""
_family=self.textFontStyle
_size = value
_weight="bold"
_underline=0
fnt = tkFont.Font(family= _family,size= _size,weight= _weight,underline= _underline)
Text(self, font=fnt)
def setNameLabel(self, new_file):
self.lbl.config(text=new_file)
def setCursorLabel(self, cursor_index):
if self.debug:
print("Action Track: setCursorLabel")
row_column = cursor_index.split('.')
cursor_text = ("row: %s\ncol: %s" % (row_column[0], row_column[-1]))
self.cursorIndex.config(text=cursor_text)
def returnButton(self):
if self.debug:
print("Action Track: returnButton")
self.pushToHistory()
# self.returnEnter(event)
content = self.entry.get()
self.clearCommand()
self.executeEntryCommand(content)
return content
def returnEnter(self,event):
if self.debug:
print("Action Track: returnEnter")
self.pushToHistory()
content = self.entry.get()
self.clearCommand()
self.executeEntryCommand(content)
return content
def textReturnEnter(self,event):
press_key = event.char
if self.debug:
print("Action Track: textReturnEnter")
self.pushToHistory()
print("event: ", press_key)
# content = self.text.get()
self.clearCommand()
self.executeCursorCommand(press_key.lower())
# self.deleteTextInput()
return press_key
def backToHistory(self,event):
if self.debug:
print("Action Track: backToHistory")
if len(self.history) > 0:
historyCondition = self.history.pop()
# print("history condition: ", historyCondition)
historyContent = historyCondition[0]
# print("history content: ", historyContent)
cursorIndex = historyCondition[1]
# print("get history cursor: ", cursorIndex)
self.writeFile(self.fileName, historyContent, cursorIndex)
else:
print(u"历史为空!")
self.text.insert(INSERT, 'p') # add a word as pad for key release delete
def keepCurrent(self, event):
if self.debug:
print("Action Track: keepCurrent")
print("keep current, insert:%s"%(INSERT))
print("before:", self.text.index(INSERT))
self.text.insert(INSERT, 'p')
print("after:", self.text.index(INSERT))
def clearCommand(self):
if self.debug:
print("Action Track: clearCommand")
self.entry.delete(0, 'end')
def getText(self):
textContent = self.text.get("1.0","end-1c")
return textContent
def executeCursorCommand(self,command):
if self.debug:
print("Action Track: executeCursorCommand")
content = self.getText()
print("Command:"+command)
try:
firstSelection_index = self.text.index(SEL_FIRST)
cursor_index = self.text.index(SEL_LAST)
aboveHalf_content = self.text.get('1.0',firstSelection_index)
followHalf_content = self.text.get(firstSelection_index, "end-1c")
selected_string = self.text.selection_get()
if re.match(self.entityRe,selected_string) != None :
## if have selected entity
new_string_list = selected_string.strip('[@]').rsplit('#',1)
new_string = new_string_list[0]
followHalf_content = followHalf_content.replace(selected_string, new_string, 1)
selected_string = new_string
# cursor_index = "%s - %sc" % (cursor_index, str(len(new_string_list[1])+4))
cursor_index = cursor_index.split('.')[0]+"."+str(int(cursor_index.split('.')[1])-len(new_string_list[1])+4)
afterEntity_content = followHalf_content[len(selected_string):]
if command == "q":
print('q: remove entity label')
else:
if len(selected_string) > 0:
entity_content, cursor_index = self.replaceString(selected_string, selected_string, command, cursor_index)
aboveHalf_content += entity_content
content = self.addRecommendContent(aboveHalf_content, afterEntity_content, self.recommendFlag)
self.writeFile(self.fileName, content, cursor_index)
except TclError:
## not select text
cursor_index = self.text.index(INSERT)
[line_id, column_id] = cursor_index.split('.')
aboveLine_content = self.text.get('1.0', str(int(line_id)-1) + '.end')
belowLine_content = self.text.get(str(int(line_id)+1)+'.0', "end-1c")
line = self.text.get(line_id + '.0', line_id + '.end')
matched_span = (-1,-1)
detected_entity = -1 ## detected entity type:-1 not detected, 1 detected gold, 2 detected recommend
for match in re.finditer(self.entityRe, line):
if match.span()[0]<= int(column_id) & int(column_id) <= match.span()[1]:
matched_span = match.span()
detected_entity = 1
break
if detected_entity == -1:
for match in re.finditer(self.recommendRe, line):
if match.span()[0]<= int(column_id) & int(column_id) <= match.span()[1]:
matched_span = match.span()
detected_entity = 2
break
line_before_entity = line
line_after_entity = ""
if matched_span[1] > 0 :
selected_string = line[matched_span[0]:matched_span[1]]
if detected_entity == 1:
new_string_list = selected_string.strip('[@*]').rsplit('#',1)
elif detected_entity == 2:
new_string_list = selected_string.strip('[$*]').rsplit('#',1)
new_string = new_string_list[0]
old_entity_type = new_string_list[1]
line_before_entity = line[:matched_span[0]]
line_after_entity = line[matched_span[1]:]
selected_string = new_string
entity_content = selected_string
cursor_index = line_id + '.'+ str(int(matched_span[1])-(len(new_string_list[1])+4))
if command == "q":
print('q: remove entity label')
elif command == 'y':
print("y: comfirm recommend label")
old_key = self.pressCommand.keys()[self.pressCommand.values().index(old_entity_type)]
entity_content, cursor_index = self.replaceString(selected_string, selected_string, old_key, cursor_index)
else:
if len(selected_string) > 0:
if command in self.pressCommand:
entity_content, cursor_index = self.replaceString(selected_string, selected_string, command, cursor_index)
else:
return
line_before_entity += entity_content
if aboveLine_content != '':
aboveHalf_content = aboveLine_content+ '\n' + line_before_entity
else:
aboveHalf_content = line_before_entity
if belowLine_content != '':
followHalf_content = line_after_entity + '\n' + belowLine_content
else:
followHalf_content = line_after_entity
content = self.addRecommendContent(aboveHalf_content, followHalf_content, self.recommendFlag)
self.writeFile(self.fileName, content, cursor_index)
def executeEntryCommand(self,command):
if self.debug:
print("Action Track: executeEntryCommand")
if len(command) == 0:
currentCursor = self.text.index(INSERT)
newCurrentCursor = str(int(currentCursor.split('.')[0])+1) + ".0"
self.text.mark_set(INSERT, newCurrentCursor)
self.setCursorLabel(newCurrentCursor)
else:
command_list = decompositCommand(command)
for idx in range(0, len(command_list)):
command = command_list[idx]
if len(command) == 2:
select_num = int(command[0])
command = command[1]
content = self.getText()
cursor_index = self.text.index(INSERT)
newcursor_index = cursor_index.split('.')[0]+"."+str(int(cursor_index.split('.')[1])+select_num)
# print("new cursor position: ", select_num, " with ", newcursor_index, "with ", newcursor_index)
selected_string = self.text.get(cursor_index, newcursor_index)
aboveHalf_content = self.text.get('1.0',cursor_index)
followHalf_content = self.text.get(cursor_index, "end-1c")
if command in self.pressCommand:
if len(selected_string) > 0:
# print("insert index: ", self.text.index(INSERT))
followHalf_content, newcursor_index = self.replaceString(followHalf_content, selected_string, command, newcursor_index)
content = self.addRecommendContent(aboveHalf_content, followHalf_content, self.recommendFlag)
# content = aboveHalf_content + followHalf_content
self.writeFile(self.fileName, content, newcursor_index)
def deleteTextInput(self,event):
if self.debug:
print("Action Track: deleteTextInput")
get_insert = self.text.index(INSERT)
print("delete insert:",get_insert)
insert_list = get_insert.split('.')
last_insert = insert_list[0] + "." + str(int(insert_list[1])-1)
get_input = self.text.get(last_insert, get_insert)
# print("get_input: ", get_input)
aboveHalf_content = self.text.get('1.0',last_insert)
followHalf_content = self.text.get(last_insert, "end-1c")
if len(get_input) > 0:
followHalf_content = followHalf_content.replace(get_input, '', 1)
content = aboveHalf_content + followHalf_content
self.writeFile(self.fileName, content, last_insert)
def replaceString(self, content, string, replaceType, cursor_index):
"""替换字符串"""
if replaceType in self.pressCommand:
new_string = "[@" + string + "#" + self.pressCommand[replaceType] + "*]"
newcursor_index = cursor_index.split('.')[0]+"."+str(int(cursor_index.split('.')[1])+len(self.pressCommand[replaceType])+5)
else:
print("Invaild command!")
print("cursor index: ", self.text.index(INSERT))
return content, cursor_index
content = content.replace(string, new_string, 1)
return content, newcursor_index
def writeFile(self, fileName, content, newcursor_index):
"""写文件"""
if self.debug:
print("Action track: writeFile")
if len(fileName) > 0:
new_name = fileName + '.ann' if '.ann' not in fileName else fileName
with codecs.open(new_name, 'w', encoding='utf-8') as ann_file:
ann_file.write(content)
# print("Writed to new file: ", new_name)
self.autoLoadNewFile(new_name, newcursor_index)
# self.generateSequenceFile()
else:
print("Don't write to empty file!")
def addRecommendContent(self, train_data, decode_data, recommendMode):
"""添加推荐的内容"""
if not recommendMode:
content = train_data + decode_data
else:
if self.debug:
print("Action Track: addRecommendContent, start Recommend entity")
content = maximum_matching(train_data, decode_data)
return content
def autoLoadNewFile(self, fileName, newcursor_index):
"""自动加载新文件"""
if self.debug:
print("Action Track: autoLoadNewFile")
if len(fileName) > 0:
self.text.delete("1.0",END)
text = self.readFile(fileName)
self.text.insert("end-1c", text)
self.setNameLabel("File: " + fileName)
self.text.mark_set(INSERT, newcursor_index)
self.text.see(newcursor_index)
self.setCursorLabel(newcursor_index)
self.setColorDisplay()
def setColorDisplay(self):
if self.debug:
print("Action Track: setColorDisplay")
self.text.config(insertbackground='red', insertwidth=4, font=self.fnt)
countVar = StringVar()
currentCursor = self.text.index(INSERT)
lineStart = currentCursor.split('.')[0] + '.0'
lineEnd = currentCursor.split('.')[0] + '.end'
if self.colorAllChunk:
self.text.mark_set("matchStart", "1.0")
self.text.mark_set("matchEnd", "1.0")
self.text.mark_set("searchLimit", 'end-1c')
self.text.mark_set("recommend_matchStart", "1.0")
self.text.mark_set("recommend_matchEnd", "1.0")
self.text.mark_set("recommend_searchLimit", 'end-1c')
else:
self.text.mark_set("matchStart", lineStart)
self.text.mark_set("matchEnd", lineStart)
self.text.mark_set("searchLimit", lineEnd)
self.text.mark_set("recommend_matchStart", lineStart)
self.text.mark_set("recommend_matchEnd", lineStart)
self.text.mark_set("recommend_searchLimit", lineEnd)
while True:
self.text.tag_configure("catagory", background=self.entityColor)
self.text.tag_configure("edge", background=self.entityColor)
pos = self.text.search(self.entityRe, "matchEnd" , "searchLimit", count=countVar, regexp=True)
if pos =="":
break
self.text.mark_set("matchStart", pos)
self.text.mark_set("matchEnd", "%s+%sc" % (pos, countVar.get()))
first_pos = pos
second_pos = "%s+%sc" % (pos, str(1))
lastsecond_pos = "%s+%sc" % (pos, str(int(countVar.get())-1))
last_pos = "%s + %sc" %(pos, countVar.get())
self.text.tag_add("catagory", second_pos, lastsecond_pos)
self.text.tag_add("edge", first_pos, second_pos)
self.text.tag_add("edge", lastsecond_pos, last_pos)
## color recommend type
while True:
self.text.tag_configure("recommend", background=self.recommendColor)
recommend_pos = self.text.search(self.recommendRe, "recommend_matchEnd" , "recommend_searchLimit", count=countVar, regexp=True)
if recommend_pos =="":
break
self.text.mark_set("recommend_matchStart", recommend_pos)
self.text.mark_set("recommend_matchEnd", "%s+%sc" % (recommend_pos, countVar.get()))
first_pos = recommend_pos
# second_pos = "%s+%sc" % (recommend_pos, str(1))
lastsecond_pos = "%s+%sc" % (recommend_pos, str(int(countVar.get())))
self.text.tag_add("recommend", first_pos, lastsecond_pos)
## color the most inside span for nested span, scan from begin to end again
if self.colorAllChunk:
self.text.mark_set("matchStart", "1.0")
self.text.mark_set("matchEnd", "1.0")
self.text.mark_set("searchLimit", 'end-1c')
else:
self.text.mark_set("matchStart", lineStart)
self.text.mark_set("matchEnd", lineStart)
self.text.mark_set("searchLimit", lineEnd)
while True:
self.text.tag_configure("insideEntityColor", background=self.insideNestEntityColor)
pos = self.text.search(self.insideNestEntityRe , "matchEnd" , "searchLimit", count=countVar, regexp=True)
if pos == "":
break
self.text.mark_set("matchStart", pos)
self.text.mark_set("matchEnd", "%s+%sc" % (pos, countVar.get()))
first_pos = "%s + %sc" %(pos, 2)
last_pos = "%s + %sc" %(pos, str(int(countVar.get())-1))
self.text.tag_add("insideEntityColor", first_pos, last_pos)
def pushToHistory(self):
if self.debug:
print("Action Track: pushToHistory")
currentList = []
content = self.getText()
cursorPosition = self.text.index(INSERT)
# print("push to history cursor: ", cursorPosition)
currentList.append(content)
currentList.append(cursorPosition)
self.history.append(currentList)
def pushToHistoryEvent(self,event):
if self.debug:
print("Action Track: pushToHistoryEvent")
currentList = []
content = self.getText()
cursorPosition = self.text.index(INSERT)
# print("push to history cursor: ", cursorPosition)
currentList.append(content)
currentList.append(cursorPosition)
self.history.append(currentList)
def renewPressCommand(self):
## 更新快捷方式映射表
if self.debug:
print("Action Track: renewPressCommand")
seq = 0
new_dict = {}
listLength = len(self.labelEntryList)
delete_num = 0
for key in sorted(self.pressCommand):
label = self.labelEntryList[seq].get()
if len(label) > 0:
new_dict[key] = label
else:
delete_num += 1
seq += 1
self.pressCommand = new_dict
for idx in range(1, delete_num+1):
self.labelEntryList[listLength-idx].delete(0,END)
self.shortcutLabelList[listLength-idx].config(text="NON= ")
with open(self.configFile, 'wb') as fp:
pickle.dump(self.pressCommand, fp)
self.setMapShow()
tkMessageBox.showinfo("Remap Notification", u"快捷方式已更新!\n\n配置文件保存在:" + self.configFile)
def setMapShow(self):
"""显示快捷方式映射表"""
if os.path.isfile(self.configFile):
with open (self.configFile, 'rb') as fp:
self.pressCommand = pickle.load(fp)
hight = len(self.pressCommand)
width = 2
row = 1
#修改小字体兼容中文
mapLabel = Label(self, text =u"快捷键", foreground="blue", font=(self.textFontStyle, 10, "bold"))
# mapLabel.pack(padx=5, pady=10)
mapLabel.grid(row=1, column = self.textColumn +2,columnspan=2, rowspan = 1, padx = 10,ipady=5)
self.labelEntryList = []
self.shortcutLabelList = []
for key in sorted(self.pressCommand):
row += 1
# print("key: ", key, " command: ", self.pressCommand[key])
symbolLabel = Label(self, text =key.upper() + ": ", foreground="blue", font=(self.textFontStyle, 14, "bold"))
symbolLabel.grid(row=row, column = self.textColumn +2,columnspan=1, rowspan = 1,ipady=5)
self.shortcutLabelList.append(symbolLabel)
labelEntry = Entry(self, foreground="blue", font=(self.textFontStyle, 9, "bold"))
labelEntry.insert(0, self.pressCommand[key])
labelEntry.grid(row=row, column = self.textColumn +3, columnspan=1, rowspan = 1,ipady=5,)
self.labelEntryList.append(labelEntry)
# print("row: ", row)
def getCursorIndex(self):
return self.text.index(INSERT)
def generateSequenceFile(self):
"""生成序列标注文件"""
if (".ann" not in self.fileName) and (".txt" not in self.fileName):
out_error = u"导出功能只能用于 .ann 或 .txt 文件。"
print(out_error)
tkMessageBox.showerror(u"导出错误!", out_error)
return -1
with codecs.open(self.fileName, 'rU', encoding='utf-8') as f:
fileLines = f.readlines()
lineNum = len(fileLines)
new_filename = self.fileName.split('.ann')[0]+ '.anns'
with codecs.open(new_filename, 'w', encoding='utf-8') as seqFile:
for line in fileLines:
if len(line) <= 2:
seqFile.write('\n')
continue
else:
if not self.keepRecommend:
line = removeRecommendContent(line, self.recommendRe)
# print(line, self.seged, self.tagScheme, self.onlyNP, self.goldAndrecomRe)
# exit()
#处理一句标记信息
wordTagPairs = getWordTagPairs(line, self.seged, self.tagScheme, self.onlyNP, self.goldAndrecomRe)
for wordTag in wordTagPairs:
seqFile.write(wordTag)
## use null line to seperate sentences
seqFile.write('\n')
print(u"导出序列标注文件:", new_filename)
print(u"行数:", lineNum)
showMessage = u"导出文件成功!\n\n"
showMessage += u"格式:" + self.tagScheme + "\n\n"
showMessage += u"推荐:" + str(self.keepRecommend) + "\n\n"
showMessage += u"分词:" + str(self.seged) + "\n\n"
showMessage += u"行数:" + str(lineNum) + "\n\n"
showMessage += u"文件:" + new_filename
tkMessageBox.showinfo(u"导出信息", showMessage)
def filterPunctuation(x):
"""
删除中文标点
"""
x = regex.sub(r'[‘’]', "'", x)
x = regex.sub(r'[“”]', '"', x)
x = regex.sub(r'[…]', '...', x)
x = regex.sub(r'[—]', '-', x)
x = regex.sub(r'[—]', '-', x)
x = regex.sub(r" ", "", x)
return x
def getWordTagPairs(tagedSentence, seged=True, tagScheme="BMES", onlyNP=False, entityRe=r'\[\@.*?\#.*?\*\]'):
"""处理单句标记信息"""
# print("tagedSentence",tagedSentence)
#清除掉中文标点
tagedSentence=filterPunctuation(tagedSentence)
newSent = tagedSentence.strip('\n')
filterList = re.findall(entityRe, newSent)
newSentLength = len(newSent)
chunk_list = []
start_pos = 0
end_pos = 0
if len(filterList) == 0:
singleChunkList = []
singleChunkList.append(newSent)
singleChunkList.append(0)
singleChunkList.append(len(newSent))
singleChunkList.append(False)
chunk_list.append(singleChunkList)
# print(singleChunkList)
singleChunkList = []
else:
for pattern in filterList:
# print(pattern)
singleChunkList = []
start_pos = end_pos + newSent[end_pos:].find(pattern)
end_pos = start_pos + len(pattern)
singleChunkList.append(pattern)
singleChunkList.append(start_pos)
singleChunkList.append(end_pos)
singleChunkList.append(True)
chunk_list.append(singleChunkList)
singleChunkList = []
## chunk_list format:
full_list = []
for idx in range(0, len(chunk_list)):
if idx == 0:
if chunk_list[idx][1] > 0:
full_list.append([newSent[0:chunk_list[idx][1]], 0, chunk_list[idx][1], False])
full_list.append(chunk_list[idx])
else:
full_list.append(chunk_list[idx])
else:
if chunk_list[idx][1] == chunk_list[idx-1][2]:
full_list.append(chunk_list[idx])
elif chunk_list[idx][1] < chunk_list[idx-1][2]:
print("ERROR: found pattern has overlap!", chunk_list[idx][1], ' with ', chunk_list[idx-1][2])
else:
full_list.append([newSent[chunk_list[idx-1][2]:chunk_list[idx][1]], chunk_list[idx-1][2], chunk_list[idx][1], False])
full_list.append(chunk_list[idx])
if idx == len(chunk_list) - 1 :
if chunk_list[idx][2] > newSentLength:
print("ERROR: found pattern position larger than sentence length!")
elif chunk_list[idx][2] < newSentLength:
full_list.append([newSent[chunk_list[idx][2]:newSentLength], chunk_list[idx][2], newSentLength, False])
else:
continue
# [['我觉', 0, 2, False], ['[@得你们#症状1*]', 2, 13, True], ['啊,你们……', 13, 19, False], ['[@我感觉你们新闻#描述1*]', 19, 34, True], ['界还要学习', 34, 39, False], ['[@一个,你们非常熟悉#描述1*]', 39, 56, True], ['西方的这一套value。', 56, 68, False]] False BMES False
# print(full_list, seged, tagScheme, onlyNP)
# exit()
return turnFullListToOutputPair(full_list, seged, tagScheme, onlyNP)
def turnFullListToOutputPair(fullList, seged=True, tagScheme="BMES", onlyNP=False):
if seged:
from transformers import BertTokenizer
#加载google bert 词典
# tokenizer = BertTokenizer.from_pretrained(vocab_file="./vocab.txt",pretrained_model_name_or_path="bert-base-chinese")
tokenizer = BertTokenizer.from_pretrained(pretrained_model_name_or_path=Bert_path)
pairList = []
for eachList in fullList:
if eachList[3]:
#处理标记内容
contLabelList = eachList[0].strip('[@$]').rsplit('#', 1)
# print("contLabelList",contLabelList)
# contLabelList ['得你们', '症状1*']
if len(contLabelList) != 2:
print("Error: sentence format error!")
label = contLabelList[1].strip('*')
if seged:
#原始是逐个字符分割,改为基于bert字典分割
# print("contLabelList[0]",contLabelList[0])
# contLabelList[0] = contLabelList[0].split()
contLabelList[0]=tokenizer.tokenize(contLabelList[0])
# print("contLabelList[0]",contLabelList[0])
# pass
if onlyNP:
label = "NP"
outList = outputWithTagScheme(contLabelList[0], label, tagScheme)
for eachItem in outList:
pairList.append(eachItem)
else:
#处理非标记内容
if seged:
# eachList[0] = eachList[0].split()
eachList[0]=tokenizer.tokenize(eachList[0])
for idx in range(0, len(eachList[0])):
basicContent = eachList[0][idx]
if basicContent == ' ':
continue
pair = basicContent + ' ' + 'O\n'
#pairList.append(pair)
pairList.append(pair)
# print("pairList",pairList)
# pairList ['我觉 O\n', '得 B-症状1\n', '你 M-症状1\n', '们 E-症状1\n', '啊,你们…… O\n', '我 B-描述1\n', '感 M-描述1\n', '觉 M-描述1\n', '你 M-描述1\n', '们 M-描述1\n', '新 M-描述1\n', '闻 E-描述1\n', '界还要学习 O\n', '一 B-描述1\n', '个 M-描述1\n', ', M-描述1\n', '你 M-描述1\n', '们 M-描述1\n', '非 M-描述1\n', '常 M-描述1\n', '熟 M-描述1\n', '悉 E-描述1\n', '西方的这一套value。 O\n']
# exit()
return pairList
def outputWithTagScheme(input_list, label, tagScheme="BMES"):
output_list = []
list_length = len(input_list)
if tagScheme=="BMES":
if list_length ==1:
pair = input_list[0]+ ' ' + 'S-' + label + '\n'
output_list.append(pair)
else:
for idx in range(list_length):
if idx == 0:
pair = input_list[idx]+ ' ' + 'B-' + label + '\n'
elif idx == list_length -1:
pair = input_list[idx]+ ' ' + 'E-' + label + '\n'
else:
pair = input_list[idx]+ ' ' + 'M-' + label + '\n'
output_list.append(pair)
else:
for idx in range(list_length):
if idx == 0:
pair = input_list[idx]+ ' ' + 'B-' + label + '\n'
else:
pair = input_list[idx]+ ' ' + 'I-' + label + '\n'
output_list.append(pair)
return output_list
def removeRecommendContent(content, recommendRe = r'\[\$.*?\#.*?\*\](?!\#)'):
"""删除推荐标注的内容"""
output_content = ""
last_match_end = 0
for match in re.finditer(recommendRe, content):
matched =content[match.span()[0]:match.span()[1]]
words = matched.strip('[$]').split("#")[0]
output_content += content[last_match_end:match.span()[0]] + words
last_match_end = match.span()[1]
output_content += content[last_match_end:]
return output_content
def decompositCommand(command_string):
command_list = []
each_command = []
num_select = ''
for idx in range(0, len(command_string)):
if command_string[idx].isdigit():
num_select += command_string[idx]
else:
each_command.append(num_select)
each_command.append(command_string[idx])
command_list.append(each_command)
each_command = []
num_select =''
# print(command_list)
return command_list
def main():
print(u"启动 YEDDA 标注工具!")
print((u"操作系统:%s")%(platform.system()))
root = Tk()
root.geometry("1300x700+200+200")
app = Example(root)
app.setFont(17)
root.mainloop()
if __name__ == '__main__':
main()