在 Quanteda 中使用 kwic 避免重叠术语
Avoid overlapping terms using kwic in Quanteda
我正在使用字典搜索语料库中出现的术语,这些术语可能单独出现,但它们通常会重叠:
corpus <- c("According to the Canadian Charter of Rights and Freedoms, all Canadians...")
dict <- dictionary(list(constitution = c("charter of rights", "canadian charter")))
kwic(corpus, dict)
以上将(正确)识别下面的句子两次:
"According to the Canadian Charter of Rights and Freedoms, all Canadians..."
然而,为了确定这些术语出现的频率,并避免重复计算,我需要确保术语 "canadian charter" 出现的情况仅在未遵循时才被计算在内通过“..权利...”
我怎样才能做到这一点?
编辑: 刚刚注意到如果使用 tokens_lookup
这不是问题,所以这个问题是一个沉默点。留下它以防它对其他人有帮助。
当您请求 kwic
时,您将获得所有模式匹配项,即使它们重叠。因此,按照我认为您要问的方式,避免重叠的方法是手动将多词表达式 (MWE) 转换为单个标记,以防止它们重叠。在你的情况下,你想在 "Canadian charter" 后面没有跟随 "of rights" 时计算它。然后我建议你标记文本,然后按照保证它们不会重叠的顺序组合 MWE。
library("quanteda", warn.conflicts = FALSE)
## Package version: 1.4.0
## Parallel computing: 2 of 12 threads used.
## See https://quanteda.io for tutorials and examples.
txt <- "The Canadian charter of rights and the Canadian charter are different."
dict <- dictionary(list(constitution = c("charter of rights", "canadian charter")))
toks <- tokens(txt)
tokscomp <- toks %>%
tokens_compound(phrase("charter of rights"), concatenator = " ") %>%
tokens_compound(phrase("Canadian charter"), concatenator = " ")
tokscomp
## tokens from 1 document.
## text1 :
## [1] "The" "Canadian" "charter of rights"
## [4] "and" "the" "Canadian charter"
## [7] "are" "different" "."
这已将短语变成单个标记,此处由 space 分隔,这意味着在 kwic()
中(如果这是您要使用的)将不会重复计算它们因为它们现在是唯一的 MWE 匹配项。
kwic(tokscomp, dict, window = 2)
##
## [text1, 3] The Canadian | charter of rights | and the
## [text1, 6] and the | Canadian charter | are different
请注意,为了简单地计算它们,您可以将 dfm()
与字典一起用作 select
参数的值:
dfm(tokscomp, select = dict)
## Document-feature matrix of: 1 document, 2 features (0.0% sparse).
## 1 x 2 sparse Matrix of class "dfm"
## features
## docs charter of rights canadian charter
## text1 1 1
最后,如果您主要想区分 "Canadian charter of rights" 和 "Canadian charter",您可以先将前者复合,然后再将后者复合(这里最好从最长到最短)。但这不是你问的。
我正在使用字典搜索语料库中出现的术语,这些术语可能单独出现,但它们通常会重叠:
corpus <- c("According to the Canadian Charter of Rights and Freedoms, all Canadians...")
dict <- dictionary(list(constitution = c("charter of rights", "canadian charter")))
kwic(corpus, dict)
以上将(正确)识别下面的句子两次:
"According to the Canadian Charter of Rights and Freedoms, all Canadians..."
然而,为了确定这些术语出现的频率,并避免重复计算,我需要确保术语 "canadian charter" 出现的情况仅在未遵循时才被计算在内通过“..权利...”
我怎样才能做到这一点?
编辑: 刚刚注意到如果使用 tokens_lookup
这不是问题,所以这个问题是一个沉默点。留下它以防它对其他人有帮助。
当您请求 kwic
时,您将获得所有模式匹配项,即使它们重叠。因此,按照我认为您要问的方式,避免重叠的方法是手动将多词表达式 (MWE) 转换为单个标记,以防止它们重叠。在你的情况下,你想在 "Canadian charter" 后面没有跟随 "of rights" 时计算它。然后我建议你标记文本,然后按照保证它们不会重叠的顺序组合 MWE。
library("quanteda", warn.conflicts = FALSE)
## Package version: 1.4.0
## Parallel computing: 2 of 12 threads used.
## See https://quanteda.io for tutorials and examples.
txt <- "The Canadian charter of rights and the Canadian charter are different."
dict <- dictionary(list(constitution = c("charter of rights", "canadian charter")))
toks <- tokens(txt)
tokscomp <- toks %>%
tokens_compound(phrase("charter of rights"), concatenator = " ") %>%
tokens_compound(phrase("Canadian charter"), concatenator = " ")
tokscomp
## tokens from 1 document.
## text1 :
## [1] "The" "Canadian" "charter of rights"
## [4] "and" "the" "Canadian charter"
## [7] "are" "different" "."
这已将短语变成单个标记,此处由 space 分隔,这意味着在 kwic()
中(如果这是您要使用的)将不会重复计算它们因为它们现在是唯一的 MWE 匹配项。
kwic(tokscomp, dict, window = 2)
##
## [text1, 3] The Canadian | charter of rights | and the
## [text1, 6] and the | Canadian charter | are different
请注意,为了简单地计算它们,您可以将 dfm()
与字典一起用作 select
参数的值:
dfm(tokscomp, select = dict)
## Document-feature matrix of: 1 document, 2 features (0.0% sparse).
## 1 x 2 sparse Matrix of class "dfm"
## features
## docs charter of rights canadian charter
## text1 1 1
最后,如果您主要想区分 "Canadian charter of rights" 和 "Canadian charter",您可以先将前者复合,然后再将后者复合(这里最好从最长到最短)。但这不是你问的。