stemCompletion无法正常工作

问题描述 投票:0回答:3

我正在尝试使用stemCompletion将词干转换成完整的单词。

以下是我正在使用的代码

txt <- c("Once we have a corpus we typically want to modify the documents in it",
     "e.g., stemming, stopword removal, et cetera.",
     "In tm, all this functionality is subsumed into the concept of a transformation.")

myCorpus <- Corpus(VectorSource(txt))
myCorpus <- tm_map(myCorpus, content_transformer(tolower))
myCorpus <- tm_map(myCorpus, removePunctuation)
myCorpusCopy <- myCorpus

# *Removing common word endings* (e.g., "ing", "es") 
myCorpus.stemmed <- tm_map(myCorpus, stemDocument, language = "english")
myCorpus.unstemmed <- tm_map(myCorpus.stemmed, stemCompletion, dictionary=myCorpusCopy)

如果我检查词干语料库的第一个元素,它会正确显示元素

myCorpus.stemmed[[1]][1]
$content
[1] "onc we have a corpus we typic want to modifi the document in it"

但是,如果我检查未受干扰的语料库的第一个元素,它会抛出垃圾

myCorpus.unstemmed[[1]][1]
$content
[1] NA

为什么未受干扰的语料库没有显示正确的内容?

r text-mining tm stemming
3个回答
0
投票

为什么未受干扰的语料库没有显示正确的内容?

由于你有一个简单的语料库对象,你实际上是在调用

stemCompletion(
  x = c("once we have a corpus we typically want to modify the documents in it", 
        "eg stemming stopword removal et cetera", 
        "in tm all this functionality is subsumed into the concept of a transformation"),
  dictionary=myCorpusCopy
)

产量

# once we have a corpus we typically want to modify the documents in it 
# NA 
# eg stemming stopword removal et cetera 
# NA 
# in tm all this functionality is subsumed into the concept of a transformation 
# NA 

由于stemCompletion等待词干的字符向量作为第一个参数(c("once", "we", "have")),而不是词干文本的字符向量(c("once we have"))。

如果你想完成你的语料库中的词干,不管这应该是什么好的,你必须将单词的字符向量传递给stemCompletion(即标记每个文本文档,干完成词干,然后再将它们粘贴在一起) )。


0
投票

我只是略微熟悉TM,但没有stemCompletion要求令牌是词干而不是已经完成的单词。


0
投票

感谢Luke给出的答案,我寻找了一个可以帮助将示例文本转换为字符向量的函数。

我遇到了this answer的另一个问题,它提供了一个自定义函数,可以在应用stemCompletion函数之前将文本转换为单个单词。

stemCompletion_mod <- function(x,dict=dictCorpus) {
 PlainTextDocument(stripWhitespace(paste(stemCompletion(unlist(strsplit(as.character(x)," ")),dictionary=dict, type="shortest"),sep="", collapse=" ")))
}

我将该函数与lapply结合使用,以获得包含未经编辑的版本的列表。这将返回正确的值,但不在SimpleCorpus数据类型中!我需要对输出列表进行一些处理,将其转换为SimpleCorpus数据类型。

myCorpus.unstemmed <- lapply(myCorpus.stemmed, stemCompletion_mod, dict = myCorpusCopy)

> myCorpus.stemmed[[1]][1]
$content
[1] "onc we have a corpus we typic want to modifi the document in it"

 > myCorpus.unstemmed[[1]][1]
$content
[1] "once we have a corpus we typically want to the documents in it"

我不知道为什么stemCompletion没有完成modifi。但这将是另一个需要探索的问题的一部分。

© www.soinside.com 2019 - 2024. All rights reserved.