-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add common functionality for language components
- Loading branch information
Showing
3 changed files
with
94 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
from qanary_helpers.qanary_queries import select_from_triplestore | ||
|
||
|
||
class question_text_with_language: | ||
|
||
def __init__(self, uri: str, text: str, lang: str): | ||
self.uri = uri | ||
self.text = text | ||
self.lang = lang | ||
|
||
def get_uri(self): | ||
return self.uri | ||
|
||
def get_text(self): | ||
return self.text | ||
|
||
def get_language(self): | ||
return self.lang | ||
|
||
|
||
def get_texts_with_detected_language_in_triplestore(triplestore_endpoint: str, graph_uri: str, lang: str) -> list[question_text_with_language]: | ||
source_texts = list() | ||
sparql_find_ld = """ | ||
PREFIX qa: <http://www.wdaqua.eu/qa#> | ||
PREFIX oa: <http://www.w3.org/ns/openannotation/core/> | ||
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> | ||
SELECT * | ||
FROM <{graph}> | ||
WHERE {{ | ||
?annotationId a qa:AnnotationOfQuestionLanguage . | ||
?annotationId oa:hasTarget ?hasTarget ; | ||
oa:hasBody ?hasBody ; | ||
oa:annotatedBy ?annotatedBy ; | ||
oa:annotatedAt ?annotatedAt . | ||
FILTER(STR(?hasBody) = {lang}) | ||
}} | ||
""".format( | ||
graph = graph_uri, | ||
lang=lang | ||
) | ||
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) | ||
source_texts.append(question_text_with_language(uri=question_uri, text=question_text, lang=lang)) | ||
|
||
return source_texts | ||
|
||
|
||
def get_translated_texts_in_triplestore(triplestore_endpoint: str, graph_uri: str, lang: str) -> list[question_text_with_language]: | ||
source_texts = list() | ||
sparql_find_ld = """ | ||
PREFIX qa: <http://www.wdaqua.eu/qa#> | ||
PREFIX oa: <http://www.w3.org/ns/openannotation/core/> | ||
SELECT * | ||
FROM <{graph}> | ||
WHERE {{ | ||
?annotationId a qa:AnnotationOfQuestionTranslation . | ||
?annotationId oa:hasTarget ?hasTarget ; | ||
oa:hasBody ?hasBody ; | ||
oa:annotatedBy ?annotatedBy ; | ||
oa:annotatedAt ?annotatedAt . | ||
FILTER(lang(?hasBody) = {lang}). | ||
}} | ||
""".format( | ||
graph = graph_uri, | ||
lang=lang | ||
) | ||
results = select_from_triplestore(triplestore_endpoint, sparql_find_ld) | ||
for result in results["results"]["bindings"]: | ||
question_uri = result["hasTarget"]["value"] | ||
question_text = result["hasBody"]["value"] | ||
source_texts.append(question_text_with_language(question_uri, question_text, lang)) | ||
|
||
return source_texts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ def read_requirements(): | |
|
||
setuptools.setup( | ||
name="qanary-helpers", | ||
version="0.2.2", | ||
version="0.3.0", | ||
author="Andreas Both, Aleksandr Perevalov", | ||
author_email="[email protected], [email protected]", | ||
description="A package that helps to build Python components for the Qanary Question Answering framework", | ||
|