Created
June 9, 2017 12:31
-
-
Save Vesihiisi/8769c6f9d3083bc1c2dc67e93ed3484b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import csv | |
import wikidataStuff.wdqsLookup as lookup | |
def query_wd(query): | |
items = [] | |
data = lookup.make_simple_wdqs_query(query, verbose=False) | |
for x in data: | |
line = { | |
"item": lookup.sanitize_wdqs_result(x['item']), | |
"NVRID": x["value"] | |
} | |
items.append(line) | |
return items | |
def get_data_from_csv_file(filename): | |
with open(filename, "r") as f_obj: | |
reader = csv.DictReader(f_obj, delimiter=',') | |
csv_data = list(reader) | |
return csv_data | |
def dump_list_to_file(some_list, filename): | |
f = open(filename, 'w') | |
for item in some_list: | |
f.write(item + "\n") | |
def make_qs_coords(no_coords, coords): | |
qs_lines = [] | |
qs_template = "{}\tP625\t@{}/{}" | |
coords = get_data_from_csv_file(coords) | |
for nid in no_coords: | |
item = nid["item"] | |
id_no = nid["NVRID"] | |
latitude = [x["lat"] for x in coords if x["NVRID"] == id_no][0] | |
longitude = [x["lon"] for x in coords if x["NVRID"] == id_no][0] | |
qs_lines.append(qs_template.format(item, latitude, longitude)) | |
return qs_lines | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--infile') | |
parser.add_argument('--outfile') | |
args = parser.parse_args() | |
query_nid_no_coord = ("SELECT ?item ?value" | |
"{ ?item wdt:P3613 ?value . OPTIONAL " | |
"{ ?item wdt:P625 ?dummy } " | |
"FILTER ( !bound(?dummy) ) .}") | |
no_coords = query_wd(query_nid_no_coord) | |
coords = args.infile | |
qs_file = args.outfile | |
quickstatements = make_qs_coords(no_coords, coords) | |
dump_list_to_file(quickstatements, qs_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small change to lines 38-40 to make it not crash: