Skip to content

Commit

Permalink
add functions for generating common insert queries
Browse files Browse the repository at this point in the history
  • Loading branch information
heinpa committed Aug 19, 2024
1 parent b0ffb99 commit c1f9570
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions qanary_helpers/language_queries.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from qanary_helpers.qanary_queries import select_from_triplestore
from qanary_helpers.qanary_queries import select_from_triplestore, get_text_question_from_uri
import logging


class question_text_with_language:

def __init__(self, uri: str, text: str, lang: str):
self.uri = uri
self.text = text
Expand Down Expand Up @@ -42,7 +42,7 @@ def get_texts_with_detected_language_in_triplestore(triplestore_endpoint: str, g
results = select_from_triplestore(triplestore_endpoint, sparql_find_ld)
for result in results["results"]["bindings"]:
question_uri = result["hasTarget"]["value"]
question_text = get_question_text_from_uri(question_uri, triplestore_endpoint)
question_text = get_text_question_from_uri(question_uri, triplestore_endpoint)
source_texts.append(question_text_with_language(uri=question_uri, text=question_text, lang=lang))

return source_texts
Expand Down Expand Up @@ -75,3 +75,64 @@ def get_translated_texts_in_triplestore(triplestore_endpoint: str, graph_uri: st
source_texts.append(question_text_with_language(question_uri, question_text, lang))

return source_texts


def create_annotation_of_question_translation(graph_uri: str, question_uri: str, translation: str, translation_language: str, app_name: str) -> str:
SPARQLqueryAnnotationOfQuestionTranslation = """
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT {{
GRAPH <{uuid}> {{
?a a qa:AnnotationOfQuestionTranslation ;
oa:hasTarget <{qanary_question_uri}> ;
oa:hasBody "{translation_result}"@{target_lang} ;
oa:annotatedBy <urn:qanary:{app_name}> ;
oa:annotatedAt ?time .
}}
}}
WHERE {{
BIND (IRI(str(RAND())) AS ?a) .
BIND (now() as ?time)
}}
""".format(
uuid=graph_uri,
qanary_question_uri=question_uri,
translation_result=translation,
target_lang=translation_language,
app_name=app_name
)
logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionTranslation}')
return SPARQLqueryAnnotationOfQuestionTranslation


def create_annotation_of_question_language(graph_uri: str, question_uri: str, language: str, app_name: str) -> str:
SPARQLqueryAnnotationOfQuestionLanguage = """
PREFIX qa: <http://www.wdaqua.eu/qa#>
PREFIX oa: <http://www.w3.org/ns/openannotation/core/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
INSERT {{
GRAPH <{uuid}> {{
?b a qa:AnnotationOfQuestionLanguage ;
oa:hasTarget <{qanary_question_uri}> ;
oa:hasBody "{src_lang}"^^xsd:string ;
oa:annotatedBy <urn:qanary:{app_name}> ;
oa:annotatedAt ?time .
}}
}}
WHERE {{
BIND (IRI(str(RAND())) AS ?b) .
BIND (now() as ?time)
}}
""".format(
uuid=graph_uri,
qanary_question_uri=question_uri,
src_lang=language,
app_name=app_name
)

logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionLanguage}')
return SPARQLqueryAnnotationOfQuestionLanguage

0 comments on commit c1f9570

Please sign in to comment.