Skip to content

Commit

Permalink
Merge pull request #343 from WDAqua/split-insertQueries-python-compon…
Browse files Browse the repository at this point in the history
…ents

Split insert queries python components
  • Loading branch information
dschiese authored Apr 5, 2024
2 parents 0580c8d + 5e79455 commit ca9b646
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from component.mt_helsinki_nlp import mt_helsinki_nlp_bp
from flask import Flask

version = "0.1.1"
version = "0.1.2"

# default config file (use -c parameter on command line specify a custom config file)
configfile = "app.conf"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def qanary_service():
result = tokenizers[lang].batch_decode(translation, skip_special_tokens=True)[0]

# building SPARQL query TODO: verify this annotation AnnotationOfQuestionTranslation ??
SPARQLquery = """
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#>
Expand All @@ -70,6 +70,27 @@ def qanary_service():
oa:annotatedBy <urn:qanary:{app_name}> ;
oa:annotatedAt ?time .
}}
}}
WHERE {{
BIND (IRI(str(RAND())) AS ?a) .
BIND (now() as ?time)
}}
""".format(
uuid=triplestore_ingraph,
qanary_question_uri=question_uri,
translation_result=result.replace("\"", "\\\""), #keep quotation marks that are part of the translation
target_lang=TARGET_LANG,
app_name=SERVICE_NAME_COMPONENT
)

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 ;
Expand All @@ -78,22 +99,21 @@ def qanary_service():
}}
}}
WHERE {{
BIND (IRI(str(RAND())) AS ?a) .
BIND (IRI(str(RAND())) AS ?b) .
BIND (now() as ?time)
}}
""".format(
uuid=triplestore_ingraph,
qanary_question_uri=question_uri,
translation_result=result.replace("\"", "\\\""), #keep quotation marks that are part of the translation
src_lang=lang,
target_lang=TARGET_LANG,
app_name=SERVICE_NAME_COMPONENT
)

logging.info(f'SPARQL: {SPARQLquery}')
logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionTranslation}')
logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionLanguage}')
# inserting new data to the triplestore
insert_into_triplestore(triplestore_endpoint, SPARQLquery)
insert_into_triplestore(triplestore_endpoint, SPARQLqueryAnnotationOfQuestionTranslation)
insert_into_triplestore(triplestore_endpoint, SPARQLqueryAnnotationOfQuestionLanguage)

return jsonify(request.get_json())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from component.mt_libretranslate import get_languages
from flask import Flask

version = "0.1.1"
version = "0.1.2"

# default config file
configfile = "app.conf"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,40 @@ def qanary_service():
result, _ = translate_input(text, lang)

# building SPARQL query TODO: verify this annotation AnnotationOfQuestionTranslation ??
SPARQLquery = """
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}"@en ;
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=triplestore_ingraph,
qanary_question_uri=question_uri,
translation_result=result.replace("\"", "\\\""), #keep quotation marks that are part of the translation
target_lang=TARGET_LANG,
app_name=SERVICE_NAME_COMPONENT
)

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 ;
Expand All @@ -62,21 +85,21 @@ def qanary_service():
}}
}}
WHERE {{
BIND (IRI(str(RAND())) AS ?a) .
BIND (IRI(str(RAND())) AS ?b) .
BIND (now() as ?time)
BIND (now() as ?time)
}}
""".format(
uuid=triplestore_ingraph,
qanary_question_uri=question_uri,
translation_result=result,
src_lang=lang,
app_name=SERVICE_NAME_COMPONENT
)

logging.info(f'SPARQL: {SPARQLquery}')

logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionTranslation}')
logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionLanguage}')
# inserting new data to the triplestore
insert_into_triplestore(triplestore_endpoint, SPARQLquery)
insert_into_triplestore(triplestore_endpoint, SPARQLqueryAnnotationOfQuestionTranslation)
insert_into_triplestore(triplestore_endpoint, SPARQLqueryAnnotationOfQuestionLanguage)

return jsonify(request.get_json())

Expand Down
2 changes: 1 addition & 1 deletion qanary-component-MT-Python-MBart/component/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from component.mt_mbart_nlp import mt_mbart_nlp_bp
from flask import Flask

version = "0.1.1"
version = "0.1.2"

# default config file
configfile = "app.conf"
Expand Down
32 changes: 26 additions & 6 deletions qanary-component-MT-Python-MBart/component/mt_mbart_nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def qanary_service():


# building SPARQL query TODO: verify this annotation AnnotationOfQuestionTranslation ??
SPARQLquery = """
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#>
Expand All @@ -85,6 +85,27 @@ def qanary_service():
oa:annotatedBy <urn:qanary:{app_name}> ;
oa:annotatedAt ?time .
}}
}}
WHERE {{
BIND (IRI(str(RAND())) AS ?a) .
BIND (now() as ?time)
}}
""".format(
uuid=triplestore_ingraph,
qanary_question_uri=question_uri,
translation_result=result.replace("\"", "\\\""), #keep quotation marks that are part of the translation
target_lang=TARGET_LANG,
app_name=SERVICE_NAME_COMPONENT
)

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 ;
Expand All @@ -93,22 +114,21 @@ def qanary_service():
}}
}}
WHERE {{
BIND (IRI(str(RAND())) AS ?a) .
BIND (IRI(str(RAND())) AS ?b) .
BIND (now() as ?time)
}}
""".format(
uuid=triplestore_ingraph,
qanary_question_uri=question_uri,
translation_result=result.replace("\"", "\\\""), #keep quotation marks that are part of the translation
src_lang=lang,
target_lang=target_lang,
app_name=SERVICE_NAME_COMPONENT
)

logging.info(f'SPARQL: {SPARQLquery}')
logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionTranslation}')
logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionLanguage}')
# inserting new data to the triplestore
insert_into_triplestore(triplestore_endpoint, SPARQLquery)
insert_into_triplestore(triplestore_endpoint, SPARQLqueryAnnotationOfQuestionTranslation)
insert_into_triplestore(triplestore_endpoint, SPARQLqueryAnnotationOfQuestionLanguage)

return jsonify(request.get_json())

Expand Down
2 changes: 1 addition & 1 deletion qanary-component-MT-Python-NLLB/component/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from component.mt_nllb import mt_nllb_bp
from flask import Flask

version = "0.1.1"
version = "0.1.2"

# default config file
configfile = "app.conf"
Expand Down
92 changes: 56 additions & 36 deletions qanary-component-MT-Python-NLLB/component/mt_nllb.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,43 +70,63 @@ def qanary_service():


# building SPARQL query TODO: verify this annotation AnnotationOfQuestionTranslation ??
SPARQLquery = """
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 .
?b a qa:AnnotationOfQuestionLanguage ;
oa:hasTarget <{qanary_question_uri}> ;
oa:hasBody "{src_lang}"^^xsd:string ;
oa:annotatedBy <urn:qanary:{app_name}> ;
oa:annotatedAt ?time .
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 (IRI(str(RAND())) AS ?b) .
BIND (now() as ?time)
}}
""".format(
uuid=triplestore_ingraph,
qanary_question_uri=question_uri,
translation_result=result.replace("\"", "\\\""), #keep quotation marks that are part of the translation
src_lang=lang,
target_lang=target_lang,
app_name=SERVICE_NAME_COMPONENT
)

logging.info(f'SPARQL: {SPARQLquery}')
# inserting new data to the triplestore
insert_into_triplestore(triplestore_endpoint, SPARQLquery)
WHERE {{
BIND (IRI(str(RAND())) AS ?a) .
BIND (now() as ?time)
}}
""".format(
uuid=triplestore_ingraph,
qanary_question_uri=question_uri,
translation_result=result.replace("\"", "\\\""), #keep quotation marks that are part of the translation
target_lang=TARGET_LANG,
app_name=SERVICE_NAME_COMPONENT
)

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=triplestore_ingraph,
qanary_question_uri=question_uri,
src_lang=lang,
app_name=SERVICE_NAME_COMPONENT
)

logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionTranslation}')
logging.info(f'SPARQL: {SPARQLqueryAnnotationOfQuestionLanguage}')
# inserting new data to the triplestore
insert_into_triplestore(triplestore_endpoint, SPARQLqueryAnnotationOfQuestionTranslation)
insert_into_triplestore(triplestore_endpoint, SPARQLqueryAnnotationOfQuestionLanguage)

return jsonify(request.get_json())

Expand Down

0 comments on commit ca9b646

Please sign in to comment.