In [14]:
prefixes = """PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX sem: <http://semanticweb.cs.vu.nl/2009/11/sem/>
PREFIX nwr: <http://www.newsreader-project.eu/>"""
endpoint = "http://hlt-services7.fbk.eu:8890/sparql"
In [15]:
import rdflib
import itertools
import time
from SPARQLWrapper import SPARQLWrapper2, JSON
sparql = SPARQLWrapper2(endpoint)
sparql.setQuery(prefixes + """
SELECT DISTINCT ?actor
WHERE {
?entity sem:hasActor ?actor .
}
limit 10
""")
#sparql.setReturnFormat(JSON)
ret = sparql.query()
print ret.variables
for binding in ret.bindings :
# each binding is a dictionary. Let us just print the results
print binding['actor'].value
In [4]:
sparql.setQuery(prefixes + """
SELECT ?org (count(?org) as ?numOrg)
WHERE {
?org a nwr:person .
?org a <http://dbpedia.org/ontology/Person> .
?org rdfs:label ?orgLabel .
}
GROUP BY ?org
ORDER BY DESC(?numOrg)
LIMIT 100
""")
#sparql.setReturnFormat(JSON)
ret = sparql.query()
print ret.variables
plot = []
dlen = len("http://dbpedia.org/resource")
print dlen
for binding in ret.bindings :
# each binding is a dictionary. Let us just print the results
org = binding['org'].value
num = binding['numOrg'].value
org_sm = org[dlen+1:len(org)]
plot.append((org_sm, int(num)))
print plot
In [12]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpld3 import enable_notebook
enable_notebook()
df = pd.DataFrame(plot, columns=["org","num"])
df.plot(kind='bar')
Out[12]:
In [6]:
import networkx as nx
sparql.setQuery(prefixes + """
SELECT ?org ?org2 (count(?evt) as ?numEvt)
WHERE {
?evt a sem:Event .
?evt sem:hasActor ?org .
?org a nwr:organization .
?org a <http://schema.org/Organization>.
?evt sem:hasActor ?org2 .
?org2 a nwr:organization .
?org2 a <http://schema.org/Organization>.
FILTER(?org != ?org2) .
}
ORDER BY DESC(?numEvt)
LIMIT 100
""")
#sparql.setReturnFormat(JSON)
ret = sparql.query()
print ret.variables
g=nx.Graph()
dlen = len("http://dbpedia.org/resource")
print dlen
for binding in ret.bindings :
# each binding is a dictionary. Let us just print the results
org = binding['org'].value
org = org[dlen+1:len(org)]
org2 = binding['org2'].value
org2 = org2[dlen+1:len(org2)]
#print num
num = int(binding['numEvt'].value)
g.add_edge(org, org2, weight=num)
#print g.nodes()
In [7]:
#for line in nx.generate_edgelist(g, data=True):
# print(line)
In [11]:
from mpld3 import enable_notebook
enable_notebook()
pos=nx.spring_layout(g, k=10.0, iterations=100, weight='weight')
nx.draw_networkx_nodes(g,pos,node_size=10,node_color='b', alpha=.2)
nx.draw_networkx_edges(g,pos,edge_color='b',alpha=.4)
nx.draw_networkx_labels(g,pos,font_size=10,font_family='sans-serif')
#plt.grid(color='white', linestyle='solid')
plt.axis('off')
plt.show()
#nx.draw_spring(g,node_size=0,edge_color='b',alpha=.2,font_size=10)
This is a graph