-
Notifications
You must be signed in to change notification settings - Fork 30
/
qa2hypo.py
134 lines (115 loc) · 4.42 KB
/
qa2hypo.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
import re
# regex
regexs = {
1: re.compile("^Where is (\w+)\?$"),
2: re.compile("^Where is the (\w+)\?$"),
3: re.compile("^Where was ([\w ]+) before the (\w+)\?$"),
4.1: re.compile("^What is ([\w ]+) (\w+) of\?$"),
4.2: re.compile("^What is (\w+) of the (\w+)\?$"),
5.1: re.compile("^What did (\w+) give to (\w+)\?$"),
5.2: re.compile("^Who gave the (\w+) to (\w+)\?$"),
5.3: re.compile("^Who did (\w+) give the (\w+) to\?$"),
5.4: re.compile("^Who gave the (\w+)\?$"),
5.5: re.compile("^Who received the (\w+)\?$"),
6: re.compile("Is (\w+) in the (\w+)\?$"),
7: re.compile("^How many objects is (\w+) carrying\?$"),
8: re.compile("^What is (\w+) carrying\?$"),
# 9: same as 6
# 10: same as 6
# 11: same as 1
# 12: same as 1
# 13: same as 13
# 14: same as 3
15: re.compile("^What is (\w+) afraid of\?$"),
16: re.compile("^What color is (\w+)\?$"),
17: re.compile("^Is the ([\w+ ]+) (below|above|to the left of|to the right of) the ([\w ]+)\?$"),
18.1: re.compile("^Is the ([\w ]+) (bigger|smaller) than the ([\w ]+)\?$"),
18.2: re.compile("^Does the ([\w ]+) fit in the ([\w ]+)\?$"),
19: re.compile("^How do you go from the (\w+) to the (\w+)\?$"),
20.1: re.compile("^Where will (\w+) go\?$"),
20.2: re.compile("^Why did (\w+) go to the (\w+)\?$"),
20.3: re.compile("^Why did (\w+) get the (\w+)\?$"),
}
class C06(object):
@staticmethod
def format(containee, container, answer=None):
if answer == "yes":
return "{} is in the {}.".format(containee, container)
elif answer == "no":
return "{} is not in the {}.".format(containee, container)
elif answer == "maybe":
return "{} is maybe in the {}.".format(containee, container)
raise Exception("Unrecognized answer: {}".format(answer))
class C08(object):
@staticmethod
def format(subject, answer=None):
if answer == "nothing":
return "{} is carrying nothing.".format(subject)
else:
return "{} is carrying the {answer}.".format(subject, answer=answer)
class C18_1(object):
@staticmethod
def format(subject, adjective, object, answer=None):
if answer == "yes":
return "The {} is {} than the {}.".format(subject, adjective, object)
elif answer == "no":
return "The {} is not {} than the {}.".format(subject, adjective, object)
raise Exception("Unrecognized answer: {}".format(answer))
class C18_2(object):
@staticmethod
def format(subject, object, answer=None):
if answer == "yes":
return "The {} fits in the {}.".format(subject, object)
elif answer == "no":
return "The {} does not fit in the {}.".format(subject, object)
raise Exception("Unrecognized answer: {}".format(answer))
out_strings = {
1: "{0} is in the {answer}.",
2: "The {0} is in the {answer}.",
3: "The {0} is in the {answer}.",
4.1: "The {0} is {1} of the {answer}.",
4.2: "The {answer} is {0} of the {1}.",
5.1: "{0} gave the {answer} to {1}.",
5.2: "{answer} gave the {0} to {1}.",
5.3: "{0} gave the {1} to {answer}.",
5.4: "{0} gave the {answer}.",
5.5: "{answer} received the {0}.",
6: C06,
7: "{0} is carrying {answer} objects.",
8: C08,
# 9: same as 6
# 10: same as 6
# 11: same as 1
# 12: same as 1
# 13: same as 13
# 14: same as 3
15: "{0} is afraid of {answer}.",
16: "The color of {0} is {answer}.",
17: "The {0} is {1} the {2}.",
18.1: C18_1,
18.2: C18_2,
19: "You go {answer} from the {0} to the {1}.",
20.1: "{0} will go to the {answer}.",
20.2: "{0} went to the {1} because he is {answer}.",
20.3: "{0} got the {1} because he is {answer}."
}
def apply(regex, string, question, answer):
result = regex.match(question)
if result:
return string.format(*result.groups(), answer=answer).capitalize()
return result
def qa2hypo(question, answer):
question = question.lstrip().rstrip()
answer = answer.lstrip().rstrip()
for task, regex in regexs.items():
string = out_strings[task]
result = apply(regex, string, question, answer)
if result:
return result
raise Exception("Unknown question format: {}".format(question))
def main():
question = "Where is Mary?"
answer = "office"
print(qa2hypo(question, answer))
if __name__ == "__main__":
main()