首页 > 解决方案 > 如何从带有文本和日期值的 .csv 文件中的列表中计算特定正面/负面词的频率?在 R 中

问题描述

我正在尝试从包含消息、特定用户和日期的文档中获取情绪。我已经清理了两个文档,以便其中包含的单词具有标准格式,然后我尝试计算它们,但我似乎能够单独计算它们(在定义单词之后),但不能使用列表字。

文件。raw 格式为:text,user_id, date,正/负列表格式为:id,word_cz,polarity

file.raw <- read.csv("/Users/tomas/Desktop/Repromeda - Repromeda 3.csv", stringsAsFactors = FALSE,)
positive <- read.csv("/Users/tomas/Desktop/positive.txt", stringsAsFactors = FALSE,)
negative <- read.csv("/Users/tomas/Desktop/negative.txt", stringsAsFactors = FALSE,)

我可以用函数计算特定的单词,比如“Okay”

getCount <- function(data,keywords)
{
  wordcount <- str_count(file.raw&text, keywords)
  return(data.frame(data,wordcount))
}
file.raw$count <-  getCount(file.raw&text,"okay")

)但我似乎无法找到一种方法来使用单词列表来自动化这个过程

理想的结果会为每行的每个正数和负数添加一列

谢谢您的帮助

标签: rlistcsvcountsentiment-analysis

解决方案


这个怎么样?

library(stringr)
data <- "yes i had a great time yesterday having fun but your lame actions were disturbing, ok?"
positive <- c("yes" , "ok", "fun", "great")
negative <- c("lame" , "disturbing", "no") 

sapply(positive, function(x) str_count(data,x))
sapply(negative, function(x) str_count(data,x))

推荐阅读