-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathanswer
executable file
·105 lines (78 loc) · 2.9 KB
/
answer
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
#!/usr/bin/env python
import sys
import os.path
import subprocess
from queditdistance import processquestions
debug = '2>/dev/null'
def init():
global debug
args = sys.argv[1:]
valid_args = True
if len(args) < 2:
valid_args = False
print "Usage: ./answer <path/to/article> <path/to/questions>"
sys.exit(-1)
for arg in args[:2]:
if not os.path.isfile(arg):
valid_args = False
print arg, "is not a valid file"
if valid_args:
if len(args) == 3 and args[2] == '--debug':
debug = ''
return args[0], args[1]
else:
sys.exit(1)
def run_question_asker(path):
cmd = './scripts/run.sh question-asker --debug %s < %s' % (debug, path)
output = subprocess.check_output(cmd, shell=True)
questions = []
answers = []
for line in output.split('\n'):
if line.strip() == '':
continue
question, sentence, answer, score = line.strip().split('\t')
questions.append(question)
answers.append(sentence)
return questions, answers
def run_question_answerer(data):
cmd = './scripts/run.sh question-answerer %s' % debug
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate('\n'.join(data))
return stdout
def parse_output(output):
questions = []
postags = []
dependencies = []
sstags = []
for part in output.strip().split('\n\n'):
question, pos, deps, ss = part.split('\n')
questions.append(question);
postags.append(pos[1:-1].split(', '))
over_split = deps[1:-1].split(', ')
dep_list = []
current_dep = ''
for dep_part in over_split:
if current_dep == '':
current_dep = dep_part + ', '
elif dep_part[-1] == ')':
dep_list.append(current_dep + dep_part)
current_dep = ''
else:
current_dep += dep_part + ', '
dependencies.append(dep_list)
sstags.append(ss[1:-1].split(', '))
return {
'questions': questions,
'postags': postags,
'deptags': dependencies,
'sstags': sstags
}
article_path, questions_path = init()
generated_questions, generated_answers = run_question_asker(article_path)
with open(questions_path) as asked_questions_file:
asked_questions = asked_questions_file.read().split('\n')
generated_questions_output = run_question_answerer(generated_questions)
generated_questions_data = parse_output(generated_questions_output)
asked_questions_output = run_question_answerer(asked_questions)
asked_questions_data = parse_output(asked_questions_output)
processquestions(asked_questions_data, generated_questions_data, generated_answers)