| Title: | Turn Plain Language Statements Into RDF, CSV, or Anything |
|---|---|
| Description: | Core functions to work with Rosetta Statements, which use templates to convert plain language strings into RDF, CSV or other representations. This package is designed to make the creation of knowledge graphs easier. |
| Authors: | Tim Alamenciak [aut, cre], Tarek Al Mustafa [aut] |
| Maintainer: | Tim Alamenciak <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-09-17 09:05:26 UTC |
| Source: | https://github.com/timalamenciak/rosettaR |
Add a template string to the library
add_template(library, template)add_template(library, template)
library |
Input must be a library dataframe created by initLibrary() |
template |
This should be one string template. |
A new library data frame containing the template that was added.
templates <- init_library() templates <- add_template(templates, apple_template)templates <- init_library() templates <- add_template(templates, apple_template)
Apple Output Template Example This is an example of an output template that reformats the statement as a CSV.
apple_csvapple_csv
apple_csv
A string that contains Jinja syntax placeholders that match
the apple_template.
https://arxiv.org/pdf/2407.20007
Apple Rosetta Statement Example This is an example of a statement that describes the weight of Apple X.
apple_statementapple_statement
apple_statement
A string that specifies an object, a quality, a measurement and a unit.
https://arxiv.org/pdf/2407.20007
Apple Rosetta Statement Example This is an example of a template to describe the weight of a particular apple.
apple_templateapple_template
apple_template
A string with slots for an object, a quality, a measurement and a unit.
https://arxiv.org/pdf/2407.20007
Change a data frame into a series of statements based on templates.
df_to_statements(df, templates)df_to_statements(df, templates)
df |
A data frame with headers that match the slots in a template. The dataframe must have one column called "TemplateID" that contains the ID of the template used to interpret the row. |
templates |
A Rosetta Statement template library created by the initLibrary function. |
A data frame statement library containing the statements and associated templates.
templates <- init_library() templates <- add_template(templates, apple_template) df <- data.frame(object = "Apple X", quality = "weight", value="100.2", unit="grams", TemplateID = "1") statements <- df_to_statements(df,templates)templates <- init_library() templates <- add_template(templates, apple_template) df <- data.frame(object = "Apple X", quality = "weight", value="100.2", unit="grams", TemplateID = "1") statements <- df_to_statements(df,templates)
Load a set of Rosetta templates into a dataframe.
init_library(file = NA)init_library(file = NA)
file |
A CSV with the headers 'TemplateID' and 'templateText'.
Headers are case-sensitive and required. Other functions
like |
A dataframe containing either the loaded templates or the proper headers for use with other functions.
#Initialize an empty library templates <- init_library() #Load a CSV library apple_templates <- init_library(system.file("extdata", "apple_templates.csv", package="rosettaR"))#Initialize an empty library templates <- init_library() #Load a CSV library apple_templates <- init_library(system.file("extdata", "apple_templates.csv", package="rosettaR"))
Converts a plain language statement into a structured dataframe using a template. Supports variables {{ var }} and optional blocks [ ... ].
rosetta_format(s, in_template, out_template = "df")rosetta_format(s, in_template, out_template = "df")
s |
The input statement string. |
in_template |
The Rosetta template string. |
out_template |
The output format ('df' for dataframe, 'rdf' for generic Turtle, or a Jinja string). |
Either a data frame or the output template with values filled in from statement and input template.
# example code rosetta_format("Apple X weighs 235 grams", "{{ fruit }} weighs {{ value }} {{ unit}}")# example code rosetta_format("Apple X weighs 235 grams", "{{ fruit }} weighs {{ value }} {{ unit}}")
Iterates through a vector of statements and attempts to match them against a library of templates. Returns a long-format dataframe.
rosetta_match(statements, templates)rosetta_match(statements, templates)
statements |
A character vector of plain language statements, OR a dataframe containing a column of statements. |
templates |
A dataframe created by |
A data.frame in long format with columns: statement_id,
statement_text, template_id, variable, value.
## Not run: # Vector Input statements <- c("Kitchener is in Canada", "Paris is in France") # Dataframe Input (e.g. from read.csv) # df <- read.csv("my_statements.csv") templates <- init_library(system.file("extdata/apple_templates.csv", package="rosettaR")) results <- rosetta_match(statements, templates) ## End(Not run)## Not run: # Vector Input statements <- c("Kitchener is in Canada", "Paris is in France") # Dataframe Input (e.g. from read.csv) # df <- read.csv("my_statements.csv") templates <- init_library(system.file("extdata/apple_templates.csv", package="rosettaR")) results <- rosetta_match(statements, templates) ## End(Not run)
Converts a list of statements into a single, cohesive Turtle (TTL) string using the Rosetta Statement Ontology (RSO). Handles prefix management automatically.
rosetta_triplify(statements, templates)rosetta_triplify(statements, templates)
statements |
A character vector of plain language statements. |
templates |
A dataframe created by |
A single character string containing the full RDF document.
## Not run: # (Use the same setup as above) ttl <- rosetta_triplify(statements, templates) cat(ttl) ## End(Not run)## Not run: # (Use the same setup as above) ttl <- rosetta_triplify(statements, templates) cat(ttl) ## End(Not run)
Validate a data frame against a LinkML schema
rosetta_validate(data, schema, target_class = NULL)rosetta_validate(data, schema, target_class = NULL)
data |
A data frame (e.g. from 'rosetta_format) or list. |
schema |
Path to the LinkML schema YAML file. |
target_class |
The class in the schema to validate against. |
A list containing ok (boolean) and issues (list of errors).
## Not run: # Create a YAML schema for this example. schema_yaml <- "id: https://example.org/apple-schema name: AppleSchema imports: - linkml:types default_range: string classes: AppleObservation: attributes: object: required: true value: range: float # Value must be a number! unit: range: string" # Save to a temporary file for this example schema_file <- tempfile(fileext = ".yaml") writeLines(schema_yaml, schema_file)# (Use the same setup as above) good_data <- data.frame(object = "Apple A", value = 150.5, unit = "g") res_good <- rosetta_validate(good_data, schema_file, target_class = "AppleObservation") print(paste("Good Data is Valid:", res_good$ok)) ## End(Not run)## Not run: # Create a YAML schema for this example. schema_yaml <- "id: https://example.org/apple-schema name: AppleSchema imports: - linkml:types default_range: string classes: AppleObservation: attributes: object: required: true value: range: float # Value must be a number! unit: range: string" # Save to a temporary file for this example schema_file <- tempfile(fileext = ".yaml") writeLines(schema_yaml, schema_file)# (Use the same setup as above) good_data <- data.frame(object = "Apple A", value = 150.5, unit = "g") res_good <- rosetta_validate(good_data, schema_file, target_class = "AppleObservation") print(paste("Good Data is Valid:", res_good$ok)) ## End(Not run)
Opens a Shiny application to interactively test Rosetta templates and validation.
run_rosetta_ui()run_rosetta_ui()
A character string containing the processed text
(if out_template is text),
or a dataframe (if out_template is "df").
## Not run: run_rosetta_ui() ## End(Not run)## Not run: run_rosetta_ui() ## End(Not run)