Skip to content

Instantly share code, notes, and snippets.

@menzenski
Created April 8, 2015 22:22
Show Gist options
  • Save menzenski/54beeb219b92d75923e6 to your computer and use it in GitHub Desktop.
Save menzenski/54beeb219b92d75923e6 to your computer and use it in GitHub Desktop.
### set the working directory---you'll need to redefine this for your local system
setwd("~/Documents/School/Kansas/Coding/2015_02_12_RCorpusLingTools/JockersDataFiles/code")
#library("syuzhet")
library("syuzhet", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")
### define our text file (here, it's in the current working directory)
### downloaded from http://www.gutenberg.org/cache/epub/2600/pg2600.txt
text.v <- scan("WarAndPeaceEnglish.txt", what="character",sep="\n")
### this takes a while :(
s_v <- get_sentences(text.v)
### create all three vectors, one for each method
bing_vector <- get_sentiment(s_v, method="bing")
afinn_vector <- get_sentiment(s_v, method="afinn")
nrc_vector <- get_sentiment(s_v, method="nrc")
### define the method that we'll use to graph
sentiment_vector <- bing_vector
# sentiment_vector <- afinn_vector
# sentiment_vector <- nrc_vector
### display basic summary stats if you want them
#sum(sentiment_vector); mean(sentiment_vector)
#sum(afinn_vector); mean(afinn_vector)
#sum(nrc_vector); mean(nrc_vector)
####### PLOTTING
### basic plot of sentiment totals for all sentences individually
plot(
sentiment_vector,
type="h",
#main="Plot Trajectory of 'War and Peace'",
xlab="Narrative Time",
ylab="Emotional Valence"
)
### plot summary using percentage-based means (100 equal chunks of text)
percent_vals <- get_percentage_values(sentiment_vector)
plot(
percent_vals,
type="l",
#main="Plot Trajectory of 'War and Peace'",
xlab="Narrative Time",
ylab="Emotional Valence",
col="red"
)
# plot summary using Fourier Transformation and low-pass filter
ft_values <- get_transformed_values(
sentiment_vector,
low_pass_size=3,
x_reverse_len=100,
scale_vals=TRUE,
scale_range=FALSE
)
plot(
ft_values,
type="h",
#main="'War and Peace' using Transformed Values",
xlab="Narrative Time",
ylab="Emotional Valence",
col="red"
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment