-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnswer_type_detection.py
More file actions
52 lines (44 loc) · 1.51 KB
/
Copy pathAnswer_type_detection.py
File metadata and controls
52 lines (44 loc) · 1.51 KB
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
import pickle
import nltk
def find_answer_type(questions_file):
# question content
d = {}
with open(questions_file, 'r') as f:
for line in f.readlines():
line = line.rstrip()
pair = line.split(':')
if pair[0] == 'QuestionID':
key = pair[1].strip()
d[key] = {}
if pair[0] == 'Difficulty':
d[key][pair[0]] = pair[1].strip()
if pair[0] == 'Question':
d[key][pair[0]] = pair[1].strip()
if pair[0] == 'Type':
try:
d[key][pair[0]] = pair[1].strip().split('|')[1]
except:
d[key][pair[0]] = pair[1].strip()
# print(d)
# Answer_type
for key, feature in d.items():
wh_word = nltk.word_tokenize(feature['Question'])[0]
ans_type = 'ans_type'
if wh_word == 'Who':
d[key][ans_type] = 'person'
if wh_word == 'Where':
d[key][ans_type] = 'location'
if wh_word == 'What':
d[key][ans_type] = 'thing'
if wh_word == 'When':
d[key][ans_type] = 'time'
if wh_word == 'Why':
d[key][ans_type] = 'reason'
d[key]['wh_word'] = wh_word
'''
# add ROOT word dependency
wh_dep_words = pickle.load(open(root_pickle_file, 'rb'))
for key, value in wh_dep_words.items():
d[key]['wh_dep_word'] = value
'''
return d