-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from bounswe/backend-wikidata-api
feat: add wikidata query endpoint Conflicts solved in the other branch, ready to merge.
Showing
9 changed files
with
267 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
|
||
.DS_Store | ||
.env | ||
/keypair | ||
.venv/** | ||
venv | ||
__pycache__/** | ||
*.pyc | ||
**/mydb/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# This helper class is used to interact with the Wikidata API | ||
import requests | ||
import json | ||
from typing import List | ||
|
||
class QleverAPI: | ||
def __init__(self): | ||
self.endpoint_url = "https://qlever.cs.uni-freiburg.de/api/wikidata" | ||
self.params = { | ||
"action": "wbsearchentities", | ||
"format": "json", | ||
"language": "en", | ||
} | ||
|
||
# Send a semantic query to the Wikidata API | ||
def execute_query(self, query): | ||
try: | ||
response = requests.get( | ||
self.endpoint_url, | ||
params={'query': query, 'format': 'json'} | ||
) | ||
response.raise_for_status() | ||
print(response) | ||
return response.json() | ||
except requests.exceptions.RequestException as e: | ||
print("Error:", e) | ||
return None | ||
|
||
def film_pattern_query(self, pattern, limit): | ||
|
||
pattern = pattern.lower() | ||
# remove spaces from the pattern | ||
pattern = pattern.replace(" ", "") | ||
|
||
SPARQL = f""" | ||
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
PREFIX wd: <http://www.wikidata.org/entity/> | ||
PREFIX wdt: <http://www.wikidata.org/prop/direct/> | ||
SELECT DISTINCT ?film ?filmLabel ?filmId WHERE {{ | ||
{{ | ||
SELECT ?film ?filmLabel ?filmId (1 as ?order) WHERE {{ | ||
?film wdt:P31 wd:Q11424; | ||
rdfs:label ?filmLabel. | ||
FILTER(LANG(?filmLabel) = "en") | ||
FILTER(STRSTARTS(REPLACE(LCASE(?filmLabel), " ", ""), "{pattern}")) | ||
}} | ||
}} | ||
UNION | ||
{{ | ||
SELECT ?film ?filmLabel ?filmId (2 as ?order) WHERE {{ | ||
?film wdt:P31 wd:Q11424; | ||
rdfs:label ?filmLabel. | ||
FILTER(LANG(?filmLabel) = "en") | ||
BIND(REPLACE(LCASE(?filmLabel), " ", "") AS ?formattedLabel) | ||
FILTER(REGEX(?formattedLabel, "{pattern}", "i")) | ||
FILTER (!STRSTARTS(REPLACE(LCASE(?filmLabel), " ", ""), "{pattern}")) | ||
}} | ||
}} | ||
}} | ||
ORDER BY ?order | ||
LIMIT {limit} | ||
""" | ||
|
||
print(SPARQL) | ||
|
||
results = self.execute_query(SPARQL) | ||
|
||
return results | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# This helper class is used to interact with the Wikidata API | ||
import requests | ||
import json | ||
from typing import List | ||
|
||
QUERY = """ | ||
SELECT ?item ?itemLabel ?itemDescription ?itemAltLabel WHERE { | ||
?item wdt:P31 wd:Q11424. | ||
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". } | ||
} | ||
""" | ||
|
||
class WikidataAPI: | ||
def __init__(self): | ||
self.endpoint_url = "https://query.wikidata.org/sparql" | ||
self.params = { | ||
"action": "wbsearchentities", | ||
"format": "json", | ||
"language": "en", | ||
} | ||
|
||
# Send a semantic query to the Wikidata API | ||
def execute_query(self, query): | ||
try: | ||
response = requests.get( | ||
self.endpoint_url, | ||
params={'query': query, 'format': 'json'} | ||
) | ||
response.raise_for_status() | ||
print(response) | ||
return response.json() | ||
except requests.exceptions.RequestException as e: | ||
print("Error:", e) | ||
return None | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
======= Films played by dicaprio | ||
{ | ||
"query": "SELECT ?film ?filmLabel WHERE { ?film wdt:P31 wd:Q11424; wdt:P161 wd:Q38111. SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en\". } }" | ||
} | ||
======= | ||
|
||
|
||
===== Film ids starting with the given string | ||
{ | ||
"query": "SELECT DISTINCT ?film ?filmLabel ?filmId WHERE { ?film wdt:P31 wd:Q11424; rdfs:label ?filmLabel. FILTER(STRSTARTS(?filmLabel, \"OPP\")) OPTIONAL { ?film wdt:P345 ?filmId. } SERVICE wikibase:label { bd:serviceParam wikibase:language \"[AUTO_LANGUAGE],en\". } } LIMIT 10" | ||
} | ||
|
||
=== Better version of it | ||
SELECT DISTINCT ?film ?filmLabel ?filmId WHERE { | ||
?film wdt:P31 wd:Q11424; # Instance of film | ||
rdfs:label ?filmLabel. # Label of the film | ||
FILTER(LANG(?filmLabel) = "en") # Filter out non-English labels | ||
FILTER(STRSTARTS(REPLACE(LCASE(?filmLabel), " ", ""), "opportunityk")) # Matches films with labels starting with "A" (case-insensitive and ignoring spaces) | ||
} | ||
LIMIT 3 | ||
==== | ||
|
||
|
||
==== | ||
Final film listing sparql | ||
|
||
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
PREFIX wd: <http://www.wikidata.org/entity/> | ||
PREFIX wdt: <http://www.wikidata.org/prop/direct/> | ||
SELECT DISTINCT ?film ?filmLabel ?filmId WHERE { | ||
{ | ||
SELECT ?film ?filmLabel ?filmId (1 as ?order) WHERE { | ||
?film wdt:P31 wd:Q11424; # Instance of film | ||
rdfs:label ?filmLabel. # Label of the film | ||
FILTER(LANG(?filmLabel) = "en") # Filter out non-English labels | ||
FILTER(STRSTARTS(REPLACE(LCASE(?filmLabel), " ", ""), "oppen")) # Matches labels starting with "oppen" (case-insensitive and ignoring spaces) | ||
} | ||
} | ||
UNION | ||
{ | ||
SELECT ?film ?filmLabel ?filmId (2 as ?order) WHERE { | ||
?film wdt:P31 wd:Q11424; # Instance of film | ||
rdfs:label ?filmLabel. # Label of the film | ||
FILTER(LANG(?filmLabel) = "en") # Filter out non-English labels | ||
FILTER(REGEX(?filmLabel, "oppen", "i")) # Matches labels containing "oppen" (case-insensitive) | ||
FILTER (!STRSTARTS(REPLACE(LCASE(?filmLabel), " ", ""), "oppen")) # Ensure it's not already matched by starts with | ||
} | ||
} | ||
} | ||
ORDER BY ?order | ||
LIMIT 3 | ||
|
||
|
||
==== |