【发布时间】:2017-04-12 07:12:40
【问题描述】:
我在 R 中获得了一个代码,用于提取文本列表的情绪并将其保存在数据框中,用于情绪分析项目。我是 r 和 coreNLP 的新手,所以我一直在解决内存等问题,但我仍然不确定如何解决所有问题。代码中的tripadvisor 数据框包含来自TripAdvisor 网页的评论,我想从中提取情绪。 TripAdvisor$titleopinion 是包含此数据的列。
我得到的错误是:Error in rJava::.jcall(volatiles$cNLP, "Ledu/stanford/nlp/pipeline/Annotation;", : 带有签名的方法流程 (I)Ledu/stanford/nlp/pipeline/Annotation;没找到
我在每个 R 会话中运行的命令是:
- Sys.setenv(JAVA_HOME='C:\Program Files\Java\jre1.8.0_121')
- 选项(java.parameters = "-Xmx8g")
我的电脑有 8G 内存,我有时会出现内存不足的问题。我正在加载的 dali1.csv 包含大约 450 个我想从中提取情绪的文本实例。
代码如下:
library(data.table)
library(devtools)
devtools::install_github("statsmaths/coreNLP")
#coreNLP::downloadCoreNLP()
library(coreNLP)
initCoreNLP("C:/TFG/stanford-corenlp-full-2016-10-31")
# Read the data
TripAdvisor <- read.csv("C:/TFG/Data/dali/dali1ENG.csv")
# Creating sentiment label
TripAdvisor$SentimentValue <- NA
TripAdvisor$SentimentValue <- ifelse(TripAdvisor$rating <= 2, "negative",
ifelse(TripAdvisor$rating == 3, "neutral",
ifelse(TripAdvisor$rating >= 4, "positive", TripAdvisor$SentimentValue)))
# Predict sentiment with coreNLP
TripAdvisor$SentimentCoreNLP <- NA
for(i in 1:nrow(TripAdvisor)){
print(i)
pos <- 0
neg <- 0
opinion <- TripAdvisor$titleopinion[i]
opinion.df <- getSentiment(annotateString(opinion))
for(j in 1:nrow(opinion.df)){
if(opinion.df$sentiment[j]=="Verypositive"){
pos = pos + 2
} else if(opinion.df$sentiment[j]=="Positive"){
pos = pos + 1
} else if(opinion.df$sentiment[j]=="Negative"){
neg = neg + 1
} else if(opinion.df$sentiment[j]=="Verynegative"){
neg = neg + 2
}
}
TripAdvisor$pos[i] <- pos
TripAdvisor$neg[i] <- neg
}
TripAdvisor$SentimentCoreNLP <- ifelse(TripAdvisor$pos > TripAdvisor$neg, "positive",
ifelse(TripAdvisor$pos < TripAdvisor$neg, "negative", "neutral"))
write.csv(TripAdvisor, file="C:/TFG/Data/dali/daliXENG.csv")
# Analysing SentimentValue vs. SentimentCoreNLP
# Table
table(TripAdvisor$SentimentCoreNLP, TripAdvisor$SentimentValue)
#100*(table(TripAdvisor$SentimentCoreNLP, TripAdvisor$SentimentValue)/(nrow(TripAdvisor)))
这个代码应该可以工作,给我它的人在具有 i3 和 8G RAM 的机器上使用它没有问题。欢迎和赞赏有关内存问题和此注释器缺失的任何见解。对不起我的英语不好,我还在学习:)
如果我遗漏了一些必需的信息,请告诉我,以便我提供。
【问题讨论】:
标签: r stanford-nlp sentiment-analysis