-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_data_and_query.py
88 lines (71 loc) · 2.39 KB
/
make_data_and_query.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
from rdflib import Namespace, Graph, Literal
from rdflib.namespace import RDFS
# TODO: change instances to dates
def make_fake_data():
gaf = Namespace('http://groundedannotationframework.org/gaf#')
leo = Namespace('http://cltl.nl/leolani/world/')
g = Graph()
g.bind('rdfs', RDFS)
g.bind('gaf', gaf)
jaap = leo.jaap
jaap_2 = leo.jaap_2
bart = leo.bart
name_jaap = Literal("Jaap")
name_bart = Literal("Bart")
instance_1 = Literal("12/08/2021")
instance_2 = Literal("05/07/2021")
instance_3 = Literal("08/08/2021")
g.add((jaap, RDFS.label, name_jaap))
g.add((jaap, gaf.denotedIn, instance_1))
g.add((jaap, gaf.denotedIn, instance_2))
g.add((jaap, gaf.denotedIn, instance_3))
g.add((jaap_2, RDFS.label, name_jaap))
g.add((jaap_2, gaf.denotedIn, instance_1))
g.add((bart, RDFS.label, name_bart))
g.add((bart, gaf.denotedIn, instance_2))
return g
def add_data(graph, name):
gaf = Namespace('http://groundedannotationframework.org/gaf#')
leo = Namespace('http://cltl.nl/leolani/world/')
graph.bind('rdfs', RDFS)
graph.bind('gaf', gaf)
def pop_query(graph, q_param):
q = """
prefix gaf: <http://groundedannotationframework.org/gaf#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?ent (COUNT(DISTINCT ?e) as ?num_mentions) where{
?ent rdfs:label "%s".
?ent gaf:denotedIn ?e.
}
group by ?ent
order by DESC(COUNT(DISTINCT ?e))
"""
result_list = []
for row in graph.query(q % q_param):
uri = row[0][0:]
occurrences = row[1][0:]
result_list.append((uri, occurrences))
return result_list
def rec_query(graph, q_param):
q = """
prefix gaf: <http://groundedannotationframework.org/gaf#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select distinct ?inst ?s
where{
?s rdfs:label "%s".
?s gaf:denotedIn ?inst
}
group by ?inst
ORDER BY DESC (?s)
"""
result_list = []
for row in graph.query(q % q_param):
# uri = row[0][0:]
result_list.append(row)
return result_list
if __name__ == "__main__":
data = make_fake_data()
popularity = pop_query(data, "Jaap")
recency = rec_query(data, "Jaap")
print(popularity)
print(recency)