|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import requests |
| 5 | +import itertools |
| 6 | +from collections import defaultdict |
| 7 | +from graphs import Graph, io |
| 8 | + |
| 9 | + |
| 10 | +# getting all the results will time out, so we do paging |
| 11 | +PAGE_SIZE = 10000 |
| 12 | +SPARQL_ENDPOINT = "https://query.wikidata.org/sparql" |
| 13 | +USER_AGENT = "tud-kbs-example-subgraph-extractor/0.0.1 (https://github.com/knowsys/Course-Knowledge-Graphs/)" |
| 14 | +# first, in a subquery, get the v/w pairs in a stable order and |
| 15 | +# retrieve a page of those. then find the labels for these results. |
| 16 | +QUERY = """#TOOL:tud-kbs-example-subgraph-extractor |
| 17 | +SELECT DISTINCT ?v ?vLabel ?w ?wLabel WHERE {{ |
| 18 | + {{ SELECT DISTINCT ?v ?w WHERE {{ |
| 19 | + ?v wdt:P1344 ?tournament . |
| 20 | + ?tournament ^wdt:P2522 ?w . |
| 21 | + }} ORDER BY ASC(?v) ASC(?w) LIMIT {limit} OFFSET {offset} }} |
| 22 | + SERVICE wikibase:label {{ |
| 23 | + bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en" . |
| 24 | + ?v rdfs:label ?vLabel . |
| 25 | + ?w rdfs:label ?wLabel . |
| 26 | + }} |
| 27 | +}} |
| 28 | +""" |
| 29 | + |
| 30 | + |
| 31 | +def counter(): |
| 32 | + c = itertools.count(1) |
| 33 | + |
| 34 | + def step(): |
| 35 | + return next(c) |
| 36 | + |
| 37 | + return step |
| 38 | + |
| 39 | + |
| 40 | +def paged(page): |
| 41 | + return QUERY.format(limit=PAGE_SIZE + 1, offset=(PAGE_SIZE + 1) * page) |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + parser = argparse.ArgumentParser( |
| 46 | + description=( |
| 47 | + "Extract tournament winner/" "participant subgraph from" "Wikidata" |
| 48 | + ) |
| 49 | + ) |
| 50 | + parser.add_argument("output", help="path to output graph") |
| 51 | + parser.add_argument("dict", help="path to output dictionary") |
| 52 | + args = parser.parse_args() |
| 53 | + |
| 54 | + ids = defaultdict(counter()) |
| 55 | + labels = {} |
| 56 | + edges = set([]) |
| 57 | + done = False |
| 58 | + page = 0 |
| 59 | + |
| 60 | + while not done: |
| 61 | + print("-!- getting page {}".format(page)) |
| 62 | + request = requests.get( |
| 63 | + SPARQL_ENDPOINT, |
| 64 | + params={"query": paged(page), "format": "json"}, |
| 65 | + headers={"user-agent": USER_AGENT}, |
| 66 | + ) |
| 67 | + request.raise_for_status() |
| 68 | + results = request.json()["results"]["bindings"] |
| 69 | + done = len(results) <= PAGE_SIZE |
| 70 | + page += 1 |
| 71 | + |
| 72 | + for binding in results: |
| 73 | + v = ids[binding["v"]["value"]] |
| 74 | + labels[v] = binding["vLabel"]["value"] |
| 75 | + w = ids[binding["w"]["value"]] |
| 76 | + labels[w] = binding["wLabel"]["value"] |
| 77 | + |
| 78 | + edges |= {(w, v)} |
| 79 | + |
| 80 | + graph = Graph(vertices=ids.values()) |
| 81 | + |
| 82 | + for (w, v) in edges: |
| 83 | + graph.add_edge(w, v) |
| 84 | + |
| 85 | + with open(args.output, "w") as metis: |
| 86 | + io.write_metis_graph(metis, graph) |
| 87 | + |
| 88 | + with open(args.dict, "w") as dictionary: |
| 89 | + for vertex_id, label in labels.items(): |
| 90 | + print('{},"{}"'.format(vertex_id, label), file=dictionary) |
0 commit comments