【问题标题】:Issue with stemCompletion of Corpus for text mining in R (tm package)R(tm 包)中用于文本挖掘的语料库的 stemCompletion 问题
【发布时间】: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 组成。

非常感谢您提前提供的帮助,我的硕士论文真的需要帮助,谢谢。

【问题讨论】:

    标签: r tm corpus stemming


    【解决方案1】:

    它的工作方式似乎有点奇怪,所以我想出了自己的 stemCompletion 函数并将其应用于语料库。在你的情况下试试这个:

        stemCompletion2 <- function(x, dictionary) {
        # split each word and store it    
        x <- unlist(strsplit(as.character(x), " "))
        # # Oddly, stemCompletion completes an empty string to
        # a word in dictionary. Remove empty string to avoid issue.
        x <- x[x != ""]
        x <- stemCompletion(x, dictionary=dictionary)
        x <- paste(x, sep="", collapse=" ")
        PlainTextDocument(stripWhitespace(x))
        }
    
        corpus <- lapply(corpus, stemCompletion2, corpus_copy)
        corpus <- as.VCorpus(corpus)`
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 2018-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多