【问题标题】:Stem Completion in R showing ErrorR中的词干完成显示错误
【发布时间】:2014-11-09 21:00:41
【问题描述】:

我在 CSV 文件中有 10 行文本数据。我想纠正各种拼写错误。例如单词“battery”(拼写错误为“battere”或“batt”等)。我考虑使用 StemDocument 后跟 stemCompletion,因此使用了以下代码:

library(tm)
library(SnowballC)
text.var<-read.csv("C:\\Users\\Sambit\\Desktop\\Sample Data.csv",header=FALSE)
data_corp<-Corpus(VectorSource(text.var))

data_corp.copy<-data_corp
data_corp<-tm_map(data_corp, stemDocument)
data_corp<-tm_map(data_corp, stemCompletion, dictionary=data_corp.copy)

但是,最后一步,即 Stem Completion 步骤显示以下错误:

Error in setNames(if (length(n)) n else rep(NA, length(x)), x) : 
  'names' attribute [10] must be the same length as the vector [2]
In addition: Warning messages:
1: In grep(sprintf("^%s", w), dictionary, value = TRUE) :
  argument 'pattern' has length > 1 and only the first element will be used
2: In grep(sprintf("^%s", w), dictionary, value = TRUE) :
  argument 'pattern' has length > 1 and only the first element will be used

我哪里可能出错了?

【问题讨论】:

  • 试试这个解决方法:stackoverflow.com/a/26696490/1036500
  • 它最有效。感谢那。但在一种情况下,“battery”被拼写为“batteri”;并且它的 StemCompletion 给出 NA;而“batter”被正确识别为“bettery”。有什么建议吗?
  • 不太确定,似乎拼写错误与原始单词差异太大,以至于词干没有用处。如果您知道所有拼写错误的单词以及它们应该是什么,您可以尝试使用字典方法
  • 其实“batteri”这个词在词干后没有变化;这就是它不受 StemCompletion 影响的原因。我想知道为什么:/

标签: r text-mining


【解决方案1】:

在 3.1.2 版本的 R 中,Stem 完成不工作。我写了类似的功能,但它真的很慢。

manualStemCorpus <- function(x) {
  words <- scan_tokenizer(x[[1]])
  for (i in seq(x) ) {
    words <- c(words, scan_tokenizer(x[[i]]))
  }

  words <- unique(sort(words))
  words.st <- stemDocument(words, language='english') 

  x <- tm_map(x, stemDocument, language = 'english')

  for (i in seq(words) ) {
    print(i)
    for (j in 1:length(x)){
      patt <- paste0('\\b', words.st[i], '\\b')
      x[[j]]<- gsub(patt, words[i], x[[j]], fixed=TRUE)
    }
  }

  return(x)

}

你可以检查一下

numberOfWords <- function(x) {
  a <- scan_tokenizer(x[[1]])
  for (i in seq(x) ) {
    a <- c(a, scan_tokenizer(x[[i]]) )
  }
  a <- unique(sort(a))
  return(length(a))
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 2011-10-03
    • 2014-10-01
    • 2020-11-26
    相关资源
    最近更新 更多