Sentiment analysis of an Aljazeera article
text1_Lines <- readLines("G7/Aljazeera.txt")
text1_Lines[1]
[1] "Top diplomats from the Group of Seven (G7) have called for “humanitarian pauses” in Israel’s bombardment in order to deliver aid to desperate Palestinian civilians in the Gaza Strip."
(text1_1_words <- get_tokens(text1_Lines[1]))
[1] "top" "diplomats" "from" "the"
[5] "group" "of" "seven" "g7"
[9] "have" "called" "for" "humanitarian"
[13] "pauses" "in" "israel" "s"
[17] "bombardment" "in" "order" "to"
[21] "deliver" "aid" "to" "desperate"
[25] "palestinian" "civilians" "in" "the"
[29] "gaza" "strip"
text1_1_sentiment_scores <- get_nrc_sentiment(text1_1_words)
row.names(text1_1_sentiment_scores)
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14"
[15] "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28"
[29] "29" "30"
row.names(text1_1_sentiment_scores)<-NULL
row.names(text1_1_sentiment_scores)<-make.unique(text1_1_words)
View(prop.table(text1_1_sentiment_scores[, 1:8]))
“top”のsentiment_score
(tmp<-text1_1_sentiment_scores[1,1:8])
text1_1_sentiment_scores[, 1:8]
sum(text1_1_sentiment_scores[, 1:8])
[1] 9
tmp/sum(text1_1_sentiment_scores[, 1:8])
全テキスト感情得点の取得
text1_string <- get_text_as_string("G7/Aljazeera.txt")
text1_words <- get_tokens(text1_string)
text1_sentiment_scores <- get_nrc_sentiment(text1_words)
感情カテゴリ別集計
SentTotal<-colSums(prop.table(text1_sentiment_scores[, 1:8]))
sort(SentTotal,decreasing=TRUE)
trust fear anticipation joy sadness
0.28571429 0.20000000 0.15714286 0.10000000 0.10000000
anger surprise disgust
0.08571429 0.05714286 0.01428571
感情カテゴリ内単語情報:trust
id<-which(text1_sentiment_scores$trust>0)
sort(table(text1_words[id]),decreasing=TRUE)
food humanitarian bank medical statement
3 3 2 2 2
agreed committed continue leading peace
1 1 1 1 1
shelter top united
1 1 1
感情カテゴリ内単語情報:fear
id<-which(text1_sentiment_scores$fear>0)
sort(table(text1_words[id]),decreasing=TRUE)
war bombardment medical broke condemnation
3 2 2 1 1
displaced fire military urgent violence
1 1 1 1 1
#最大、最小データの準備
index<-1:8
SentimentLabels <- names(text1_sentiment_scores)
maxmin <- data.frame(
lapply(index,function(i) assign(SentimentLabels[i], c(0.3,0), envir = globalenv()) )
)
names(maxmin)<-SentimentLabels[index]
scores <-colSums(prop.table(text1_sentiment_scores[, 1:8]))
dat <- data.frame(
lapply(index,function(i) assign(sentiments[i], scores[i], envir = globalenv()) )
)
names(dat)<-SentimentLabels[index]
dat <- rbind(maxmin, dat) #データの結合
####radarchartの設定#####
#centerzero = TRUEで中心が0
#axistype:軸基準設定,0:無し, 1:割合, 2:実数, 3:割合,実数, 4:最大を1, 5:最大を1,実数
#seg:分割数
#plty:線の種類
#vlcex:ラベルの大きさ
radarchart(dat, axistype = 2, seg = 8, plty = 1, pcol="blue", vlcex = 1.,
centerzero = TRUE, vlabels = colnames(dat),cglcol ="gray",
title = "NRC Emotions in AL JAZEERA Text (8 Nov 2023)")

# default: pcol=1:8 (1:black, 2:red, 3:green, 4:blue, 5:cyan, 6:magenta, 7:yellow, 8:gray)
text2_string <- get_text_as_string("G7/BBC.txt")
text2_words <- get_tokens(text2_string)
text2_sentiment_scores <- get_nrc_sentiment(text2_words)
#最大、最小データの準備
index<-1:8
SentimentLabels <- names(text1_sentiment_scores)
maxmin <- data.frame(
lapply(index,function(i) assign(SentimentLabels[i], c(0.3,0), envir = globalenv()) )
)
names(maxmin)<-SentimentLabels[index]
scores1<-colSums(prop.table(text1_sentiment_scores[, 1:8]))
scores2<-colSums(prop.table(text2_sentiment_scores[, 1:8]))
scores <-mapply(c,scores1,scores2)
RNGkind("Mersenne-Twister")
dat <- data.frame(
lapply(index,function(i) assign(sentiments[i], scores[,i], envir = globalenv()) )
)
names(dat)<-SentimentLabels[index]
dat <- rbind(maxmin, dat)
radarchart(dat, axistype = 2, seg = 8, plty = 1, vlcex = 1., pcol=c(2,3),
centerzero = TRUE, vlabels = colnames(dat), cglcol ="gray",
title = "NRC Emotions in AL JAZEERA & BBC (8 Nov 2023)")
