forked from chetannaik/question_answering_system
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entailment.py
executable file
·56 lines (41 loc) · 1.26 KB
/
entailment.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
import json
import requests
from nltk import tokenize
from memoized import Memoize
def get_list(sen):
"""Converts the sentence into a List such that it can be used as an POST
request json blob data.
Args:
sen : a string
Returns:
res: a list of list containing words and tags.
"""
res = list()
count = 0
for word in tokenize.word_tokenize(sen):
l = [word, "Any", count]
count += 1
res.append(l)
return res
@Memoize
def get_ai2_textual_entailment(t, h):
"""Returns the output of POST request to AI2 textual entailment service
Args:
t, h : text and hypothesis (two strings)
Returns:
req : A text version of json response.
"""
text = get_list(t)
hypothesis = get_list(h)
data = {"text": text, "hypothesis": hypothesis}
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}
url = 'http://localhost:8191/api/entails'
req = requests.post(url, headers=headers, data=json.dumps(data))
return req.json()
def main():
text = raw_input("Enter the text: ")
hypothesis = raw_input("Enter the hypothesis: ")
print "Response: "
print get_ai2_textual_entailment(text, hypothesis)
if __name__ == "__main__":
main()