-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathADBProjectPart2.py
More file actions
95 lines (82 loc) · 3.95 KB
/
Copy pathADBProjectPart2.py
File metadata and controls
95 lines (82 loc) · 3.95 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
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
import json
import re
import urllib
# This code is used for removing the cleaning up the user query before searching the FreeBaseAPI.
def removePunctuation(str):
return re.sub(ur"[\,\|\-\?\[\]\{\}\(\)\"\:\;\!\@\#\$\ %\^\&\*\>\<]|(\.$)|(\.\.\.)", ' ', str)
def QuestionAnswering(key, fullQuery):
# Process results only if the query matches the result Who created [xxx]?
q = fullQuery.strip().split(' ')
if len(q)>2 and q[0].lower() == 'who' and q[1].lower() == 'created':
# Find the query
search = removePunctuation(' '.join(fullQuery.split(' ')[2:]))
output = {}
api_key = key
service_url = 'https://www.googleapis.com/freebase/v1/mqlread'
# Query for organization founders
query = [{"organizations_founded":[{"a:name": None,"name~=": search}],"id":None,"name":None,"type":"/organization/organization_founder"}]
params = {
'query': json.dumps(query),
'key': api_key
}
url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib.urlopen(url).read().decode('utf-8'))
# If the api returns results add it to output.
for founders in response['result']:
founderDict = {}
if founders['name'] not in output:
output[founders['name']] = {"organizations":[],"books":[]}
for organization in founders['organizations_founded']:
output[founders['name']]["organizations"].append(organization['a:name'])
# Query for book authors
query = [{"works_written":[{"a:name":None,"name~=":search}],"id":None,"name":None,"type":"/book/author"}]
params = {
'query': json.dumps(query),
'key': api_key
}
url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib.urlopen(url).read().decode('utf-8'))
# If the api returns results add it to output.
for authors in response['result']:
authorDict = {}
if authors['name'] not in output:
output[authors['name']] = {"organizations":[],"books":[]}
for books in authors['works_written']:
output[authors['name']]["books"].append(books['a:name'])
# Check if some valid results were returned by Freebase API.
if len(output)>0:
print("\n")
for key in sorted(output):
orgs = output[key]["organizations"]
numOrganizations = len(orgs)
if numOrganizations > 0:
outputString = key + ' (as BusinessPerson) created '
index = 0
for org in orgs:
if(index > 0 and (index + 1) < numOrganizations):
outputString = outputString + ', '
elif(index > 0):
outputString = outputString + ' and '
outputString = outputString + org
index = index + 1
print(outputString)
books = output[key]["books"]
numBooks = len(books)
if numBooks > 0:
outputString = key + ' (as Author) created '
index = 0
for book in books:
if(index > 0 and (index + 1) < numBooks):
outputString = outputString + ', '
elif(index > 0):
outputString = outputString + ' and '
outputString = outputString + book
index = index + 1
print(outputString)
print("\n========================================================================\n")
else:
print("No results found.")
else:
print("Query: " + fullQuery + ", not in the correct format.")
#if __name__=='__main__':
# QuestionAnswering("XXXXXX","Who created")