예제 #1
0
def get_graph_from_wikidata_id(wikidata_id, central_item):
    url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
    data = requests.get(url,
                        params={
                            'query': _query % wikidata_id,
                            'format': 'json'
                        })
    if data.status_code != 200:
        time.sleep(40)
        data = requests.get(url,
                            params={
                                'query': _query % wikidata_id,
                                'format': 'json'
                            })
        if data.status_code != 200:
            time.sleep(160)
            data = requests.get(url,
                                params={
                                    'query': _query % wikidata_id,
                                    'format': 'json'
                                })
    data = data.json()
    print("Reading")
    sys.stdout.flush()
    triplets = ""
    graph_set = set()
    for item in data['results']['bindings']:
        try:
            from_item = wikidata_items.translate_from_url(wikidata_id)
            relation = wikidata_items.translate_from_url(item['rel']['value'])
            to_item = wikidata_items.translate_from_url(item['item']['value'])
            graph_set.add(relation)
            graph_set.add(to_item)
        except Exception as e:
            pass
        """try:
            from_item = wikidata_items.translate_from_url(item['item']['value'])
            relation = wikidata_items.translate_from_url(item['rel2']['value'])
            to_item = wikidata_items.translate_from_url(item['to_item']['value'])
            #triplets.append((from_item, relation, to_item))
            graph_set.add(relation)
            graph_set.add(to_item)
        except Exception as e:
            #print("Here ", e)
            pass"""
        #sys.exit()
    #triplets = sorted(list(set(triplets)))
    triplets = ' '.join(graph_set)
    if triplets == "":
        raise RuntimeError("This graph contains no suitable triplets.")
    return triplets
예제 #2
0
def get_triplets_for_word_2_hops(word):
    url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
    triplets = []
    for query in [query_nn_forw, query_nn2_forw]:
        data = requests.get(url,
                            params={
                                'query': query % word,
                                'format': 'json'
                            }).json()
        for item in data['results']['bindings']:
            try:
                to_item = wikidata_items.translate_from_url(
                    item['item1']['value'])  # + '|' + item['item1']['value']
                relation = wikidata_items.translate_from_url(
                    item['rel']['value'])  # + '|' + item['rel']['value']
                from_item = wikidata_items.translate_from_url(
                    item['item0']['value'])  #  + '|' + item['item0']['value']
                triplets.append((from_item, relation, to_item))
            except:
                pass
    return triplets
예제 #3
0
def get_graph_from_wikidata_id(wikidata_id, central_item):
    url = 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
    data = requests.get(url,
                        params={
                            'query': _query % wikidata_id,
                            'format': 'json'
                        }).json()
    triplets = []
    for item in data['results']['bindings']:
        try:
            from_item = wikidata_items.translate_from_url(wikidata_id)
            relation = wikidata_items.translate_from_url(item['rel']['value'])
            to_item = wikidata_items.translate_from_url(item['item']['value'])
            triplets.append((from_item, relation, to_item))
        except:
            pass
        try:
            from_item = wikidata_items.translate_from_url(
                item['item']['value'])
            relation = wikidata_items.translate_from_url(item['rel2']['value'])
            to_item = wikidata_items.translate_from_url(
                item['to_item']['value'])
            triplets.append((from_item, relation, to_item))
        except:
            pass
    triplets = sorted(list(set(triplets)))
    if not triplets:
        raise RuntimeError("This graph contains no suitable triplets.")
    return get_adjacency_matrices_and_vectors_given_triplets(
        triplets, central_item, _model)