【发布时间】:2018-04-04 22:26:59
【问题描述】:
我的团队正在使用 R 中的 Quanteda 包对中等大小的文本块(数万个单词)进行一些主题建模。我想在主题建模过程之前将单词简化为词干,以便我没有将同一个词的变体计算为不同的主题。
唯一的问题是词干算法会留下一些不是真正单词的单词。 “幸福”源于“快乐”,“安排”源于“安排”,依此类推。所以,在我可视化主题建模的结果之前,我想恢复词干以完成单词。
通过阅读 StackOverflow 上的一些以前的线程,我发现了一个来自 TM 包的函数 stemCompletion(),它可以执行此操作,at least approximately。它似乎工作得相当好。
但是当我将它应用于文档文本矩阵中的术语向量时,stemCompletion() 总是替换字符向量的名称,而不是字符本身。这是一个可重现的示例:
# Set up libraries
library(janeaustenr)
library(quanteda)
library(tm)
# Get first 200 words of Mansfield Park
words <- head(mansfieldpark, 200)
# Build a corpus from words
corpus <- quanteda::corpus(words)
# Eliminate some words from counting process
STOPWORDS <- c("the", "and", "a", "an")
# Create a document text matrix and do topic modeling
dtm <- corpus %>%
quanteda::dfm(remove_punct = TRUE,
remove = STOPWORDS) %>%
quanteda::dfm_wordstem(.) %>% # Word stemming takes place here
quanteda::convert("topicmodels")
# Word stems are now stored in dtm$dimnames$Terms
# View a sample of stemmed terms
tail(dtm$dimnames$Terms, 20)
# View the structure of dtm$dimnames$Terms (It's just a character vector)
str(dtm$dimnames$Terms)
# Apply tm::stemCompletion to Terms
unstemmed_terms <-
tm::stemCompletion(dtm$dimnames$Terms,
dictionary = words, # or corpus
type = "shortest")
# Result is composed entirely of NAs, with the values stored as names!
str(unstemmed_terms)
tail(unstemmed_terms, 20)
我正在寻找一种方法来将 stemCompletion() 返回的结果转换为字符向量,而不是转换为字符向量的名称属性。对此问题的任何见解都非常感谢。
【问题讨论】:
-
我之前没有使用过 stemCompletion 但除非我错过了什么,否则
names(x)是一个包含x名称的字符向量?这听起来像你在找什么?像这样:unstemmed_terms <- names(unstemmed_terms) -
这很简单,但很有效。谢谢!我仍然对 为什么 stemCompletion 将其结果存储在 names 属性中感到好奇,但这很有帮助。
-
stemCompletion()不会将结果存储在返回的字符向量的名称属性中。相反,名称是词干完成的词,词干完成的词是向量元素。 -
对于这个例子,你也不需要加载 tidyverse 包。
-
@KenBenoit 谢谢;我把 tidyverse 拿出来了。习惯的力量。
标签: r tm topic-modeling quanteda