stemCompletion 无法正常工作

stemCompletion is not working properly

我正在尝试使用 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

为什么未提取词干的语料库没有显示正确的内容?

Why is the unstemmed corpus not showing the right content?

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

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(即标记化每个文本文档,词干-完成词干,然后再次将它们粘贴在一起)。

我对 TM 只是略微熟悉,但 stemCompletion 不要求标记是词干而不是已经完成的单词。

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

我遇到了另一个关于 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 结合起来得到一个包含无词干版本的列表。这个 returns 是正确的值,但它不在 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 没有完成修改。但这将是另一个需要探索的问题的一部分。