-
Notifications
You must be signed in to change notification settings - Fork 1
/
Python_SPARQL.py
68 lines (56 loc) · 2 KB
/
Python_SPARQL.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
__author__ = "Ramona Kuehn"
__license__ = "MIT"
__version__ = "1.0."
__email__ = "[email protected]"
"""
This file shows exemplary how to use SPARQL queries in Python on the GRhOOT ontology,
the ontology for rhetorical figures in German.
Examples are the competency question
Q3: figures that are neither tropes not figures of speech
Q4: looking for rhetorical figures that have their defining element in a word.
Q5: looking for rhetorical figures where a letter is omitted.
"""
import rdflib
g = rdflib.Graph()
g.parse('grhoot.owl', format='application/rdf+xml')
ont = rdflib.Namespace('https://ramonakuehn.de/')
g.bind('ont', ont)
# only figure of speech and figure of thought with position beginning
competency_question_q3 = """
SELECT distinct ?figur
WHERE {
?figur ont:istRhetorischeGruppe ?gruppe .
?gruppe rdfs:label ?gruppenName .
?figur ont:istInPosition ?position .
?position rdfs:label ?posName .
Filter (?gruppenName != "Tropenfigur" && ?gruppenName != "Konstruktionsfigur" && ?posName = "Anfang") }
"""
competency_question_q4 = """
SELECT distinct ?figur
WHERE {
?figur ont:liegtImBereich ?bereich .
?bereich rdfs:label ?nameBereich .
Filter (?nameBereich = "Wort" ) }
"""
competency_question_q5 = """
SELECT distinct ?figur
WHERE {
?figur ont:wirdWeggelassen ?element .
?element rdfs:label ?name .
Filter (?name = "Buchstabe" ) }
"""
print("Competency Question Q3: Neither a trope nor a figure of construction, but position at the beginning")
result = g.query(competency_question_q3)
print(f"Number of matching rhetorical figures: {len(result)}")
for row in result:
print(row)
print("Competency Question Q4: In the area of a word")
result = g.query(competency_question_q4)
print(f"Number of matching rhetorical figures: {len(result)}")
for row in result:
print(row)
print("\nCompetency Question Q5: Where a letter is omitted")
result = g.query(competency_question_q5)
print(f"Number of matching rhetorical figures: {len(result)}")
for row in result:
print(row)