【问题标题】:Sentiment Analysis in R with tidyverse package - object 'sentiment' not found带有tidyverse包的R中的情感分析-找不到对象“情感”
【发布时间】:2018-02-20 02:22:51
【问题描述】:

我正在尝试重现此情绪分析示例:https://www.kaggle.com/rtatman/tutorial-sentiment-analysis-in-r

我有一个“file.txt”,其中包含我要在“../input”文件夹中分析的文本。

library(tidyverse)
library(tidytext)
library(glue)
library(stringr)
library(dplyr)
require(plyr)

# get a list of the files in the input directory
files <- list.files("../input")
fileName <- glue("../input/", files[1], sep = "")
fileName <- trimws(fileName)
fileText <- glue(read_file(fileName))
fileText <- gsub("\\$", "", fileText)
tokens <- data_frame(text = fileText) %>% unnest_tokens(word, text)

但在这一行之后

#get the sentiment from the first text: 
tokens %>%
  inner_join(get_sentiments("bing")) %>% # pull out only sentiment words
  count(sentiment) %>% # count the # of positive & negative words
  spread(sentiment, n, fill = 0) %>% # made data wide rather than narrow
  mutate(sentiment = positive - negative) # # of positive words - # of negative owrds

我收到一条错误消息

计数错误(.,情绪):找不到对象“情绪”

昨天同样的代码运行良好,今天我得到了这个错误。看来问题是由plyr 包引起的。在 dplyr 之前加载 plyr 时似乎工作正常,但现在即使按该顺序加载也会出错。

【问题讨论】:

  • 欢迎来到 Stack Overflow。如果你想得到一些帮助,你能提供一个示例数据吗?目前,没有人可以访问您的数据。底线是提供最少的可重现数据和您的代码。看看this post。同时错误消息告诉您没有名为sentiment 的列。在你使用count()之前似乎有问题。
  • 它对我有用。上面给出了Joining, by = "word" # A tibble: 1 x 3 negative positive sentiment <dbl> <dbl> <dbl> 1 117 240 123你数据读对了吗?检查glue这一步看你是否读对了
  • @jazzurro 谢谢你的建议。我编辑了我的帖子以提供更多详细信息。我还有什么需要补充的吗? @akrun 我昨天确实得到了相同的输出(即每个文档的负字数和正字数),但今天它给出了一个错误。我认为已正确阅读,当我输入“令牌”时,它会为我提供我试图分析的文档中的前 10 个令牌。然而,下一行给出了一个错误。
  • 你能停下来看看吗? tokens %>% inner_join(get_sentiments("bing"))
  • @Gangesh Dubey 是的,这可以正常工作,并给了我积极和消极的小玩笑。这是否意味着问题出在count(sentiment)?我怎样才能修复 ti?

标签: r sentiment-analysis tidytext


【解决方案1】:

问题是由plyr 包与dplyr 一起加载引起的。我使用this approach 使用plyr 而不加载它,现在代码运行没有任何错误。

【讨论】:

    【解决方案2】:

    我遇到了同样的错误,即使没有加载 plyr 包,您可以在调用“count”函数时使用显式包来修复它:

    dplyr::count(sentiment)
    

    它应该看起来像这样:

    #get the sentiment from the first text: 
    tokens %>%
      inner_join(get_sentiments("bing")) %>% # pull out only sentiment words
      dplyr::count(sentiment) %>% # count the # of positive & negative words
      spread(sentiment, n, fill = 0) %>% # made data wide rather than narrow
      mutate(sentiment = positive - negative) # # of positive words - # of negative owrds
    

    【讨论】:

    • 谢谢,这很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多