From c1f9570d181c037c1fc82ba9da938c43a0fcd3de Mon Sep 17 00:00:00 2001 From: heinpa Date: Tue, 20 Aug 2024 01:35:45 +0200 Subject: [PATCH] add functions for generating common insert queries --- qanary_helpers/language_queries.py | 67 ++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/qanary_helpers/language_queries.py b/qanary_helpers/language_queries.py index c9a5346..1f91a7f 100644 --- a/qanary_helpers/language_queries.py +++ b/qanary_helpers/language_queries.py @@ -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 @@ -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 @@ -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: + PREFIX oa: + PREFIX xsd: + + INSERT {{ + GRAPH <{uuid}> {{ + ?a a qa:AnnotationOfQuestionTranslation ; + oa:hasTarget <{qanary_question_uri}> ; + oa:hasBody "{translation_result}"@{target_lang} ; + oa:annotatedBy ; + 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: + PREFIX oa: + PREFIX xsd: + + INSERT {{ + GRAPH <{uuid}> {{ + ?b a qa:AnnotationOfQuestionLanguage ; + oa:hasTarget <{qanary_question_uri}> ; + oa:hasBody "{src_lang}"^^xsd:string ; + oa:annotatedBy ; + 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