【发布时间】: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 directoryfiles <- 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