R Sentiment Analysis Tutorial

In R language, conducting sentiment analysis and word frequency count often requires the use of additional packages, such as tm and sentimentr. Here is a simple example code that can be used for sentiment analysis and word frequency count:

# 安装和加载必要的包
install.packages("tm")
install.packages("sentimentr")
library(tm)
library(sentimentr)

# 创建一个文本向量
text <- c("这个电影非常好看,情节紵紵有致,演员表现也很出色。",
          "这个餐厅的服务太差了,食物也不好吃。")

# 创建一个语料库
corpus <- VCorpus(VectorSource(text))

# 进行文本预处理
corpus <- tm_map(corpus, content_transformer(tolower))
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, removeWords, stopwords("en"))

# 进行情感分析
sentiment <- sentiment(corpus)

# 输出情感分析结果
print(sentiment$all)

# 进行词频统计
tdm <- TermDocumentMatrix(corpus)
freq <- rowSums(as.matrix(tdm))
freq <- subset(freq, freq > 0)
freq <- sort(freq, decreasing = TRUE)

# 输出词频统计结果
print(freq)

This code initially creates a vector containing two texts, then converts it into a corpus for text preprocessing. Next, it performs sentiment analysis using the sentimentr package and outputs the results. Finally, it uses the TermDocumentMatrix function in the tm package for word frequency statistics and outputs the results.

This code segment makes it easy to conduct sentiment analysis and word frequency statistics, allowing for further modification and expansion based on specific needs.

bannerAds