corp <- corpus_subset(data_corpus_inaugural, Year > 2000)
toks <- tokens(corp)
dict <- dictionary(list(
tax = c("tax", "taxes", "taxing"), # fixed patterns
economy = list("econom*", # glob patterns
job = c("work*", "job*")), # nested keys
health = c("health care", "public health") # multi-word expressions
))
# compound tokens
tokens_compound(toks, pattern = dict) |>
dfm() |>
dfm_select(dict)
tokens_lookup(toks, dictionary = dict, levels = 1) |>
dfm()
# subset a dictionary
dict[1:2]
dict[c("economy")]
# update a dictionary
dictionary(dict, levels = 2)
if (FALSE) {
dfmat <- dfm(tokens(data_corpus_inaugural))
# import the Laver-Garry dictionary from Provalis Research
download.file("https://provalisresearch.com/Download/LaverGarry.zip",
tf <- tempfile(), mode = "wb")
unzip(tf, exdir = (td <- tempdir()))
dict_lg <- dictionary(file = paste(td, "LaverGarry.cat", sep = "/"))
dfm_lookup(dfmat, dict_lg)
# import a LIWC formatted dictionary from http://www.moralfoundations.org
download.file("http://bit.ly/37cV95h", tf <- tempfile())
dict_liwc <- dictionary(file = tf, format = "LIWC")
dfm_lookup(dfmat, dict_liwc)
}
Run the code above in your browser using DataLab