dictionary: Create a dictionary object

View source: R/dictionaries.R

dictionaryR Documentation

Create a dictionary object

Description

Create a quanteda dictionary object to perform pattern matching on tokens, dfm and fcm.

Usage

dictionary(
  x,
  file = NULL,
  format = NULL,
  separator = " ",
  tolower = TRUE,
  tokenize = FALSE,
  levels = 1:100,
  encoding = "utf-8"
)

Arguments

x

a named list of valuetype patterns or an existing dictionary object. See examples. This argument should be omitted if file is specified.

file

file identifier for a foreign dictionary.

format

character identifier for the format of the foreign dictionary. If not supplied, the format is guessed from the dictionary file's extension. Available options are:

"wordstat"

format used by Provalis Research's WordStat software

"LIWC"

format used by the Linguistic Inquiry and Word Count software

"yoshikoder"

format used by Yoshikoder software

"lexicoder"

format used by Lexicoder

"YAML"

the standard YAML format

separator

the character in between multi-word dictionary values. This defaults to " ".

tolower

if TRUE, convert all dictionary values to lowercase.

tokenize

if TRUE segment dictionary values by separators to using the built-in tokenizer. Useful for Japanese and Chinese dictionaries.

levels

integers specifying the levels of entries in x or file to be included in the object.

encoding

additional optional encoding value for reading in imported dictionaries. This uses the iconv labels for encoding. See the "Encoding" section of the help for file.

Details

A dictionary object can include multi-word expressions segmented by separator. When it is applied to tokens object, they match both sequences of separate tokens and compounded tokens.

Dictionary objects can be subsetted using [ and [[, operating the same as the equivalent list operators. If dictionary() is applied to existing objects, it is possible to select levels.

Dictionary objects can be coerced from and to lists using as.dictionary() and as.list(), and checked using is.dictionary().

Currently supported input file formats are the WordStat, LIWC, Lexicoder v2 and v3, and Yoshikoder formats. The import using the LIWC format works with all currently available dictionary files supplied as part of the LIWC 2001, 2007, and 2015 software (see References).

Value

A dictionary class object, essentially a specially classed named list of characters.

References

WordStat dictionaries page, from Provalis Research https://provalisresearch.com/products/content-analysis-software/wordstat-dictionary/.

Pennebaker, J.W., Chung, C.K., Ireland, M., Gonzales, A., & Booth, R.J. (2007). The development and psychometric properties of LIWC2007. [Software manual]. Austin, TX (https://www.liwc.app/).

Yoshikoder page, from Will Lowe https://conjugateprior.org/software/yoshikoder/.

Lexicoder format, https://www.snsoroka.com/data-lexicoder/

See Also

as.dictionary(), as.list(), is.dictionary()

Examples

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)

## Not run: 
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)

## End(Not run)

quanteda documentation built on April 7, 2026, 1:06 a.m.