我正试图借助我的DTM(文档术语矩阵)或TDM(术语文档矩阵)在R中进行情感分析。我在论坛和Google上找不到任何类似的主题。因此,我创建了一个语料库,并从该语料库中在R中生成了dtm / tdm。下一步是应用情感分析,稍后需要通过SVM进行库存预测。我给的代码是:
dtm <- DocumentTermMatrix(docs)
dtm <- removeSparseTerms(dtm, 0.99)
dtm <- as.data.frame(as.matrix(dtm))
tdm <- TermDocumentMatrix(docs)
tdm <- removeSparseTerms(tdm, 0.99)
tdm <- as.data.frame(as.matrix(tdm))
我读到有可能在get_sentiments()函数的帮助下通过tidytext包。但是不可能将其应用于DTM / TDM。我如何对已过滤,已词干化的已清除过滤词进行情绪分析?我看到很多人都对一个空洞句子进行了情感分析,但是我想将其应用到我的单个单词中,以查看它们是否是肯定的,否定的,得分等。
答案 0 :(得分:1)
SentimentAnalysis
与tm
集成良好。
library(tm)
library(SentimentAnalysis)
documents <- c("Wow, I really like the new light sabers!",
"That book was excellent.",
"R is a fantastic language.",
"The service in this restaurant was miserable.",
"This is neither positive or negative.",
"The waiter forget about my dessert -- what poor service!")
vc <- VCorpus(VectorSource(documents))
dtm <- DocumentTermMatrix(vc)
analyzeSentiment(dtm,
rules=list(
"SentimentLM"=list(
ruleSentiment, loadDictionaryLM()
),
"SentimentQDAP"=list(
ruleSentiment, loadDictionaryQDAP()
)
)
)
# SentimentLM SentimentQDAP
# 1 0.000 0.1428571
# 2 0.000 0.0000000
# 3 0.000 0.0000000
# 4 0.000 0.0000000
# 5 0.000 0.0000000
# 6 -0.125 -0.2500000
答案 1 :(得分:0)
要在dtm上使用tidytext来获取情感,请先将dtm转换为整齐的格式,然后在整洁的数据和极化词词典之间进行内部联接。我将使用与上面相同的文档。上面示例中的某些文档是肯定的,但给出了中立的分数。 让我们看看tidytext的表现
library(tidytext)
library(tm)
library(dplyr)
library(tidyr)
documents <- c("Wow I really like the new light sabers",
"That book was excellent",
"R is a fantastic language",
"The service in this restaurant was miserable",
"This is neither positive or negative",
"The waiter forget about my dessert -- what poor service")
# create tidy format
vectors <- as.character(documents)
v_source <- VectorSource(vectors)
corpuss <- VCorpus(v_source)
dtm <- DocumentTermMatrix(corpuss)
as_tidy <- tidy(dtm)
# Using bing lexicon: you can use other as well(nrc/afinn)
bing <- get_sentiments("bing")
as_bing_words <- inner_join(as_tidy,bing,by = c("term"="word"))
# check positive and negative words
as_bing_words
# set index for documents number
index <- as_bing_words%>%mutate(doc=as.numeric(document))
# count by index and sentiment
index <- index %>% count(sentiment,doc)
# spread into positives and negavtives
index <- index %>% spread(sentiment,n,fill=0)
# add polarity scorer
index <- index %>% mutate(polarity = positive-negative)
index
Doc 4和6是负数,5表示中立,而其余部分是正数