Create a quanteda dictionary class object, either from a list or by importing from a foreign format. 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).

dictionary(
  x,
  file = NULL,
  format = NULL,
  separator = " ",
  tolower = TRUE,
  encoding = "utf-8"
)

Arguments

x

a named list of character vector dictionary entries, including valuetype pattern matches, and including multi-word expressions separated by concatenator. See examples. This argument may be omitted if the dictionary is read from file.

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

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.

Value

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

Details

Dictionaries can be subsetted using [ and [[, operating the same as the equivalent list operators.

Dictionaries can be coerced from lists using as.dictionary(), coerced to named lists of characters using as.list(), and checked using is.dictionary().

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/

Examples

corp <- corpus_subset(data_corpus_inaugural, Year>1900)
dict <- dictionary(list(christmas = c("Christmas", "Santa", "holiday"),
                          opposition = c("Opposition", "reject", "notincorpus"),
                          taxing = "taxing",
                          taxation = "taxation",
                          taxregex = "tax*",
                          country = "america"))
tokens(corp) |>
    tokens_lookup(dictionary = dict) |>
    dfm()
#> Document-feature matrix of: 31 documents, 6 features (71.51% sparse) and 4 docvars.
#>                 features
#> docs             christmas opposition taxing taxation taxregex country
#>   1901-McKinley          0          2      0        1        1       0
#>   1905-Roosevelt         0          0      0        0        0       0
#>   1909-Taft              0          1      0        4        6       4
#>   1913-Wilson            0          0      0        1        1       0
#>   1917-Wilson            0          0      0        0        0       2
#>   1921-Harding           0          0      0        1        2      15
#> [ reached max_ndoc ... 25 more documents ]

# subset a dictionary
dict[1:2]
#> Dictionary object with 2 key entries.
#> - [christmas]:
#>   - christmas, santa, holiday
#> - [opposition]:
#>   - opposition, reject, notincorpus
dict[c("christmas", "opposition")]
#> Dictionary object with 2 key entries.
#> - [christmas]:
#>   - christmas, santa, holiday
#> - [opposition]:
#>   - opposition, reject, notincorpus
dict[["opposition"]]
#> [1] "opposition"  "reject"      "notincorpus"

# combine dictionaries
c(dict["christmas"], dict["country"])
#> Dictionary object with 2 key entries.
#> - [christmas]:
#>   - christmas, santa, holiday
#> - [country]:
#>   - america

if (FALSE) {
dfmat <- dfm(tokens(data_corpus_inaugural))

# import the Laver-Garry dictionary from Provalis Research
dictfile <- tempfile()
download.file("https://provalisresearch.com/Download/LaverGarry.zip",
              dictfile, mode = "wb")
unzip(dictfile, exdir = (td <- tempdir()))
dictlg <- dictionary(file = paste(td, "LaverGarry.cat", sep = "/"))
dfm_lookup(dfmat, dictlg)

# import a LIWC formatted dictionary from http://www.moralfoundations.org
download.file("http://bit.ly/37cV95h", tf <- tempfile())
dictliwc <- dictionary(file = tf, format = "LIWC")
dfm_lookup(dfmat, dictliwc)
}