Converts a hierarchical dictionary (a named list of named lists, ending in character vectors at the lowest level) into a flat list of character vectors. Works like unlist(dictionary, recursive = TRUE) except that the recursion does not go to the bottom level. Called by dfm().

flatten_dictionary(
  dict,
  levels = 1:100,
  level = 1,
  key_parent = "",
  dict_flat = list()
)

Arguments

dict

list to be flattened

levels

integer vector indicating levels in the dictionary

level

internal argument to pass current levels

key_parent

internal argument to pass for parent keys

dict_flat

internal argument to pass flattened dictionary

Value

A dictionary flattened to variable levels

Author

Kohei Watanabe

Examples

dict1 <-
    dictionary(list(populism=c("elit*", "consensus*", "undemocratic*", "referend*",
                               "corrupt*", "propagand", "politici*", "*deceit*",
                               "*deceiv*", "*betray*", "shame*", "scandal*", "truth*",
                               "dishonest*", "establishm*", "ruling*")))
flatten_dictionary(dict1)
#> $populism
#>  [1] "elit*"         "consensus*"    "undemocratic*" "referend*"    
#>  [5] "corrupt*"      "propagand"     "politici*"     "*deceit*"     
#>  [9] "*deceiv*"      "*betray*"      "shame*"        "scandal*"     
#> [13] "truth*"        "dishonest*"    "establishm*"   "ruling*"      
#> 
#> attr(,"meta")
#> attr(,"meta")$system
#> attr(,"meta")$system$`package-version`
#> [1] ‘3.2.0’
#> 
#> attr(,"meta")$system$`r-version`
#> [1] ‘4.1.2’
#> 
#> attr(,"meta")$system$system
#>   sysname   machine      user 
#>  "Darwin"  "x86_64" "kbenoit" 
#> 
#> attr(,"meta")$system$directory
#> [1] "/Users/kbenoit/Dropbox (Personal)/GitHub/quanteda/quanteda/docs/reference"
#> 
#> attr(,"meta")$system$created
#> [1] "2021-11-25"
#> 
#> 
#> attr(,"meta")$object
#> attr(,"meta")$object$valuetype
#> [1] "glob"
#> 
#> attr(,"meta")$object$separator
#> [1] " "
#> 
#> 
#> attr(,"meta")$user
#> list()
#> 

dict2 <- list(level1a = list(level1a1 = c("l1a11", "l1a12"),
                             level1a2 = c("l1a21", "l1a22")),
              level1b = list(level1b1 = c("l1b11", "l1b12"),
                             level1b2 = c("l1b21", "l1b22", "l1b23")),
              level1c = list(level1c1a = list(level1c1a1 = c("lowest1", "lowest2")),
                             level1c1b = list(level1c1b1 = c("lowestalone"))))
flatten_dictionary(dict2)
#> $level1a
#> [1] "l1a11" "l1a12" "l1a21" "l1a22"
#> 
#> $level1b
#> [1] "l1b11" "l1b12" "l1b21" "l1b22" "l1b23"
#> 
#> $level1c.level1c1a
#> [1] "lowest1" "lowest2"
#> 
#> $level1c.level1c1b
#> [1] "lowestalone"
#> 
flatten_dictionary(dict2, 2)
#> $level1c1a
#> [1] "lowest1" "lowest2"
#> 
#> $level1c1b
#> [1] "lowestalone"
#> 
flatten_dictionary(dict2, 1:2)
#> $level1a
#> [1] "l1a11" "l1a12" "l1a21" "l1a22"
#> 
#> $level1b
#> [1] "l1b11" "l1b12" "l1b21" "l1b22" "l1b23"
#> 
#> $level1c.level1c1a
#> [1] "lowest1" "lowest2"
#> 
#> $level1c.level1c1b
#> [1] "lowestalone"
#>