-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.py
633 lines (509 loc) · 21.5 KB
/
edit.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
# -*- coding: UTF-8 -*-
#
# DAE (Dialogue Act Editor) is program for doing semantic and dialogue act annotation
# for dialogs stored in XML format, compatible with DATE format.
#
# Copyright (c) 2005 by Filip Jurcicek and Jiri Zahradil, Department of Cybernetics,
# University of West Bohemia, Czech Republic
#
# See LICENSE.TXT for license details
import xml.dom.minidom
import xml.parsers.expat
import codecs, platform
import webbrowser
import datetime
import os, wx, re, glob, wx.lib.layoutf
import wx.lib.dialogs, wx.lib.layoutf, wx.lib.scrolledpanel
from editor_model import *
from dialogs import *
from TriListCtrl import *
from DescribeDialog import *
from ScrolledMessageEditDialog import *
from SingleDAEditDialog import *
from DAEditDialog import *
from DAEHtmlListBox import *
from TranslateDialog import *
from ConfigDialog import *
class DEAFrame(wx.Frame):
def MakeButton(self, label, func, button_id = -1):
button = wx.Button(self.panel, button_id, label, size=self.button_size)
button.Bind(wx.EVT_BUTTON, func)
self.buttons.Add(button, 0)
return button
def MakeSpacer(self):
self.buttons.Add(wx.StaticLine(self.panel,-1,style=wx.LI_VERTICAL,size=(0,10)))
def __init__(
self, parent, ID, title, datovy_model, filename=None,
pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.DEFAULT_FRAME_STYLE
):
# FRAME
wx.Frame.__init__(self, parent, ID, title, pos, size, style)
self.SetAutoLayout(False)
self.defaultLabel = self.GetLabel()+" "
# self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
self.panel = wx.Panel(self, -1)
# DATAMODEL
self.dm = datovy_model
self.window_title = "DAE"
# TLACITKA
self.button_size = (120,-1)
self.buttons = wx.BoxSizer(wx.VERTICAL)
self.MakeButton("", self.OnClose, wx.ID_CLOSE)
self.MakeSpacer()
self.MakeButton("", self.OnOpenXML, wx.ID_OPEN)
self.MakeButton("", self.OnSaveXML, wx.ID_SAVE)
self.MakeSpacer()
self.MakeButton("S&how XML", self.OnSource)
# self.MakeButton("&Edit turn XML", self.OnEditRAWTurn)
self.MakeButton("&Describe", self.OnDescribe)
self.MakeButton("&Annotate", self.OnAnnotate)
self.MakeButton("&Translate", self.OnTranslate)
self.MakeSpacer()
self.MakeButton("", self.OnPrint, wx.ID_PRINT)
self.MakeButton("Confi&guration", self.OnConfigEdit)
# self.MakeSpacer()
# self.MakeButton("Change a &view", self.OnChangeView)
# CHOOSE A VIEW:
choices = [
(0, "text","Only TXT"),
(1, "da", "Only DAs"),
(2, "text_da","Both TXT+DAs"),
(3, "translation", "Translation"),
(4, "da_with_states", "DAs with states")]
self.radioBox = wx.RadioBox(self.panel,-1,
label="Choose a view",
choices = map(lambda x: x[2], choices),
style=wx.VERTICAL,
size=(120,-1))
self.radioBox.SetSelection(2)
self.radioBox.choices = choices
self.radioBox.Bind(wx.EVT_RADIOBOX, self.OnChangeViewRB)
self.buttons.Add(self.radioBox,0)
self.MakeSpacer()
button = wx.Button(self.panel, wx.ID_REFRESH, size=self.button_size)
button.Bind(wx.EVT_BUTTON, self.RefreshDir)
self.buttons.Add(button, 0)
self.MakeSpacer()
self.btns_stavy = []
self.btns_stavy.append(self.MakeButton("Stav &1", lambda e: self.OnStav(1,e)))
self.btns_stavy.append(self.MakeButton("Stav &2", lambda e: self.OnStav(2,e)))
self.btns_stavy.append(self.MakeButton("Stav &3", lambda e: self.OnStav(3,e)))
self.btns_stavy.append(self.MakeButton("Stav &4", lambda e: self.OnStav(4,e)))
self.btns_stavy.append(self.MakeButton("Stav &5", lambda e: self.OnStav(5,e)))
self.btns_stavy.append(self.MakeButton("Stavy - napoveda", self.HelpOnStav))
# TURNS LISTBOX
self.lstTurn = DAEHtmlListBox(self.panel, -1, size=(-1,-1), style=wx.BORDER_SUNKEN | wx.EXPAND)
self.dm.turns = list()
self.dm.dom = False
self.lstTurn.dm = self.dm
self.lstTurn.SetItemCount(0)
self.lstTurn.Bind(wx.EVT_LISTBOX_DCLICK, self.OnAnnotate)
self.lstTurn.Bind(wx.EVT_KEY_DOWN, self.OnKey)
self.lstTurn.Bind(wx.EVT_LISTBOX, self.UpdateBtns)
#if self.dm.editor_mode == "signed":
# # editor prekladu do znakove reci
# self.radioBox.SetSelection(3)
# self.lstTurn.Bind(wx.EVT_LISTBOX_DCLICK, self.OnTranslate)
# self.OnChangeViewRB(None)
self.radioBox.SetSelection(2)
for (key,val,text) in self.radioBox.choices:
if val == self.dm.editor_mode:
self.radioBox.SetSelection(key)
print "Setting mode: %s (%d)" % (val, key)
break
self.OnChangeViewRB(None)
# search text and butons
sizerSelectDir = wx.BoxSizer(wx.HORIZONTAL)
self.textSearch = wx.TextCtrl(self.panel, -1, "", size=(-1,-1))
self.textSearch.Bind(wx.EVT_KEY_DOWN, self.OnTextSearchKey)
srchDir = wx.Button(self.panel, -1, "Search in the direc(&z)tory")
srchDir.Bind(wx.EVT_BUTTON, self.OnSearchInDir)
srchDlg = wx.Button(self.panel, -1, "Search in the dialog(&x)ue")
srchDlg.Bind(wx.EVT_BUTTON, self.OnSearchInDlg)
sizerSelectDir.Add(self.textSearch, 1, wx.ALL | wx.EXPAND, 5)
sizerSelectDir.Add(srchDir)
sizerSelectDir.Add(srchDlg)
# DIALOGS LISTBOX
self.lstDialog = TriListCtrl(self.panel,-1,["File name", "Processing state", "Description"], size=(-1, -1))
self.lstDialog.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnOpenXML)
# MAIN LAYOUT
sizerTB = wx.BoxSizer(wx.HORIZONTAL)
sizerTB.Add(self.lstTurn, 1, wx.EXPAND)
sizerTB.Add(self.buttons, 0, wx.EXPAND)
sizerTBD = wx.BoxSizer(wx.VERTICAL)
sizerTBD.Add(sizerTB, 9, wx.EXPAND)
sizerTBD.Add(sizerSelectDir, 0, wx.EXPAND)
sizerTBD.Add(self.lstDialog, 2, wx.EXPAND)
self.Bind(wx.EVT_CLOSE, self.OnClose)
# status bar can be used for DEBUG purposes
self.CreateStatusBar()
self.SetStatusText("OK")
self.panel.SetSizer(sizerTBD)
self.lstDialog.SetFocus()
self.RefreshDir(None)
self.ShowStavyBtns(False)
if filename != None:
self.OnOpenXML(None, filename)
def UpdateBtns(self, evt):
n = self.lstTurn.GetSelection()
if n < 0:
MyMsgBox("First of all, open any dialogue XML.", wx.OK)
return
spk = self.dm.turns[n].getAttribute("speaker")
self.ShowStavyBtns(spk == "user")
def ShowStavyBtns(self, show = False):
for b in self.btns_stavy:
b.Enable(show)
def HelpOnStav(self, evt):
print "Help On Stav"
def RefreshWindow(self):
if self.dm.GetModified():
self.SetLabel(self.window_title + " (modified)")
else:
self.SetLabel(self.window_title)
self.lstTurn.RefreshAll()
def OnStav(self, cislo_stavu, evt):
n = self.lstTurn.GetSelection()
# t = self.dm.turns[n]
das = self.dm.RetrieveDA_DOM(n)
for i in range(len(das)-1,0,-1):
das[i].setAttribute("state5",das[i-1].getAttribute("state5"))
das[0].setAttribute("state5", str(cislo_stavu))
self.dm.SetModified()
self.RefreshWindow()
# self.dm.ReplaceTurn(n, t)
# print "We are here %d" % cislo_stavu
def OnTextSearchKey(self, event):
kc = event.GetKeyCode()
if kc == wx.WXK_RETURN:
self.OnSearchInDir(event)
else:
event.Skip()
def OnSearchInDir(self, event):
self.SetStatusText("Searching in current directory.")
self.lstDialog.data = glob.glob(self.dm.datadir + "/*.xml")
self.lstDialog.StoreSelected()
self.lstDialog.DeleteAllItems()
searchRequest = self.textSearch.GetValue().split(' ')
for filename in self.lstDialog.data:
try:
dom = xml.dom.minidom.parse(filename)
except xml.parsers.expat.ExpatError:
# MOD by JZ @ 5.8.2005
# continue with other files
print "XML parsing error in file %s" % filename
continue
if self.SearchInString(dom, searchRequest):
self.InsertDialogue(dom, filename)
self.lstDialog.SortListItems()
self.lstDialog.SetFocus()
self.Refresh()
self.SetStatusText("OK")
def OnSearchInDlg(self, event):
n = self.lstTurn.GetSelection()
if n < 0:
MyMsgBox("First of all, open any dialogue XML.", wx.OK)
return
searchRequest = self.textSearch.GetValue().split(' ')
for turn in range(n + 1, self.dm.GetTurnNum()):
if self.SearchInString(self.dm.turns[turn], searchRequest):
self.lstTurn.SetSelection(turn)
break
else:
for turn in range(0, n):
if self.SearchInString(self.dm.turns[turn], searchRequest):
self.lstTurn.SetSelection(turn)
break
self.lstTurn.SetFocus()
def SearchInString(self, turn, searchRequest):
msg = myPrettyXml(turn, "unicode").strip()
fnd = True
for each in searchRequest:
result = msg.find(each)
if result == -1:
fnd = False
break
return fnd
def RefreshDir(self, event):
self.SetStatusText("Refreshing the list of dialogues.")
self.lstDialog.data = glob.glob(self.dm.datadir + "/*.xml")
self.lstDialog.StoreSelected()
self.lstDialog.DeleteAllItems()
for filename in self.lstDialog.data:
try:
dom = xml.dom.minidom.parse(filename)
except xml.parsers.expat.ExpatError:
# MOD by JZ @ 5.8.2005
# continue with other files
print "XML parsing error in file %s" % filename
continue
# sys.excepthook(sys.exc_info()[0],sys.exc_info()[1],sys.exc_info()[2])
# return False
self.InsertDialogue(dom, filename)
self.lstDialog.SortListItems()
self.Refresh()
self.SetStatusText("OK")
def InsertDialogue(self, dom, filename):
elem = dom.getElementsByTagName("task")[0] #root element
try:
description = elem.getAttribute("description")
except:
description = ""
try:
processingState = elem.getAttribute("processing-state")
except:
processingState = ""
if processingState == "":
try:
processingState = elem.getAttribute("processig-state")
elem.removeAttribute("processig-state")
except:
processingState = ""
if processingState == "":
processingState = "unannotated"
self.lstDialog.InsertItem((filename, processingState, description))
def OnClose(self, event):
if self.dm.GetModified():
ret = MyMsgBox("The dialogue XML was modified. \n \n"
"Do you want to save the modified dialogue?\n", wx.YES | wx.NO | wx.CANCEL)
if ret == wx.YES:
try:
self.dm.Save()
except AttributeError:
pass
if ret == wx.CANCEL:
return
# if ret == wx.NO:
# continue
self.Destroy()
def OnEditRAWTurn(self, event):
turn_num = self.lstTurn.GetSelection()
if turn_num < 0:
MyMsgBox("First of all, open any dialogue XML.", wx.OK)
return
t = self.dm.turns[turn_num]
msg = myPrettyXml(t, "unicode")
dlg = ScrolledMessageEditDialog(self, msg, "Editable source XML of turn %d" % (turn_num+1))
dlg.ShowModal()
msg = dlg.GetResult()
if msg:
# text není stejný, při stornu vrací False
# upravit text
dom = xml.dom.minidom.parseString(msg)
new_turns = dom.getElementsByTagName("turn")
# new_turns by milo být pole délky 1
if (len(new_turns) <1):
#chyba není to tam!
MyMsgBox("The element TURN was not found.")
return
MyMsgBox("I modified the array of turns.")
self.dm.ReplaceTurn(turn_num, new_turns[0])
self.RefreshWindow()
def OnKey(self, event):
kc = event.GetKeyCode()
if kc == wx.WXK_RETURN or kc == wx.WXK_SPACE:
if self.dm.editor_mode == "signed":
self.OnTranslate(None)
else:
self.OnAnnotate(None)
## elif kc == wx.WXK_ESCAPE:
## # escape
## self.Close()
else:
event.Skip()
# def OnChangeView(self, event):
# try:
# if self.lstTurn.style == "text":
# self.lstTurn.style = "da"
# self.radioBox.SetSelection(1)
# else:
# if self.lstTurn.style == "da":
# self.lstTurn.style = "text_da"
# self.radioBox.SetSelection(2)
# else:
# if self.lstTurn.style == "text_da":
# self.lstTurn.style = "translation"
# self.radioBox.SetSelection(3)
# else:
# self.lstTurn.style = "text"
# self.radioBox.SetSelection(0)
#
# self.lstTurn.RefreshAll()
# except AttributeError:
# pass
def OnChangeViewRB(self, event):
sel = self.radioBox.GetSelection()
self.lstTurn.style = self.radioBox.choices[sel][1]
self.RefreshWindow()
def OnDescribe(self, event):
n = self.lstTurn.GetSelection()
if n < 0:
MyMsgBox("First of all, open any dialogue XML.", wx.OK)
return
dlg = DescribeDialog(self, -1, "Description", self.dm.getDescription(),
self.dm.getProcessingState(), self.dm.processingStateTags)
if dlg.ShowModal() == wx.ID_OK:
self.dm.setDescription(dlg.getDescription())
self.dm.setProcessingState(dlg.getProcessingState())
self.dm.SetModified()
# change attributes in self.lstDialog
self.lstDialog[self.lstDialog.currentItem] = (self.lstDialog[self.lstDialog.currentItem][0],
dlg.getProcessingState(), dlg.getDescription())
self.lstDialog.Refresh()
self.RefreshWindow()
self.lstTurn.SetFocus()
def OnTranslate(self, event):
n = self.lstTurn.GetSelection()
if n < 0:
MyMsgBox("First of all, open any dialogue XML.", wx.OK)
return
# das = self.dm.RetrieveDA(n)
dlg = TranslateDialog(self, -1, "Translation", \
DialogueTurn(self.dm.turns[n]), self.dm)
if dlg.ShowModal() == wx.ID_OK:
turn = dlg.GetResult()
# self.dm.StoreTurn(n, das)
self.dm.turns[n] = turn.GetXMLDOMTurn()
self.RefreshWindow()
self.lstTurn.SetFocus()
def OnAnnotate(self, event):
n = self.lstTurn.GetSelection()
if n < 0:
MyMsgBox("First of all, open any dialogue XML.", wx.OK)
return
das = self.dm.RetrieveDA(n)
dlg = DAEditDialog(self, -1, "Editation of dialogue acts", das, self.dm.GetTurnText(n), self.dm)
if dlg.ShowModal() == wx.ID_OK:
das = dlg.GetDialogActs()
self.dm.StoreDA(n, das)
self.RefreshWindow()
self.lstTurn.SetFocus()
def OnSource(self,event):
n = self.lstTurn.GetSelection()
if n < 0:
MyMsgBox("First of all, open any dialogue XML.", wx.OK)
return
source = self.dm.GetSourceXML()
dlg = wx.lib.dialogs.ScrolledMessageDialog(self, source, "Source XML code from file: "+self.dm.filename,
size=(700,400))
dlg.ShowModal()
def OnOpenXML(self, event, filename = None):
if self.dm.GetModified():
ret = MyMsgBox("The dialogue XML was modified. \n \n"
"Do you want to save the modified dialogue?\n", wx.YES | wx.NO | wx.CANCEL)
if ret == wx.YES:
try:
self.dm.Save()
except AttributeError:
pass
if ret == wx.CANCEL:
return
# if ret == wx.NO:
# continue with a new dialogue
try:
currentItem = event.m_itemIndex
except AttributeError:
currentItem = self.lstDialog.currentItem
try:
if filename == None:
filename = self.lstDialog.GetItemText(currentItem)
except AttributeError:
ret = MyMsgBox("No dialogue XML is selected. \n \n"
"Please, select a dilogue XML that you want to open!\n", wx.OK)
return
if not self.dm.Load(filename):
MyMsgBox("File %s cannot be loaded or parsed." % (filename))
return
loaded_count = self.dm.GetTurnNum()
self.lstTurn.SetItemCount(loaded_count)
self.dm.ImplicitState5()
if loaded_count > 0:
self.lstTurn.SetSelection(0)
self.window_title = self.defaultLabel + ": %s" % self.dm.dialog_id
self.RefreshWindow()
self.lstTurn.SetFocus()
self.UpdateBtns(None)
def OnSaveXML(self, event):
try:
self.dm.Save()
except AttributeError:
pass
def OnConfigEdit(self, event):
dlg = ConfigDialog(self, -1, "Configuration", self.dm)
if dlg.ShowModal() == wx.ID_OK:
self.dm.SaveToConfig()
pass
def OnPrint(self, event):
n = self.lstTurn.GetSelection()
if n < 0:
MyMsgBox("First of all, open any dialogue XML.", wx.OK)
return
dh = codecs.open("printed-dialogue.html", "w", "UTF-8")
dh.write('<head>\n')
dh.write('<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">\n')
dh.write('</head>\n')
# style - print
dh.write('<style media=print>\n')
dh.write('<!--\n')
dh.write('H1\n'
'{\n'
'font-size:12pt;\n'
'font-family: arial'
'}\n')
dh.write('H2\n'
'{\n'
'font-size:9pt;\n'
'font-family: arial'
'}\n')
dh.write('.custom, .custom TD, .custom TH\n'
'{\n'
'font-size:8pt;\n'
'font-family: arial'
'}\n')
dh.write('-->\n')
dh.write('</style>\n')
# style - screen
dh.write('<style media=screen>\n')
dh.write('<!--\n')
dh.write('H1\n'
'{\n'
'font-size:15pt;\n'
'font-family: arial'
'}\n')
dh.write('H2\n'
'{\n'
'font-size:12pt;\n'
'font-family: arial'
'}\n')
dh.write('.custom, .custom TD, .custom TH\n'
'{\n'
'font-size:11pt;\n'
'font-family: arial'
'}\n')
dh.write('-->\n')
dh.write('</style>\n')
# body
dh.write('<body>\n')
dh.write('<h1>\n')
dh.write(self.dm.filename)
dh.write('</h1>\n')
dh.write('<h2>\n')
dh.write('Date: ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
dh.write('</h2>\n')
dh.write('<h2>\n')
dh.write('Annotator: ' + self.dm.anotatorid)
dh.write('</h2>\n')
for n in range(self.dm.GetTurnNum()):
dh.write(self.lstTurn.OnGetItem(n) + "\n")
dh.write('</font>\n')
dh.write('</body>\n')
dh.close()
file = os.path.abspath("printed-dialogue.html")
if platform.system() == "Windows":
file = os.path.normpath(file)
file = file.replace("\\","/")
print file
os.system("cmd.exe /c start "+file)
else:
webbrowser.open(file, True, True)