getting sentimental about ‘sentiment’ analysis

infoart.ca
2 min readJul 11, 2021

--

Wanted to read War and Peace, since always… finally starting to do it in R:

Let’s feed first few pages of the text to R:

myTolstoy <- readLines("https://github.com/ghose-gh/text-r/edit/main/War_n_Peace.txt")TolstoyLines<- get_sentences(myTolstoy)

Runtime show:

231 lines…

Good time to check the sentiment for each…

sentiment <- get_sentiment(TolstoyLines)

And… so much to see, so much to feel…

Now some quick numbers…

> sum(sentiment)
[1] 127.2
> mean(sentiment)
[1] 0.5506494

Being Tolstoy was not easy… from -3 to 5.5!

> summary(sentiment)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-2.9500 0.0000 0.5000 0.5506 1.2500 5.5000

And some finale graphs…

plot(
sentiment,
type="l",
main="Example Plot Trajectory",
xlab = "Narrative Time",
ylab= "Emotional Valence"
)

let’s get the percentage values…

ssn<- get_percentage_values(sentiment)

making it a bit more readable:

Now to plot the percentage values:

plot(
ssn,
type="l",
main="O Tolstoy!",
xlab = "Narrative Time",
ylab= "Emotional Valence",
col="red"
)

which give us…

Now before I go, let’s try transformed values instead of percentage, to get a more grained picture of the ups and downs:

transformed <- get_transformed_values(
sentiment,
low_pass_size = 3,
x_reverse_len = 100,
scale_vals = TRUE,
scale_range = FALSE
)

And the plot:

plot(
transformed,
type="l",
main="O Tolstoy!",
xlab = "Narrative Time",
ylab= "Emotional Valence",
col="red"
)

That was a good day R in the Syuzhet library!

--

--

infoart.ca
infoart.ca

Written by infoart.ca

Center for Social Capital & Environmental Research | Posts by Bishwajit Ghose, BI consultant and lecturer at the University of Ottawa

No responses yet