Read entity patterns from a file, or write them to one, for instance to keep them with a project, edit them by hand, or share them with users of spaCy in Python.
Usage
entity_patterns_read(file, label = NULL, case_insensitive = FALSE)
entity_patterns_write(x, file)Arguments
- file
the path of the file to read or write. Its extension sets the format, and must be
.jsonlfor writing:.jsonl: spaCy's own format, one pattern per line, each a JSON object with the keyslabel,patternand optionallyid, as written by spaCy'sEntityRuler.to_disk();.json: a JSON array of the same patterns;.txt(reading only): one phrase per line, all with the label given bylabel. Empty lines are skipped.
- label
character; for a
.txtfile, the label of its phrases.- case_insensitive
logical; for phrases, whether they match regardless of case. See
as_entity_patterns().- x
patterns to write: a
spacyr_entity_patternsobject, or anything thatas_entity_patterns()accepts.
Value
entity_patterns_read() returns a spacyr_entity_patterns
object. entity_patterns_write() returns file, invisibly.
Details
Reading and writing JSON needs the jsonlite package.
entity_patterns_write() writes JSONL only, so file must end in .jsonl.
It checks the patterns before opening file, so an existing file is left
as it was if they are not valid. A phrase that matches regardless of case is written with the extra
key "case_insensitive": true, which entity_patterns_read() reads back.
spaCy itself ignores that key, and matches the phrase exactly.
Patterns written and read back are identical to the originals.
Examples
# \donttest{
if (requireNamespace("jsonlite", quietly = TRUE)) {
pats <- as_entity_patterns(list(
list(label = "PERSON",
pattern = list(list(LOWER = list(IN = c("dr", "dr."))),
list(ENT_TYPE = "PERSON", ENT_IOB = "B"),
list(ENT_TYPE = "PERSON", ENT_IOB = "I", OP = "*"))),
list(label = "ORG", pattern = "Acme Widgets", id = "acme")
))
file <- tempfile(fileext = ".jsonl")
entity_patterns_write(pats, file)
readLines(file)
identical(entity_patterns_read(file), pats)
}
#> [1] TRUE
# }