Skip to content
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

Feat string to uri #37

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 151 additions & 1 deletion src/spinneret/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,166 @@ def create_graph(metadata_files: list = None, vocabulary_files: list = None) ->
# Load metadata
if metadata_files is not None:
for filename in metadata_files:
g.parse(filename)
g.parse(filename, format="json-ld")

# Load vocabularies
if vocabulary_files is not None:
for filename in vocabulary_files:
g.parse(filename, format=guess_format(filename))

# Some string literals should be converted to URIRefs to create linked
# data with vocabularies. These are often cases where SOSO conventions
# recommend connecting the value to an object property (e.g. `url`) rather
# than the object @id. The following code makes these conversions based on
# where vocabulary URIs can be used in SOSO markup.
g = convert_keyword_url_to_uri(g)
g = convert_variable_property_id_to_uri(g)
g = convert_variable_measurement_technique_to_uri(g)
g = convert_variable_unit_code_to_uri(g)
g = convert_license_to_uri(g)

return g


def convert_keyword_url_to_uri(graph: Graph) -> Graph:
"""
:param graph: Graph of metadata and vocabularies
:returns: Graph with keyword URLs converted to URIs
:notes: Converts values of `schema:keyword/schema:DefinedTerm/schema:url`
to URI references if the value appears to be a URL.
"""
update_request = """
PREFIX schema: <https://schema.org/>

DELETE {
?term schema:url ?value .
}
INSERT {
?term schema:url ?newURI .
}
WHERE {
?dataset schema:keywords ?term .
?term a schema:DefinedTerm .
?term schema:url ?value .
FILTER (isLiteral(?value) && REGEX(?value, "^https?://", "i")) .
BIND (URI(?value) AS ?newURI)
}
"""
graph.update(update_request)
return graph


def convert_variable_property_id_to_uri(graph: Graph) -> Graph:
"""
:param graph: Graph of metadata and vocabularies
:returns: Graph with variable property IDs converted to URIs
:notes: Converts values of `schema:variableMeasured/schema:PropertyValue/
schema:propertyID` to URI references if the value appears to be a URL.
"""
update_request = """
PREFIX schema: <https://schema.org/>

DELETE {
?term schema:propertyID ?value .
}
INSERT {
?term schema:propertyID ?newURI .
}
WHERE {
?dataset schema:variableMeasured ?term .
?term a schema:PropertyValue .
?term schema:propertyID ?value .
FILTER (isLiteral(?value) && REGEX(?value, "^https?://", "i")) .
BIND (URI(?value) AS ?newURI)
}
"""
graph.update(update_request)
return graph


def convert_variable_measurement_technique_to_uri(graph: Graph) -> Graph:
"""
:param graph: Graph of metadata and vocabularies
:returns: Graph with variable measurement techniques converted to URIs
:notes: Converts values of `schema:variableMeasured/schema:PropertyValue/
schema:measurementTechnique` to URI references if the value appears to
be a URL.
"""
update_request = """
PREFIX schema: <https://schema.org/>

DELETE {
?term schema:measurementTechnique ?value .
}
INSERT {
?term schema:measurementTechnique ?newURI .
}
WHERE {
?dataset schema:variableMeasured ?term .
?term a schema:PropertyValue .
?term schema:measurementTechnique ?value .
FILTER (isLiteral(?value) && REGEX(?value, "^https?://", "i")) .
BIND (URI(?value) AS ?newURI)
}
"""
graph.update(update_request)
return graph


def convert_variable_unit_code_to_uri(graph: Graph) -> Graph:
"""
:param graph: Graph of metadata and vocabularies
:returns: Graph with variable unit codes converted to URIs
:notes: Converts values of `schema:variableMeasured/schema:PropertyValue/
schema:unitCode` to URI references if the value appears to be a URL.
"""
update_request = """
PREFIX schema: <https://schema.org/>

DELETE {
?term schema:unitCode ?value .
}
INSERT {
?term schema:unitCode ?newURI .
}
WHERE {
?dataset schema:variableMeasured ?term .
?term a schema:PropertyValue .
?term schema:unitCode ?value .
FILTER (isLiteral(?value) && REGEX(?value, "^https?://", "i")) .
BIND (URI(?value) AS ?newURI)
}
"""
graph.update(update_request)
return graph


def convert_license_to_uri(graph: Graph) -> Graph:
"""
:param graph: Graph of metadata and vocabularies
:returns: Graph with licenses converted to URIs
:notes: Converts values of `schema:license` to URI references if the value
appears to be a URL.
"""
update_request = """
PREFIX schema: <https://schema.org/>

DELETE {
?dataset schema:license ?value .
}
INSERT {
?dataset schema:license ?newURI .
}
WHERE {
?dataset schema:license ?value .
FILTER (isLiteral(?value) && REGEX(?value, "^https?://", "i")) .
BIND (URI(?value) AS ?newURI)
}
"""
graph.update(update_request)
return graph


if __name__ == "__main__":
# Example usage
WORKING_DIR = "/Users/csmith/Data/soso/all_edi_test_results"
Expand Down
Loading
Loading