我一直致力于识别和分类的搭配昆特达包在R。
例如;
我从文档列表中创建令牌对象,并应用配置分析。
toks <- tokens(text$abstracts)
collocations <- textstat_collocations(toks)然而,据我所见,没有明确的方法来判断哪个搭配频繁/存在于哪个文档中。即使我应用kwic(toks, pattern = phrase(collocations), selection = 'keep'),结果也只包括text1、text2等。
我想分组配置分析结果的基础上的博士学位。跟全德达有可能吗?
发布于 2021-05-25 09:27:13
听起来你想用文件来记录搭配。textstat_collocations()的输出已经为每个搭配提供了计数,但这些都是针对整个语料库的。
因此,按文档分组(或任何其他变量)的解决方案是:
textstat_collocations()获取搭配。下面,我在删除秒词和punctuation.tokens_compound()完成了生成停止词的标记。这将将每个配置序列转换为单个标记。textstat_frequency()来根据文档对化合物进行计数。这是一个更棘手的使用内置的初始语料库实现:
library("quanteda")
## Package version: 3.0
## Unicode version: 13.0
## ICU version: 69.1
## Parallel computing: 12 of 12 threads used.
## See https://quanteda.io for tutorials and examples.
library("quanteda.textstats")
toks <- data_corpus_inaugural %>%
tail(10) %>%
tokens(remove_punct = TRUE, padding = TRUE) %>%
tokens_remove(stopwords("en"), padding = TRUE)
colls <- textstat_collocations(toks)
head(colls)
## collocation count count_nested length lambda z
## 1 let us 34 0 2 6.257000 17.80637
## 2 fellow citizens 14 0 2 6.451738 16.18314
## 3 fellow americans 15 0 2 6.221678 16.16410
## 4 one another 14 0 2 6.592755 14.56082
## 5 god bless 15 0 2 8.628894 13.57027
## 6 united states 12 0 2 9.192044 13.22077现在我们将它们组合起来,只保留搭配,然后通过文档获取频率:
dfmat <- tokens_compound(toks, colls, concatenator = " ") %>%
dfm() %>%
dfm_keep("* *")dfm已经包含了每个搭配的按文档计数,但是如果您希望计数为data.frame格式,并带有分组选项,则使用textstat_frequency()。这里我只按文档输出前两位,但是如果您删除n = 2,那么它将根据文档给出所有搭配的频率。
textstat_frequency(dfmat, groups = docnames(dfmat), n = 2) %>%
head(10)
## feature frequency rank docfreq group
## 1 nuclear weapons 4 1 1 1985-Reagan
## 2 human freedom 3 2 1 1985-Reagan
## 3 new breeze 4 1 1 1989-Bush
## 4 new engagement 3 2 1 1989-Bush
## 5 let us 7 1 1 1993-Clinton
## 6 fellow americans 4 2 1 1993-Clinton
## 7 let us 6 1 1 1997-Clinton
## 8 new century 6 1 1 1997-Clinton
## 9 nation's promise 2 1 1 2001-Bush
## 10 common good 2 1 1 2001-Bushhttps://stackoverflow.com/questions/67677033
复制相似问题