File: construct_query_wikidata.py

package info (click to toggle)
rudof 0.1.146%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 10,048 kB
  • sloc: python: 1,288; makefile: 32; sh: 1
file content (29 lines) | stat: -rw-r--r-- 737 bytes parent folder | download | duplicates (2)
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
from pyrudof import Rudof, RudofConfig, RDFFormat, QueryResultFormat 

endpoint = "https://query.wikidata.org/sparql"

sparql_query = """
PREFIX wd:  <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX :    <http://example.org/>

CONSTRUCT { 
   ?p a     :Person ;
      :name ?person ;
      :occupation ?occupation
} WHERE {
    ?p wdt:P31 wd:Q5 ;   
          wdt:P106 ?o ;
          rdfs:label ?person ;
          wdt:P19 wd:Q14317 .
  ?o rdfs:label ?occupation 
  FILTER (lang(?person) = "en" && lang(?occupation) = "en")   
}
LIMIT 10
"""
rudof = Rudof(RudofConfig())
rudof.use_endpoint(endpoint)

result = rudof.run_query_construct_str(sparql_query, QueryResultFormat.Turtle)

print(result)