-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add query to delete statement #93
Changes from 7 commits
3c8f23d
c0f5dbc
e619f80
1a66505
a69dc32
e44c843
7615728
4164985
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -306,3 +306,102 @@ def get_query_for_conditional_get_statements(triple: Triple) -> SPARQLQuery: | |
|
||
# Cleaning up the query | ||
return _clean_sparql_representation(query) | ||
|
||
|
||
def get_query_for_remove_preference(pkg_data: PKGData) -> SPARQLQuery: | ||
"""Gets SPARQL query to remove a preference. | ||
|
||
Args: | ||
pkg_data: PKG data associated to a statement. | ||
|
||
Returns: | ||
SPARQL query. | ||
""" | ||
blank_node_id = "?statement" | ||
statement = _get_statement_representation(pkg_data, blank_node_id) | ||
# Remove statement description from conditions | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to do that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The description is removed because it not possible that it is the same when asking to remove a statement, especially when using natural language. For example, a user might input "Delete my preference towards Tom Cruise" or " Remove the statements related to Tom Cruise" to remove the statement "I like Tom Cruise." |
||
statement = re.sub(r'dc:description "[^"]+" ;', "", statement) | ||
|
||
query = f""" | ||
DELETE {{ | ||
?preference ?p ?o . | ||
?subject wi:preference ?preference . | ||
}} | ||
WHERE {{ | ||
{statement} | ||
?subject wi:preference ?preference . | ||
?preference pav:derivedFrom {blank_node_id} . | ||
?preference ?p ?o . | ||
}} | ||
""" | ||
|
||
# Cleaning up the query | ||
return _clean_sparql_representation(query) | ||
|
||
|
||
def get_query_for_remove_statement(pkg_data: PKGData) -> SPARQLQuery: | ||
"""Gets SPARQL query to remove a statement. | ||
|
||
Note that if a preference is derived from the statement, it is also removed. | ||
|
||
Args: | ||
pkg_data: PKG data associated to a statement. | ||
|
||
Returns: | ||
SPARQL query. | ||
""" | ||
blank_node_id = "?statement" | ||
statement_representation = _get_statement_representation( | ||
pkg_data, blank_node_id | ||
) | ||
# Remove statement description from conditions | ||
statement_representation = re.sub( | ||
r'dc:description "[^"]+" ;', "", statement_representation | ||
) | ||
|
||
query = f""" | ||
DELETE {{ | ||
?statement ?p ?o . | ||
NoB0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
?preference ?pp ?op . | ||
}} | ||
WHERE {{ | ||
{statement_representation} | ||
?statement ?p ?o . | ||
NoB0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
OPTIONAL {{ | ||
?preference pav:derivedFrom ?statement . | ||
?preference ?pp ?op . | ||
}} | ||
}} | ||
""" | ||
|
||
# Cleaning up the query | ||
return _clean_sparql_representation(query) | ||
|
||
|
||
def get_queries_for_remove_cleanup() -> List[SPARQLQuery]: | ||
"""Gets SPARQL queries to delete dangling concepts and weight scales. | ||
|
||
Returns: | ||
List of SPARQL queries. | ||
""" | ||
query_delete_concepts = """ | ||
DELETE { | ||
?concept ?p ?o . | ||
} WHERE { | ||
?concept a skos:Concept . | ||
?concept ?p ?o . | ||
FILTER NOT EXISTS { ?_1 ?_2 ?concept . } | ||
} | ||
""" | ||
NoB0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
query_delete_concepts = _clean_sparql_representation(query_delete_concepts) | ||
query_delete_scales = """ | ||
DELETE { | ||
?x ?p ?o . | ||
} WHERE { | ||
?x wo:scale ?_3 . | ||
?x ?p ?o . | ||
FILTER NOT EXISTS { ?_1 ?_2 ?x . } | ||
} | ||
""" | ||
NoB0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
query_delete_scales = _clean_sparql_representation(query_delete_scales) | ||
return [query_delete_concepts, query_delete_scales] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -302,3 +302,39 @@ def test_get_query_for_conditional_get_statements( | |
assert utils.get_query_for_conditional_get_statements( | ||
pkg_data_example.triple | ||
) == strip_string(expected_query) | ||
|
||
|
||
def test_get_query_for_remove_statement( | ||
pkg_data_example: PKGData, statement_representation: str | ||
) -> None: | ||
"""Tests get_query_for_remove_statement method. | ||
|
||
Args: | ||
pkg_data_example: PKG data example. | ||
statement_representation: Statement representation. | ||
""" | ||
statement_node_id = utils.get_statement_node_id(pkg_data_example) | ||
statement_representation = statement_representation.replace( | ||
statement_node_id, "?statement" | ||
) | ||
statement_representation = re.sub( | ||
r'dc:description "[^"]+" ;', "", statement_representation | ||
) | ||
sparql_query = f""" | ||
DELETE {{ | ||
?statement ?p ?o . | ||
?preference ?pp ?op . | ||
}} | ||
WHERE {{ | ||
{statement_representation} | ||
?statement ?p ?o . | ||
OPTIONAL {{ | ||
?preference pav:derivedFrom ?statement . | ||
?preference ?pp ?op . | ||
}} | ||
}} | ||
""" | ||
|
||
assert utils.get_query_for_remove_statement( | ||
pkg_data_example | ||
) == strip_string(sparql_query) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tests for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added a test for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it always the same? Can we make it a global variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a global variable
_SPARQL_STATEMENT_VARIABLE
for?statement
, please check that it does not impact the readability of the queries.