是的 - 在形成 dfm 之前,您需要在令牌上使用 tokens_lookup()。一旦您对单个单词进行了标记,它们就不再作为您需要匹配字典中的多单词值的有序序列存在。所以 1) 形成标记对象,2) 使用 tokens_lookup() 将字典应用于标记,然后 3) 形成 dfm。
library("quanteda")
#> Package version: 1.5.2
BritainEN <-
dictionary(list(identity = c("British", "Great Britain")))
txt <- c(doc1 = "Great Britain is a country.",
doc2 = "British citizens live in Great Britain.")
tokens(txt) %>%
tokens_lookup(dictionary = BritainEN, exclusive = FALSE)
#> tokens from 2 documents.
#> doc1 :
#> [1] "IDENTITY" "is" "a" "country" "."
#>
#> doc2 :
#> [1] "IDENTITY" "citizens" "live" "in" "IDENTITY" "."
tokens(txt) %>%
tokens_lookup(dictionary = BritainEN) %>%
dfm()
#> Document-feature matrix of: 2 documents, 1 feature (0.0% sparse).
#> 2 x 1 sparse Matrix of class "dfm"
#> features
#> docs identity
#> doc1 1
#> doc2 2
已添加
为了回答额外的评论问题并扩展 @phiver 对此非常有用的答案,还有一个 nested_scope 参数设计用于可能在另一个 MWE 字典键的值中发生的匹配。
例子:
library("quanteda")
## Package version: 1.5.2
Ireland_nested <- dictionary(list(
ie_alone = "Ireland",
ie_nested = "Northern Ireland"
))
txt <- c(
doc1 = "Northern Ireland is a country.",
doc2 = "Some citizens of Ireland live in Northern Ireland."
)
toks <- tokens(txt)
tokens_lookup(toks, dictionary = Ireland_nested, exclusive = FALSE)
## Tokens consisting of 2 documents.
## doc1 :
## [1] "IE_NESTED" "IE_ALONE" "is" "a" "country" "."
##
## doc2 :
## [1] "Some" "citizens" "of" "IE_ALONE" "live" "in"
## [7] "IE_NESTED" "IE_ALONE" "."
tokens_lookup(toks,
dictionary = Ireland_nested, nested_scope = "dictionary",
exclusive = FALSE
)
## Tokens consisting of 2 documents.
## doc1 :
## [1] "IE_NESTED" "is" "a" "country" "."
##
## doc2 :
## [1] "Some" "citizens" "of" "IE_ALONE" "live" "in"
## [7] "IE_NESTED" "."
第一个匹配 both 键,因为嵌套级别正好在键内,但嵌套模式出现在两个不同的键中。 (在@phiver 中,模式嵌套在键中,在我的示例中它们不是。)当nested_scope = "dictionary" 时,它会在整个字典中查找嵌套模式匹配,而不仅仅是在键中,因此在我的示例中不会重复。
您选择哪一种取决于您的目的。我们将 quanteda 设计为具有大多数用户想要和期望的默认设置,但为有特定需求的用户添加了类似的附加选项。 (通常这些需求首先由 Kohei 或我在处理我们自己的特定用例时表达!)