我使用quanteda从一个令牌对象创建一个文档特征矩阵(dfm)。我的tokens对象包含许多ngram(例如:"united_states")。当我使用dfm()函数创建一个dfm时,我的ngram被底层核心拆分("united_states“被拆分成”联合“”状态“)。如何在维护ngram的同时创建dfm?
下面是我的流程:
my_tokens <- tokens(my_corpus, remove_symbols=TRUE, remove_punct = TRUE, remove_numbers = TRUE)
my_tokens <- tokens_compound(pattern=phrase(my_ngrams))
my_dfm <- dfm(my_tokens, stem= FALSE, tolower=TRUE)我在my_tokens中看到了"united_states“,但在dfm中,它变成了”联合“和”状态“作为单独的标记。
感谢您能提供的任何帮助!
发布于 2020-08-10 23:57:49
不清楚您使用的是哪个版本的quanteda,但基本上这应该是可行的,因为默认的记号赋予器(来自tokens())不会拆分包含内部_的单词。
演示:
library("quanteda")
## Package version: 2.1.1
# tokens() will not separate _ words
tokens("united_states")
## Tokens consisting of 1 document.
## text1 :
## [1] "united_states"下面是一个可重复使用的短语“美国”的例子:
my_corpus <- tail(data_corpus_inaugural, 3)
# show that the phrase exists
head(kwic(my_corpus, phrase("united states"), window = 2))
##
## [2009-Obama, 2685:2686] bless the | United States | of America
## [2013-Obama, 13:14] of the | United States | Congress,
## [2013-Obama, 2313:2314] bless these | United States | of America
## [2017-Trump, 347:348] , the | United States | of America
## [2017-Trump, 1143:1144] to the | United States | of America
my_tokens <- tokens(my_corpus,
remove_symbols = TRUE,
remove_punct = TRUE, remove_numbers = TRUE
)
my_tokens <- tokens_compound(my_tokens, pattern = phrase("united states"))
my_dfm <- dfm(my_tokens, stem = FALSE, tolower = TRUE)
dfm_select(my_dfm, "*_*")
## Document-feature matrix of: 3 documents, 1 feature (0.0% sparse) and 4 docvars.
## features
## docs united_states
## 2009-Obama 1
## 2013-Obama 2
## 2017-Trump 2https://stackoverflow.com/questions/63273318
复制相似问题