forked from mnr/R-for-Data-Science-Lunchbreak-Lessons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0x_279 sentiment analysis.R
78 lines (68 loc) · 2.85 KB
/
0x_279 sentiment analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# sample corpus
library(tm)
emotions_df <- data.frame(
doc_id = c("THE COMING NIGHT",
"Love Letters of Nathaniel Hawthorne",
"Big Dummy's Guide to the Internet"),
text = c("THIS week has been one of heavy sorrow to very many. The neighbourhood
has lost one who for many years has stood foremost in large-hearted
Christian benevolence. The poor have been deprived of a kind friend, to
whose liberality they might ever resort. The children have been bereaved
of one who has for years been anxious to devote her attentive care to
their early training; and all who have ever needed a sympathizing friend
have followed one this day to the grave as warm-hearted, energetic, and
intelligent as is often to be met with in society. Her character is well
described in some lines written by herself on the death of one she dearly
loved—",
"Sweetest, I know not when I shall see thee; but I trust it will not be
longer than the end of next week. I love thee! I love thee! I wouldst
thou wert with me; for then would my labor be joyful--and even now it
is not sorrowful. Dearest, I shall make an excellent husbandman. I
feel the original Adam reviving within me.",
"Increasingly, computers come with modems already installed. If
yours didn't, you'll have to decide what speed modem to get. Modem
speeds are judged in bps rate or bits per second. One bps means
the modem can transfer roughly one bit per second; the greater the
bps rate, the more quickly a modem can send and receive information.
A letter or character is made up of eight bits."),
author = c("THE REV. EDWARD HOARE, A.M.",
"Nathaniel Hawthorne",
"Electronic Frontier Foundation")
)
emotions_corpus <- Corpus(DataframeSource(emotions_df))
summary(emotions_corpus)
# meanr ----------
# focused on HuLiu dictionary
# package contains one function: "score"
# "score" column is "positive" - "negative"
# install.packages("meanr")
library(meanr)
score(emotions_df[,"text"]) # wc = word count
# syuzhet ------------
# quick and easy sentiment analysis
# based on fiction novels. only latin characters
# install.packages("syuzhet")
library(syuzhet)
get_sentiment(emotions_corpus)
# methods: "syuzhet", "bing", "afinn", "nrc" and "stanford"
get_sentiment(emotions_corpus, method = "bing")
# tardis -----------
# Text Analysis with Rules and Dictionaries
# for Inferring Sentiment
# simple to use. Calculation on sentences
# multiple sentences return mean, stdDev, & range
# install.packages("tardis")
library(tardis)
tardis(emotions_df,
text_column = "text")
tardis(emotions_df,
text_column = "text",
simple_count = TRUE)
# vader ------------
# tuned to social media
# Valence Aware Dictionary and sEntiment Reasoner
# install.packages("vader")
library(vader)
vader_df <- vader_df(emotions_df[ , "text"])
get_vader(emotions_df[3,"text"])
vader_df