-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
56 lines (48 loc) · 1.93 KB
/
application.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 os
import nltk
import client
import sys
from nltk.tree import ParentedTree
from parser import Parser
from components.property_finder import PropertyFinder
class Application(object):
def __init__(self):
self._nlp = client.StanfordNLP()
nltk.data.path.append(str(os.path.abspath(os.path.dirname(os.path.abspath(__file__))+'/../nltk_data')))
pass
def get_answer(self, question):
result = self._nlp.parse(question)
try:
tree = ParentedTree.fromstring(result['sentences'][0]['parsetree'])
except IndexError or KeyError:
return None
parser = Parser()
parser.run(tree)
answers = parser.answers
del parser
return answers
def run(self):
while True:
print "Enter question: "
question = sys.stdin.readline()
answers = self.get_answer(question)
for answer in answers:
print("SCORE: " + str(answer.score)+" ==========================")
print("Label: "+str(answer.data['label'].encode('utf-8')))
try:
print("Description: "+str(answer.data['description'].encode('utf-8')))
except KeyError:
pass
print("Properties:")
for prop in answer.data['statements']:
try:
print("\t"+str(prop.encode('utf-8')))
for value in answer.data['statements'][prop]['values']:
if value['data']._type == 'wikibase-entityid':
print("\t\t linked object %s" % value['data']._value['numeric-id'])
continue
tmp = value['data']['value']
print("\t\t"+str(tmp['statements']))
except Exception:
pass
print("\n\n")