Skip to content

Label commands

kurra.cli.commands.labels

CLI commands for managing RDF labels.

find_command

find_command(f: str = typer.Argument(..., help='The RDF file to fine IRIs missing labels within'), local_context: str = typer.Option(None, '--local-context', '-l', help='An RDF file or directory of RDF files containing labels')) -> None

Print every IRI in an RDF file or directory that is missing an rdfs:label.

Parameters:

Name Type Description Default
f str

The RDF file or directory of RDF files to scan for IRIs missing labels.

Argument(..., help='The RDF file to fine IRIs missing labels within')
local_context str

An RDF file or directory of files containing labels to check against, in addition to f itself.

Option(None, '--local-context', '-l', help='An RDF file or directory of RDF files containing labels')
Source code in kurra/cli/commands/labels.py
@app.command(name="find", help="Find IRIs missing labels")
def find_command(
    f: str = typer.Argument(
        ..., help="The RDF file to fine IRIs missing labels within"
    ),
    local_context: str = typer.Option(
        None,
        "--local-context",
        "-l",
        help="An RDF file or directory of RDF files containing labels",
    ),
) -> None:
    """Print every IRI in an RDF file or directory that is missing an `rdfs:label`.

    Args:
        f: The RDF file or directory of RDF files to scan for IRIs missing labels.
        local_context: An RDF file or directory of files containing labels to check against, in addition to `f` itself.
    """
    if Path(f).is_file() or Path(f).is_dir():
        if local_context is not None:
            if Path(local_context).is_file() or Path(local_context).is_dir():
                for iri in find_missing_labels(Path(f), Path(local_context)):
                    console.print(iri)
            else:
                console.print(
                    "[red]If a value for local context is given, it must be an existing file or directory[/red]"
                )
        else:
            for iri in find_missing_labels(Path(f)):
                console.print(iri)
    else:
        console.print(
            "[red]The value for f must be an existing file or directory[/red]"
        )

get_command

get_command(f: str = typer.Argument(..., help='The RDF file to fine IRIs missing labels within'), local_context: str = typer.Option(None, '--local-context', '-l', help='An RDF file or directory of RDF files containing labels'), additional_context: str = typer.Option('https://fuseki.dev.kurrawong.ai/semback/sparql', '--additional-context', '-c', help='An additional source of labels'), return_type: str = typer.Option('graph', '--return-type', '-r', help="Return RDF in the longturtle format or a printed table of IRIs and labels. Either 'graph' or 'table'")) -> None

Fetch and print labels for IRIs that are missing them from a given context (local_context) or additional context (additional_context), by default the KurrawongAI Semantic Background.

Parameters:

Name Type Description Default
f str

The RDF file or directory of RDF files to scan for IRIs missing labels.

Argument(..., help='The RDF file to fine IRIs missing labels within')
local_context str

An RDF file or directory of files containing labels to check against, in addition to f itself.

Option(None, '--local-context', '-l', help='An RDF file or directory of RDF files containing labels')
additional_context str

A further RDF source (file, directory, or SPARQL endpoint) to fetch missing labels from.

Option('https://fuseki.dev.kurrawong.ai/semback/sparql', '--additional-context', '-c', help='An additional source of labels')
return_type str

graph to output longturtle RDF, table for a printed table of IRIs and labels.

Option('graph', '--return-type', '-r', help="Return RDF in the longturtle format or a printed table of IRIs and labels. Either 'graph' or 'table'")
Source code in kurra/cli/commands/labels.py
@app.command(
    name="get",
    help="Gets labels for IRIs missing them from a given context or the KurrawongAI Semantic Background",
)
def get_command(
    f: str = typer.Argument(
        ..., help="The RDF file to fine IRIs missing labels within"
    ),
    local_context: str = typer.Option(
        None,
        "--local-context",
        "-l",
        help="An RDF file or directory of RDF files containing labels",
    ),
    additional_context: str = typer.Option(
        "https://fuseki.dev.kurrawong.ai/semback/sparql",
        "--additional-context",
        "-c",
        help="An additional source of labels",
    ),
    return_type: str = typer.Option(
        "graph",
        "--return-type",
        "-r",
        help="Return RDF in the longturtle format or a printed table of IRIs and labels. Either 'graph' or 'table'",
    ),
) -> None:
    """Fetch and print labels for IRIs that are missing them from a given context (`local_context`) or additional context (`additional_context`), by default the KurrawongAI Semantic Background.

    Args:
        f: The RDF file or directory of RDF files to scan for IRIs missing labels.
        local_context: An RDF file or directory of files containing labels to check against, in addition to `f` itself.
        additional_context: A further RDF source (file, directory, or SPARQL endpoint) to fetch missing labels from.
        return_type: `graph` to output longturtle RDF, `table` for a printed table of IRIs and labels.
    """
    iris = []
    if Path(f).is_file() or Path(f).is_dir():
        if local_context is not None:
            if Path(local_context).is_file() or Path(local_context).is_dir():
                iris = find_missing_labels(Path(f), Path(local_context))
            else:
                console.print(
                    "[red]If a value for local context is given, it must be an existing file or directory[/red]"
                )
        else:
            iris = find_missing_labels(Path(f))
    else:
        console.print(
            "[red]The value for f must be an existing file or directory[/red]"
        )

    if iris:
        if Path(additional_context).is_file() or Path(additional_context).is_dir():
            rdf = get_labels(iris, Path(additional_context, return_type))
        else:
            rdf = get_labels(iris, additional_context, return_type)

        if return_type == "graph":
            console.print(rdf.serialize(format="longturtle"))
        else:
            t = Table(show_header=True, header_style="b")
            t.add_column("IRI", no_wrap=True)
            t.add_column("Label", no_wrap=True)
            for iri, label in rdf.items():
                t.add_row(iri, label)
            console.print(t)
    else:
        console.print("No IRIs missing labels")

context_command

context_command(rdf: Path, local_context: Path | None = None) -> None

Generate a JSON-LD context from the labels in a given RDF file.

Parameters:

Name Type Description Default
rdf Path

The RDF file to generate a JSON-LD context from.

required
local_context Path | None

An RDF file or directory of files containing labels to source from, in addition to rdf itself.

None
Source code in kurra/cli/commands/labels.py
@app.command(
    "context", help="Generate a JSON-LD context based on the labels in a given RDF file"
)
def context_command(
    rdf: Annotated[
        Path, typer.Argument(help="The RDF file to generate a JSON-LD context from")
    ],
    local_context: Annotated[
        Path | None,
        typer.Option(
            "--local-context",
            "-l",
            help="An RDF file or directory of RDF files containing labels",
        ),
    ] = None,
) -> None:
    """Generate a JSON-LD context from the labels in a given RDF file.

    Args:
        rdf: The RDF file to generate a JSON-LD context from.
        local_context: An RDF file or directory of files containing labels to source from, in addition to `rdf` itself.
    """
    if local_context is not None:
        ctx = jsonld_context(load_graph(rdf), load_graph(local_context))
    else:
        ctx = jsonld_context(load_graph(rdf))
    json.dump(ctx, stdout, indent=4)