【问题标题】:Dictionary() is not supported anymore in tm package. How to emend code?tm 包中不再支持 Dictionary()。如何修改代码?
【发布时间】:2014-03-14 11:06:48
【问题描述】:

我刚刚注意到,在更新到 tm v. 0.5-10 后,不再支持函数 Dictionary()。这是一个错误吗?还是被弃用了?我想使用另一个函数来创建字典吗?

由于我现在有很多行代码要修改,在不进行所有工程的情况下,最好的方法是什么?

【问题讨论】:

  • 来自包装新闻:“改用字符向量;使用 Terms() 从文档术语或术语文档矩阵中提取术语”cran.r-project.org/web/packages/tm/news.html
  • @Ben Terms() 不完全复制 Dictionary() Error in UseMethod("Terms") : no applicable method for 'Terms' applied to an object of class "character"

标签: r tm


【解决方案1】:

如果您按照@Ben 的建议使用 Dictionary,我认为您可以创建一个名为 Dictionary 的虚拟函数,它只返回您传递给它的字符向量。

Dictionary <- function(x) {
    if( is.character(x) ) {
        return (x)
    }
    stop('x is not a character vector')
}

但是,从长远来看,卷起袖子重构代码可能会更好。

【讨论】:

    【解决方案2】:

    来自 NEWS 文件的更完整的摘录:

    \subsection{DEPRECATED & DEFUNCT}{
        \itemize{
          \item Following functions have been removed:
          \itemize{
            \item \code{Dictionary()} (use a character vector instead; use
              \code{Terms()} to extract terms from a document-term or term-document 
              matrix),
    

    所以。是的,它已被弃用和删除。正如 Ben 所建议的,作者打算让您使用 Terms()。为什么你得到错误只是一个闲置推测的问题,因为你没有提供数据对象和代码抛出错误。一种猜测是您提供的对象不是 TDM 或 DTM。

    【讨论】:

      【解决方案3】:

      正如 IShouldBuyABoat 所说,您没有向我们提供有关您如何使用 Dictionary 的任何线索,因此我们无法真正为您提供任何具体答案(请更新您的问题以提供更多详细信息)。

      无论如何,您的“如何更新我的代码”问题的答案可能是“只需删除 Dictionary 就可以了”,您可以在此处看到:

      library(tm)
      data(crude)
      

      了解 Dictionary 在早期版本的 tm 包中做了什么:

      methods(Dictionary)
      getAnywhere(Dictionary.DocumentTermMatrix)
      # function(x) structure(Terms(x), class = c("Dictionary", "character"))
      getAnywhere(Dictionary.character)
      # function (x)  structure(x, class = c("Dictionary", "character"))
      

      无论如何,这是一种毫无意义的功能,删除它似乎很明智。但是如何更新依赖它的代码呢?

      您可能曾像这样使用过Dictionary

      myDictionary <- Dictionary(c("some", "tokens", "that", "I", "am", "interested", "in"))
      inspect(DocumentTermMatrix(crude, list(dictionary = myDictionary)))
      

      现在这个函数不再可用,你可以改为使用字符向量:'

      myTerms <- c("some", "tokens", "that", "I", "am", "interested", "in")
      inspect(DocumentTermMatrix(crude, list(dictionary = myTerms)))
      

      这两个示例的输出是相同的,第一个使用 tm 版本 0.5-9,第二个使用版本 0.5-10

      NEWS 中使用Terms 的指令是,如果您想获取文档术语矩阵中的所有单词,就像这样

      Terms(DocumentTermMatrix(crude))
      

      如果这些都对你没有帮助,那么你需要提供更多关于你想要做什么的细节。

      【讨论】:

        猜你喜欢
        • 2021-09-28
        • 2020-06-18
        • 2022-08-24
        • 2015-09-26
        • 2014-01-29
        • 1970-01-01
        • 1970-01-01
        • 2021-07-20
        • 2014-12-14
        相关资源
        最近更新 更多