Highlight tokens in fulltext based on exact match, a regular expression or
corpus position in kwic output or html document.
highlight(.Object, ...)
# S4 method for class 'character'
highlight(.Object, highlight = list(), regex = FALSE, perl = FALSE, ...)
# S4 method for class 'html'
highlight(.Object, highlight = list(), regex = FALSE, perl = FALSE, ...)
# S4 method for class 'kwic'
highlight(
.Object,
highlight = list(),
regex = FALSE,
perl = TRUE,
verbose = TRUE,
...
)A html, character, a kwic object.
Terms to be highlighted can be passed in as named character
vectors of terms (or regular expressions); the name needs to be a
valid color name. It is also possible to pass in a matrix with ranges (as
returned by cpos()).
A character vector, or a list of character or integer
vectors.
Logical, whether character vectors are interpreted as regular expressions.
Logical, whether to use perl-style regular expressions for
highlighting when regex is TRUE.
Logical, whether to output messages.
If highlight is a character vector, the names of the vector are interpreted
as colors. If highlight is a list, the names of the list are considered as
colors. Values can be character values or integer values with token ids.
Colors are inserted into the output html and need to be digestable for the
browser used.
use("polmineR")
#> ✔ corpus loaded: GERMAPARLMINI (version: 0.1.0 | build date: 2023-04-16)
use(pkg = "RcppCWB", corpus = "REUTERS")
#> ✔ corpus loaded: REUTERS
P <- partition("REUTERS", places = "argentina")
#> ... get cpos and strucs
H <- html(P)
Y <- highlight(H, list(lightgreen = "higher"))
if (interactive()) htmltools::html_print(Y)
# highlight matches for a CQP query
regions <- cpos(P, query = '"prod.*"', cqp = TRUE)
H2 <- highlight(H, highlight = list(yellow = regions))
# the method can be used in pipe
P %>% html() %>% highlight(list(lightgreen = "1986")) -> H
P %>% html() %>% highlight(list(lightgreen = c("1986", "higher"))) -> H
P %>% html() %>% highlight(list(lightgreen = 4020:4023)) -> H
# use highlight for kwic output
K <- kwic("REUTERS", query = "barrel")
K2 <- highlight(K, highlight = list(yellow = c("oil", "price")))
# use character vector for output, not list
K2 <- highlight(
K,
highlight = c(
green = "pric.",
red = "reduction",
red = "decrease",
orange = "dropped"
),
regex = TRUE
)