-
Notifications
You must be signed in to change notification settings - Fork 15
/
user_input.py
267 lines (219 loc) · 9.93 KB
/
user_input.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
import os
from collections import defaultdict
import pickle
import random
from lxml import etree
class User():
'''
'''
def __init__(self):
pass
def annotate(self):
self.user = input("what is your name?:")
self.cwd = os.path.dirname(os.path.realpath(__file__))
self.out_folder = os.path.join(self.cwd, 'user_input', self.user)
if os.path.isdir(self.out_folder) == False:
os.mkdir(self.out_folder)
#user input to decide which task they are going to do
#refer to methods
self.task = input('which task would you like to do: evaluate_resource \
| resolve_sub_ | empty_base_synsets1 | empty_base_synsets2 | tops | large_synsets \
large_synsets1 | large_synsets2 | synsets_5_10 ? ')
if self.task == 'evaluate_resource':
self.evaluate_resource()
if self.task == 'resolve_sub_':
self.resolve_sub_les()
if self.task in ['empty_base_synsets1','empty_base_synsets2','tops','large_synsets',
'large_synsets1','large_synsets2','synsets_5_10']:
self.stats_synset_inspection()
def evaluate_resource(self):
'''
randomly pick x examples and use user input
to evaluate if they are correct
'''
resource = input('which resource do you want to evaluate?: ')
num_instances = input('how many instances do you want to evaluate?: ')
answer = input('monosemous (m) or polysemous (p)?: ')
monosemous = answer == "m"
average_polysemy,polysemy_dict = self.polysemy_dict()
monosemy_set = set(polysemy_dict[1])
all_le_objs = defaultdict(dict)
#get all le_obj -> synset_identifier
for le_obj in self.les_get_generator():
provenance_tag = le_obj.get_provenance()
lemma = le_obj.get_lemma()
#filter on number of senses
lemma_is_monosemous = lemma in monosemy_set
if all([lemma_is_monosemous == monosemous,
resource in provenance_tag]):
target = le_obj.get_synset_id()
if target is not None:
all_le_objs[le_obj] = target
#randomly pick x instances
random_pick = random.sample( all_le_objs.items(), int(num_instances) )
synsets = set([ sy_id for le_obj,sy_id in random_pick])
#obtain synset info
synset_info = self.synsets_get_definition_dict()
#user evaluation
annotation = {}
for counter,(le_obj,sy_id) in enumerate(random_pick):
print(' ')
print('item %s, to go: %s' % (counter,int(num_instances)-counter))
le_id = le_obj.get_id()
print(le_id)
print
sy_info = {}
if sy_id in synset_info:
sy_info = synset_info[sy_id]
print(sy_id)
definition = []
if 'definition' in sy_info:
definition = sy_info['definition']
print(definition)
answer = input('correct? (y|n):')
annotation[le_id] = {'sy_id' : sy_id,
'sy_def' : definition,
'answer' : answer == 'y'}
correct = [info['answer'] for info in annotation.values()].count(True)
perc_correct = correct/int(num_instances)
#save results
basename = "{resource}_monosemous_{monosemous}_instances_{num_instances}".format(**locals())
output_path = os.path.join(self.out_folder,basename+'.bin')
with open(output_path,'wb') as outfile:
pickle.dump((perc_correct,annotation),outfile)
print('done')
print(perc_correct)
print('for resource: %s' % resource)
print('saved at %s' % output_path)
def resolve_sub_les(self):
'''
there are LexicalEntry elements in the resource with _sub_ in it.
this method provides a way to choose which one is correct
'''
annotation = {}
synsets = set()
set_of_orbn_ids = set()
for le_obj in self.les_get_generator():
le_id = le_obj.get_sense_id()
lemma = le_obj.get_lemma()
target = le_obj.get_synset_id()
if '_sub_' in le_id:
base,rest = le_id.split('_sub_')
if base not in annotation:
annotation[base] = {'lemma': lemma,
'odwn_definition' : le_obj.get_definition(),
'checked' : False,
'synsets' : []}
synsets.update([target])
set_of_orbn_ids.update([base])
annotation[base]['synsets'].append(target)
synset_info = self.synsets_get_definition_dict()
orbn_data = self.orbn_definition_dict(self.orbn,set_of_orbn_ids=set_of_orbn_ids)
total = [len(info['synsets']) >= 2 for info in annotation.values()].count(True)
#load method
output_path = os.path.join(self.out_folder,'_sub_.bin')
if os.path.exists(output_path):
annotation = pickle.load(open(output_path,'rb'))
print()
print(total)
print()
for counter,(base,info) in enumerate(sorted(annotation.items())):
if len(info['synsets']) == 1:
continue
if info['checked']:
continue
print()
print('item: %s (of %s), to go: %s' % (counter,total,(total-counter)))
print('le_id: '+base)
print('lemma: '+info['lemma'])
print('odwn definition: '+info['odwn_definition'])
if base in orbn_data:
print('orbn_definition: '+orbn_data[base]['definition'])
for example in orbn_data[base]['examples']:
print('example: '+example)
print()
for counter,synset in enumerate(info['synsets']):
print()
print(counter)
print(synset)
print(synset_info[synset]['definition'])
correct = input("which ones are correct (separated by spaces ?: ")
try:
answers = [int(answer) for answer in correct.split(' ')]
except ValueError:
answers = []
annotation[base]['answers'] = answers
annotation[base]['checked'] = True
#save it to file
output_path = os.path.join(self.out_folder,'_sub_.bin')
with open(output_path,'wb') as outfile:
pickle.dump(annotation,outfile)
def lemma_inspection(self,min_polysemy=1):
'''
'''
pass
def stats_synset_inspection(self):
'''
@type task: str
@ivar task: empty_base_synsets | tops | large_synsets
'''
output_path = os.path.join(self.out_folder,self.task+'.bin')
synsets = pickle.load( open( os.path.join(self.cwd,'resources',self.task+'.bin'),'rb') )
if None in synsets:
synsets.remove(None)
synonym_dict = pickle.load( open ( os.path.join(self.cwd,'resources','synonym_dict.bin') ,'rb') )
#load annotation
if os.path.exists(output_path):
annotation = pickle.load( open(output_path,'rb'))
else:
annotation = { synset: {'done': False}
for synset in synsets}
total = len(synsets)
for counter,(sy_id,info) in enumerate(sorted(annotation.items())):
if info['done']:
continue
sy_obj = self.synsets_find_synset(sy_id)
if sy_obj is None:
continue
print()
print('item: %s (of %s), to go: %s' % (counter,total,(total-counter)))
print('sy_id: '+sy_id)
eng_lemmas = []
if sy_id in synonym_dict:
eng_lemmas = synonym_dict[sy_id]
print('english lemmas: %s' % eng_lemmas)
for gloss in sy_obj.get_glosses(languages=['en']):
print('gloss: %s' % gloss)
print()
print('START OF PRINTING LE_IDS')
le_dict = {}
for le_counter,le_obj in enumerate(self.les_all_les_of_one_synset(sy_id)):
print()
le_dict[str(le_counter)] = le_obj.get_id()
print('le_id identifier: %s' % le_obj.get_id())
print('le_id number: %s' % le_counter)
print('le_id definition: %s' % le_obj.get_definition())
print('le_id lemma: %s' % le_obj.get_lemma())
print('END OF PRINTING LE_IDS')
print()
#user input
le_ids_to_remove = input('le_ids to remove (numbers separated by spaces): ')
c_lu_ids_to_add = input('c_lu_ids to add (separated by spaces): ')
lemmas_to_add = input('lemmas to add (lemmas separated by spaces): ')
for category,item in [('le_ids_to_remove',''),
('c_lu_ids_to_add',c_lu_ids_to_add),
('lemmas_to_add',lemmas_to_add)]:
splitted = item.split()
annotation[sy_id][category] = splitted
for number in le_ids_to_remove.split():
try:
annotation[sy_id]['le_ids_to_remove'].append(le_dict[number])
except KeyError:
pass
annotation[sy_id]['done'] = True
#save and load
with open(output_path,'wb') as outfile:
pickle.dump(annotation,outfile)
annotation = pickle.load( open(output_path,'rb'))
print(annotation[sy_id])
input('continue?')