-
Notifications
You must be signed in to change notification settings - Fork 0
/
actors_2_recon_actors_to_base.py
125 lines (94 loc) · 2.76 KB
/
actors_2_recon_actors_to_base.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
import requests
import json
import jellyfish
import sys
import csv
def get_closest_match(x, list_random):
best_match = None
highest_compare_score = 0
for current_string in list_random:
current_score = jellyfish.jaro_winkler_similarity(x, current_string)
if(current_score > highest_compare_score):
highest_compare_score = current_score
best_match = current_string
return { 'match': best_match, 'score': highest_compare_score}
SPARQL_ENDPOINT = 'https://query.semlab.io/proxy/wdqs/bigdata/namespace/wdq/sparql'
# get the labels for everything
sparql = """
SELECT ?item ?itemLabel
WHERE
{
VALUES ?value
{
wd:Q19049
wd:Q1
wd:Q24537
wd:Q19103
wd:Q1804
wd:Q20618
wd:Q19072
}
?item wdt:P1 ?value.
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } # Helps get the label in your language, if not, then en language
}
"""
headers = {
'Accept': 'application/sparql-results+json',
}
params = {
'query' : sparql
}
response = requests.get(
SPARQL_ENDPOINT,
params=params,
headers=headers,
)
data = response.json()
labelLookup={}
base_labels_only = []
for result in data['results']['bindings']:
labelLookup[result['item']['value'].split('/')[-1]] = result['itemLabel']['value']
base_labels_only.append(result['itemLabel']['value'])
datascape_actors = json.load(open('data/actors.json'))
print(datascape_actors)
actors_good_match_file = open('data/actors_good_matchs.csv','w')
csv_writer = csv.writer(actors_good_match_file)
for datascape_id in datascape_actors:
datascape_name = datascape_actors[datascape_id]['name']
# print(datascape_name, get_closest_match(datascape_name,base_labels_only))
best_match = get_closest_match(datascape_name,base_labels_only)
if best_match['score'] >= 0.9:
qid = []
for q in labelLookup:
if labelLookup[q] == best_match['match']:
qid.append(q)
if len(qid) > 1:
print("ERRROR", qid, best_match)
#sys.exit()
sparql = f"""
SELECT ?project ?projectLabel
WHERE
{{
wd:{qid[0]} wdt:P11 ?project.
SERVICE wikibase:label {{ bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }}
}}
"""
headers = {
'Accept': 'application/sparql-results+json',
}
params = {
'query' : sparql
}
response = requests.get(
SPARQL_ENDPOINT,
params=params,
headers=headers,
)
data = response.json()
projects = []
for result in data['results']['bindings']:
if 'projectLabel' in result:
projects.append(result['projectLabel']['value'])
csv_writer.writerow([datascape_id,datascape_name,best_match['match'], qid[0],best_match['score'],",".join(projects)])
print(datascape_name, best_match)
actors_good_match_file.close()