簡體   English   中英

使用 tidyverse 包在 R 中進行情緒分析 - 找不到對象“情緒”

[英]Sentiment Analysis in R with tidyverse package - object 'sentiment' not found

我試圖重現這個情感分析的例子: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包引起的。 plyrdplyr之前加載時,它似乎工作正常,但即使它們按該順序加載,現在也會出現錯誤。

該問題是由於plyr軟件包與dplyr一起加載引起的。 我使用這種方法來使用plyr而不加載它,並且代碼現在運行時沒有任何錯誤。

我遇到了同樣的錯誤,即使沒有加載 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM