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 character
highlight(.Object, highlight = list(), regex = FALSE, perl = FALSE, ...)

# S4 method for html
highlight(.Object, highlight = list(), regex = FALSE, perl = FALSE, ...)

# S4 method for kwic
highlight(
  .Object,
  highlight = list(),
  regex = FALSE,
  perl = TRUE,
  verbose = TRUE,
  ...
)

Arguments

.Object

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 then needs to be a valid color name.

highlight

A character vector, or a list of character or integer vectors.

regex

Logical, whether character vectors are interpreted as regular expressions.

perl

Logical, whether to use perl-style regular expressions for highlighting when regex is TRUE.

verbose

Logical, whether to output messages.

Details

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.

Examples

use("polmineR")
#> ... activating corpus: GERMAPARLMINI (version: 0.0.1 | build date: 2019-02-23)
#> ... activating corpus: REUTERS
P <- partition("REUTERS", places = "argentina")
#> ... get encoding: latin1
#> ... 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 H2 <- highlight( H, highlight = list(yellow = cpos(hits(P, query = '"prod.*"', cqp = TRUE))) ) # 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 )