-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
290 lines (235 loc) · 9.3 KB
/
functions.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
import csv
import os
import re
import sys
from collections import OrderedDict
from tkinter import messagebox
from bs4 import BeautifulSoup, Tag
import config
from TK_extensions.entry_dialog import ComboBoxDialog, EntryDialog
def process_input(input_path):
def inital_meta(soup):
try:
new_metadata = soup.new_tag('metadata')
ou_tag = Tag(builder=soup.builder,
name="meta",
attrs={'name': 'origutt',
'type': 'text',
'value': clean_string(
soup.sentence.text, newlines=True,
punctuation=False, doublespaces=False)})
new_metadata.append(ou_tag)
soup.alpino_ds.append(new_metadata)
meta = soup.metadata
return soup, meta
except:
raise RuntimeError(
'No metadata found, unable to build new. Cannot use editor.')
# read xml
f = open(input_path, 'r')
xml_content = f.read()
f.close()
soup = BeautifulSoup(xml_content, 'xml')
meta = soup.metadata
external_meta = False
if not meta:
# check if a TrEd-generated file (containing metadata) is available
if os.path.isfile('{}~'.format(input_path)):
f = open('{}~'.format(input_path), 'r')
xml_content = f.read()
f.close()
soup_tilde = BeautifulSoup(xml_content, 'xml')
meta = soup_tilde.metadata
if meta:
external_meta = True
else:
soup, meta = inital_meta(soup)
else:
soup, meta = inital_meta(soup)
# original utterance
origutt = meta.find('meta', {'name': 'origutt'})
# revised utterance (optional)
revised_utt = meta.find('meta', {'name': 'revisedutt'})
revised_exists = False if revised_utt is None else True
if revised_utt is None:
revised_utt = origutt
# sentence
sentence = soup.sentence
if external_meta:
sentence_id = soup_tilde.sentence['sentid'] if \
soup_tilde.sentence.has_attr('sentid') else None
else:
sentence_id = sentence['sentid'] if sentence.has_attr(
'sentid') else None
sentence_text = clean_string(
sentence.text, newlines=True, punctuation=False, doublespaces=False)
# orignal sentence (optional)
orig_sent = meta.find('meta', {'name': 'origsent'})
(orig_sent_text, orig_sent_exists) = \
(sentence_text, False) if orig_sent is None \
else (orig_sent['value'], True)
# Alpino input (optional)
alpino_input = meta.find('meta', {'name': 'alpino_input'})
(alpino_input_text, alpino_input_exists) = \
(sentence_text, False) if alpino_input is None \
else (alpino_input['value'], True)
return {'origutt': origutt['value'],
'revised_utt': revised_utt['value'],
'revised_exists': revised_exists,
'sentence': sentence_text,
'sent_id': sentence_id,
'origsent': orig_sent_text,
'origsent_exists': orig_sent_exists,
'alpino_input': alpino_input_text,
'alpino_input_exists': alpino_input_exists,
'xml_content': soup.prettify(),
'metadata': meta
}
def hard_reset_metadata(app):
soup = BeautifulSoup(app.xml_content, "xml")
meta = soup.metadata
# remove revisedutt and alpino_input
revised_utt = meta.find('meta', {'name': 'revisedutt'})
if revised_utt:
revised_utt.decompose()
alpino_input = meta.find('meta', {'name': 'alpino_input'})
if alpino_input:
alpino_input.decompose()
# reset sentence
orig_sent = meta.find('meta', {'name': 'origsent'})
if orig_sent:
sentence_tag = Tag(builder=soup.builder,
name="sentence",
attrs={'sentid': app.sentid})
sentence_tag.string = orig_sent.attrs['value']
soup.sentence.replace_with(sentence_tag)
orig_sent.decompose()
# save the xml
with open(app.input_path, 'w+') as f:
f.write(soup.prettify())
messagebox.showinfo(
"", "Succesfully reset!\nPress OK to exit the program.")
# exit the program
sys.exit()
def build_new_metadata(app, alpino_return=None):
soup = BeautifulSoup(app.xml_content, "xml")
if not soup.metadata:
meta = app.metadata
soup.append(meta)
else:
meta = soup.metadata
revised_utt_tag = Tag(builder=soup.builder,
name="meta", attrs={'name': 'revisedutt',
'type': 'text',
'value': app.revised_utt})
alpino_input_tag = Tag(builder=soup.builder,
name="meta", attrs={'name': 'alpino_input',
'type': 'text',
'value': app.alpino_input})
if app.sentid:
sentence_tag = Tag(builder=soup.builder,
name="sentence", attrs={'sentid': app.sentid})
else:
sentence_tag = Tag(builder=soup.builder, name="sentence")
sentence_tag.string = app.sentence
# guaranteed replacements
soup.sentence.replace_with(sentence_tag)
# conditional replacements/additions
if app.revised_exists:
revised_utt = meta.find('meta', {'name': 'revisedutt'})
revised_utt.replace_with(revised_utt_tag)
else:
meta.append(revised_utt_tag)
if app.alpino_input_exists:
orig_alpino_input = meta.find('meta', {'name': 'alpino_input'})
orig_alpino_input.replace_with(alpino_input_tag)
else:
meta.append(alpino_input_tag)
if not app.origsent_exists:
orig_sent_tag = Tag(builder=soup.builder,
name="meta",
attrs={'name': 'origsent',
'type': 'text',
'value': app.origsent})
meta.append(orig_sent_tag)
if alpino_return:
a_r = BeautifulSoup(alpino_return, "xml")
soup.node.replace_with(a_r.node)
return soup.prettify()
def ask_input(frame, label_text='', options=[]):
if options != []:
inputDialog = ComboBoxDialog(frame, label_text, options)
else:
inputDialog = EntryDialog(frame, label_text)
frame.wait_window(inputDialog.top)
return inputDialog.results
def correct_parenthesize(original, correction):
'''
take a string and its corrected equivalent.
calculate the differences between the two and parenthesizes these.
differences with whitespace should be split.
'''
ws_pattern = re.compile(r'(\S+)\s+(\S+)')
pattern = r'(.*)'
replace_pattern = r''
i = 1
# if there is whitespace in the correction,
# return [: ] form
if re.search(r'\s+', correction):
return '{} [: {}]'.format(original, correction)
# only edits at start or end
if original in correction:
pattern = r'(.*)({})(.*)'.format(original)
replace_pattern = r'(\1)\2(\3)'
parenthesize = re.sub(pattern, replace_pattern, correction)
remove_empty = re.sub(r'\(\)', '', parenthesize)
split_whitespace = re.sub(r'\((\S+)(\s+)(\S+)\)',
r'(\1)\2(\3)', remove_empty)
return split_whitespace
else:
pattern = r'(.*)'
replace_pattern = r''
i = 1
for letter in original:
pattern += (r'({})(.*)'.format(letter))
replace_pattern += r'(\{})\{}'.format(i, i+1)
i += 2
pattern += r'(.*)'
pattern = re.compile(pattern)
# if the pattern is not in the correction,
# a ()-notation is not possible
# in this case, return [: ]-notation
if not re.match(pattern, correction):
return '{} [: {}]'.format(original, correction)
# replace all diff with (diff)
parenthesize = re.sub(pattern, replace_pattern, correction)
# remove ()
remove_empty = re.sub(r'\(\)', '', parenthesize)
# split corrections with whitespace
split_whitespace = re.sub(r'\((\S+)(\s+)(\S+)\)',
r'(\1)\2(\3)', remove_empty)
# if all else fails, just use the [: ] notation
if not split_whitespace.replace('(', '').replace(')', '') == correction:
return '{} [: {}]'.format(original, correction)
return split_whitespace
def clean_string(string, newlines=True, punctuation=True, doublespaces=True):
# remove newlines
if newlines:
string = string.replace('\n', '')
string = string.replace('\r', '')
# add spaces around punctuation
if punctuation:
string = re.sub(r'([.,!?()\[\]])', r' \1 ', string)
# reduce double spaces to singles
if doublespaces:
string = re.sub(r'\s{2,}', ' ', string)
return string
def read_config_csv(csv_path):
out_dict = OrderedDict()
with open(csv_path) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
out_dict[row[0]] = row[1]
return dict(out_dict)
def is_whitelisted_system_keybind(event):
return True if (event.keysym != '??') and (event.keysym in config.SYSTEM_KEYBINDS) else False