Skip to contents

hr_register() files a module under a kind, such as "reader" or "segmenter", through the same checks as that kind's own registration function: hr_register("reader", ...) runs what gr_register_reader() runs, so a reader named "auto" or with a malformed signature is refused here as it is there. hr_modules() lists the kinds and what each holds.

Usage

hr_register(
  kind,
  name,
  fn,
  ...,
  description = "",
  calls = NULL,
  version = NULL
)

Arguments

kind

The kind of module, one string. A kind the package does not know is an error of class hr_unknown_kind.

name

The module's name, one string. Registering a name again replaces the module.

fn

The module's function, with the arguments its kind calls it with (see the kind's own registration function). For a protocol, the protocol object; for a step, a step or a function of (input, ctx).

...

The other arguments of the kind's own registration function, by name, such as signature for a reader or extensions for an extractor. A segmenter also takes effort_label, a function of the segment spec that returns the trace label its model requests carry, or "" when it sends none. A run's effort setting then reaches the segmenter as the effort for that label alone. Without it, the segmenter gets the run's whole effort map. A step takes the arguments of hr_step(), such as params, input and output.

description

One-line description, shown by the kind's listing and by hr_modules(). For a protocol, a non-empty description replaces the protocol's own.

calls

The stems of the trace labels the module's model calls carry, for the kinds that declare them (readers, segmenters and steps). NULL (the default) declares the module's name for a reader or a segmenter and no stems for a step. Any other value for a kind that does not declare calls is an error of class gr_bad_setting.

version

The module's own version, one string, kept in the version field. NULL (the default) records NA, or, for a step, the step's own version.

Value

Invisibly, name.

Details

The kinds the package brings, and the function each one goes through: "extractor" (gr_register_extractor()), "cleaner" (gr_register_cleaner()), "segmenter" (gr_register_segmenter()), "reader" (gr_register_reader()), "embedder" (gr_register_embedder()), "protocol" (gr_register_protocol()) and "step". For a protocol, fn is the protocol object from gr_protocol(). For a step, fn is a step from hr_step(), or a function of (input, ctx) that hr_register() passes to hr_step() with the name and the arguments in ....

A module registered here records two more fields: version, the version you give, and contract, the version of the module contract the package implements, 0.2 in this release. Every listing shows them as its last two columns. A module registered through a kind's own function has NA in both, and its conformance check stays advisory.

Examples

# A segmenter that makes one chunk per line, registered by kind.
hr_register("segmenter", "by_line", description = "one chunk per line",
  version = "1.0.0",
  fn = function(doc, spec, client, trace) {
    new_chunks(strsplit(doc$text, "\n+")[[1]], "by_line", spec)
  })
subset(hr_modules("segmenter"), name == "by_line")[, c("kind", "name", "cost", "contract")]
#>        kind    name cost contract
#> 2 segmenter by_line free      0.2
nrow(gr_segment(gr_example(), list(method = "by_line"))$chunks)
#> Using cached ingestion for this document + settings.
#> Segmenting with 'by_line' (cap 1200 tokens, overlap 0).
#> [1] 32

# A step, registered by kind and listed with its input and output.
hr_register("step", "shout", fn = function(input, ctx) toupper(input),
  input = "answer", output = "state")
hr_modules("step")[, c("name", "input", "output", "version", "contract")]
#>    name  input output version contract
#> 1 shout answer  state   0.0.1      0.2

# A kind the package does not know is refused.
try(hr_register("ranker", "mine", fn = identity))
#> Error in kind_access(kind, "set") : 
#>   Unknown kind of module 'ranker'. Kinds you can register: extractor, cleaner, segmenter, reader, embedder, protocol, step.