/
Compare_subject_planet.py
53 lines (44 loc) · 1.86 KB
/
Compare_subject_planet.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
# ###################################################
# Explore the direct inked entities between keywords
# Here direct linked entities states the one edge link
# between the two set of Keywords
# Input : Two keywords
# Output : Set of Linked Edges between the two keywords
# The linked edges are the properties between the
# two keywords.
# @ Author : Rituraj Singh
# rituraj.singh@irisa.fr
# Created Time : 18 July 2018, 17 : 45 (GMT + 2)
# At: INRIA/IRISA, Rennes, France
####################################################
from SPARQLWrapper import SPARQLWrapper, JSON
import numpy as np
sparql = SPARQLWrapper("http://dbpedia.org/sparql")
# print(query_str)
# Q1 : dbpedia:India dcterms:subject ?super
max_label_traverse = 2;
def compare_planet_labels(first_keyword, second_keyword, traverse_label):
query_str = """
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dbc: <http://dbpedia.org/resource/Category:>
select distinct ?super where {
?super (^skos:broader){0,""" + traverse_label + """} <""" + first_keyword + """>,<""" + second_keyword + """>
}"""
# print(query_str)
sparql.setQuery(query_str);
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
# print(results);
my_key = [];
for result in results['results']['bindings']:
# print(result['super']['value'])
my_key.append(result['super']['value'])
my_keywords = np.array(my_key)
return my_keywords;
def compare_planet_subjects(first_keyword, second_keyword):
for label_val in range(1, int(max_label_traverse)):
common_satellites = compare_planet_labels(first_keyword, second_keyword, str(label_val));
# print('value of Common Satellite : ', common_satellites);
if common_satellites.size != 0:
return label_val;