-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate.py
132 lines (111 loc) · 4.96 KB
/
annotate.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
import xml.etree.ElementTree as et
# The subordinating conjunctions that can introduce indirect speech
subordinators = ['quod', 'quia', 'quoniam', 'quin']
# The files we are using (there are other files in the PROIEL Treebank)
file_names = ['caes-gal.xml', 'cic-att.xml', 'cic-off.xml', 'latin-nt.xml', 'pal-agr.xml', 'per-aeth.xml']
proiel_dir = '../../proiel-treebank/'
def is_infinitive(tok):
return tok.get('part-of-speech') == 'V-' and tok.get('relation') == 'comp' and tok.get('morphology')[3] == 'n'
def is_main_verb(tok):
return tok.get('part-of-speech') == 'V-' and tok.get('relation') == 'pred'
def is_subordinator(tok):
return tok.get('part-of-speech') == 'G-' and tok.get('relation') == 'comp' and tok.get('form') in subordinators
def print_filenames():
print("Files:")
index = 0
for file_name in file_names:
print('\t' + str(index) + ')', file_name)
index += 1
def print_sentence(sent, current_index, total):
print_loc = True
sent_str = ''
for tok in sent.iter('token'):
# Print the sentence number
if print_loc:
print(tok.get('citation-part') + ' (' + str(current_index + 1) + '/' + str(total) + ')')
print_loc = False
if tok.get('form') != None:
sent_str += tok.get('form') + tok.get('presentation-after')
print(sent_str)
def auto_process():
total_count = 0
for file_name in file_names:
root = et.Element('root')
count = 0
etree = et.parse(proiel_dir + file_name)
# Create a dictionary storing morphology
morph_tags = {}
for morph in etree.iter('morphology'):
for field in morph.iter('field'):
create = True
for value in field.iter('value'):
if create:
morph_tags[field.get('tag')] = {}
create = False
morph_tags[field.get('tag')][value.get('tag')] = value.get('summary')
morph_tags[field.get('tag')]['-'] = 'N/A' # For inapplicable values
for sent in etree.iter('sentence'):
already_added = False
get_sub_info = False
for tok in sent.iter('token'):
if not already_added and (is_infinitive(tok) or is_subordinator(tok)):
already_added = True
count += 1
if is_infinitive(tok):
sent.set('indirect-type', 'AcI')
elif is_subordinator(tok):
sent.set('indirect-type', tok.get('form'))
get_sub_info = True
sent.set('indirect-lemma', tok.get('lemma'))
sent.set('indirect-tense', morph_tags['tense'][tok.get('morphology')[2]])
sent.set('indirect-mood', morph_tags['mood'][tok.get('morphology')[3]])
sent.set('indirect-voice', morph_tags['voice'][tok.get('morphology')[4]])
if not get_sub_info:
root.append(sent)
if get_sub_info and is_main_verb(tok):
sent.set('indirect-lemma', tok.get('lemma'))
sent.set('indirect-tense', morph_tags['tense'][tok.get('morphology')[2]])
sent.set('indirect-mood', morph_tags['mood'][tok.get('morphology')[3]])
sent.set('indirect-voice', morph_tags['voice'][tok.get('morphology')[4]])
root.append(sent)
break
with open(file_name, 'wb') as file:
file.write(et.tostring(root))
print(file_name + ': annotated', count, 'sentences')
total_count += count
print('Processed a total of', total_count, 'sentences')
def manual_process():
print_filenames()
file_name = file_names[int(input("Which file? "))]
current_index = int(input('Resumption number? '))
root = et.Element('root')
# If we are resuming, re-read the already-annotaed sentences
if current_index > 0:
root = et.parse(file_name).getroot()
etree = et.parse(proiel_dir + file_name)
sents = []
for sent in etree.iter('sentence'):
already_added = False
for tok in sent.iter('token'):
if not already_added and (is_infinitive(tok) or is_subordinator(tok)):
already_added = True
sents.append(sent)
for sent in sents[current_index:]:
print_sentence(sent, current_index, len(sents))
answer = input('Indirect speech? ')
if answer == 'acc' or answer in subordinators:
sent.set('indirect-type', answer)
root.append(sent)
elif answer == 'pause':
print('Your resumption number is', current_index)
break
else:
pass
current_index += 1
with open(file_name, 'wb') as file:
file.write(et.tostring(root))
choice = input("Manual? (default = no) ")
if choice.lower() in ['y', 'yes']:
manual_process()
else:
auto_process()