Skip to content

Instantly share code, notes, and snippets.

@simongray
Created September 24, 2021 13:40
Show Gist options
  • Save simongray/c0d756104ba581d4438c69acdaa20395 to your computer and use it in GitHub Desktop.
Save simongray/c0d756104ba581d4438c69acdaa20395 to your computer and use it in GitHub Desktop.
Query the DanNet pre-release dataset using SPARQL
import requests
from rdflib import Graph
# Fetch data
url = 'https://github.com/kuhumcst/DanNet/releases/download/v2021.9.24/dannet-expanded.ttl'
r = requests.get(url)
open('dannet-expanded.ttl', 'wb').write(r.content)
# Build a graph
g = Graph()
g.parse('dannet-expanded.ttl')
# Query the graph
usage_query = """
SELECT ?sense ?usage_str
WHERE {
?sense ontolex:usage ?usage .
?usage rdf:value ?usage_str .
}
"""
result = g.query(usage_query)
# Print results
for row in result:
print(f"{row.sense} -> {row.usage_str}")
@simongray
Copy link
Author

Requires requests and rdflib packages to be installed, e.g run pip install requests rdflib beforehand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment