【发布时间】:2018-08-16 17:33:48
【问题描述】:
我在使用 tm 包完成创建的语料库时遇到词干问题。
这是我的代码中最重要的几行:
# Build a corpus, and specify the source to be character vectors
corpus <- Corpus(VectorSource(comments_final$textOriginal))
corpus
# Convert to lower case
corpus <- tm_map(corpus, content_transformer(tolower))
# Remove URLs
removeURL <- function(x) gsub("http[^[:space:]]*", "", x)
corpus <- tm_map(corpus, content_transformer(removeURL))
# Remove anything other than English letters or space
removeNumPunct <- function(x) gsub("[^[:alpha:][:space:]]*", "", x)
corpus <- tm_map(corpus, content_transformer(removeNumPunct))
# Remove stopwords
myStopwords <- c(setdiff(stopwords('english'), c("r", "big")),
"use", "see", "used", "via", "amp")
corpus <- tm_map(corpus, removeWords, myStopwords)
# Remove extra whitespace
corpus <- tm_map(corpus, stripWhitespace)
# Remove other languages or more specifically anything with a non "a-z" and "0-9" character
corpus <- tm_map(corpus, content_transformer(function(s){
gsub(pattern = '[^a-zA-Z0-9\\s]+',
x = s,
replacement = " ",
ignore.case = TRUE,
perl = TRUE)
}))
# Keep a copy of the generated corpus for stem completion later as dictionary
corpus_copy <- corpus
# Stemming words of corpus
corpus <- tm_map(corpus, stemDocument, language="english")
现在为了完成词干提取,我应用了 tm 包的 stemCompletion。
# Completing the stemming with the generated dictionary
corpus <- tm_map(corpus, content_transformer(stemCompletion), dictionary = corpus_copy, type="prevalent")
但是,这就是我的语料库被破坏和弄乱的地方,并且 stemCompletion 无法正常工作。特别的是,R 并不表示错误,代码运行但结果很糟糕。 p>
有人知道解决方案吗?顺便说一句,我的“cmets_final”数据框由我使用 tubeR 包下载的 youtube cmets 组成。
非常感谢您提前提供的帮助,我的硕士论文真的需要帮助,谢谢。
【问题讨论】: