-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentiment Analysis.R
More file actions
271 lines (257 loc) · 9.45 KB
/
Sentiment Analysis.R
File metadata and controls
271 lines (257 loc) · 9.45 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
packages <- c('tm', 'stringr', 'syuzhet', 'xlsx')
if (length(setdiff(packages, rownames(installed.packages()))) > 0) {
install.packages(setdiff(packages, rownames(installed.packages())))
}
library(tm)
library(stringr)
library(syuzhet)
library(xlsx)
rm(list=ls(all=TRUE))
# Transcripts of interviews are stored as separate text files (e.g. '1.txt, 2.txt...')
# in the below folder:
DataPath <- '//VARDA/vol1/files/research/DEB/Data Section/Active Projects/Faculty_Resident Projects/Kothari_Natural Language/DV Transcripts/dv raw text'
# We'll use the [tm] library to collect the texts and strip them down to lists of words.
# We need to make sure that contractions do not appear as NRC items once their
# punctuation has been stripped. (e.g. "I'll" oughn't be misinterpreted as "ill")
expandContractions <- function(foo){
contractions <- c('aren\'t',
'can\'t',
'could\'ve',
'couldn\'t',
'didn\'t',
'doesn\'t',
'don\'t',
'gonna',
'gotta',
'hadn\'t',
'hasn\'t',
'haven\'t',
'he\'d',
'he\'ll',
'he\'s',
'how\'d',
'how\'ll',
'how\'s',
'I\'d',
'I\'ll',
'I\'m',
'I\'ve',
'isn\'t',
'it\'d',
'it\'ll',
'it\'s',
'mayn\'t',
'may\'ve',
'mightn\'t',
'might\'ve',
'mustn\'t',
'must\'ve',
'needn\'t',
'oughtn\'t',
'shan\'t',
'she\'d',
'she\'ll',
'she\'s',
'should\'ve',
'shouldn\'t',
'something\'s',
'that\'ll',
'that\'re',
'that\'s',
'that\'d',
'there\'d',
'there\'re',
'there\'s',
'these\'re',
'they\'d',
'they\'ll',
'they\'re',
'they\'ve',
'this\'s',
'those\'re',
'wasn\'t',
'we\'d',
'we\'d\'ve',
'we\'ll',
'we\'re',
'we\'ve',
'weren\'t',
'what\'d',
'what\'ll',
'what\'re',
'what\'s',
'what\'ve',
'when\'s',
'where\'d',
'where\'re',
'where\'s',
'where\'ve',
'which\'s',
'who\'d',
'who\'d\'ve',
'who\'ll',
'who\'re',
'who\'s',
'who\'ve',
'why\'d',
'why\'re',
'why\'s',
'won\'t',
'would\'ve',
'wouldn\'t',
'y\'all',
'you\'d',
'you\'ll',
'you\'re',
'you\'ve')
expansions <-c ('are not',
'can not',
'could have',
'could not',
'did not',
'does not',
'do not',
'going to',
'got to',
'had not',
'has not',
'have not',
'he would',
'he will',
'he is',
'how did',
'how will',
'how is',
'I would',
'I will',
'I am',
'I have',
'is not',
'it would',
'it will',
'it is',
'may not',
'may have',
'might not',
'might have',
'must not',
'must have',
'need not',
'ought not',
'shall not',
'she would',
'she will',
'she is',
'should have',
'should not',
'something is',
'that will',
'that are',
'that is',
'that would',
'there would',
'there are',
'there has',
'these are',
'they would',
'they will',
'they are',
'they have',
'this has',
'those are',
'was not',
'we would',
'we would have',
'we will',
'we are',
'we have',
'were not',
'what did',
'what will',
'what are',
'what has',
'what have',
'when has',
'where did',
'where are',
'where has',
'where have',
'which has',
'who would',
'who would have',
'who will',
'who are',
'who is',
'who have',
'why did',
'why are',
'why is',
'will not',
'would have',
'would not',
'you all',
'you would',
'you will',
'you are',
'you have')
names(expansions) <- contractions
str_replace_all(foo, expansions)
}
# The [tm] library's 'removePunctuation' function fails to remove typographic
# quotes - we'll add the function 'leaveOnlyLetters' and use regex to solve that.
leaveOnlyLetters <- function(foo) gsub("[^a-zA-Z ]","",foo)
# Using 'tm' to create corpora:
RawCorpus <- Corpus(DirSource(DataPath))
# ...and clean:
CleanCorpus <- RawCorpus
CleanCorpus <- tm_map(CleanCorpus, content_transformer(expandContractions))
CleanCorpus <- tm_map(CleanCorpus, content_transformer(leaveOnlyLetters))
CleanCorpus <- tm_map(CleanCorpus, stripWhitespace)
CleanCorpus <- tm_map(CleanCorpus, content_transformer(tolower))
# Establish lists of words:
first_person_singular_pronouns <- c('i', 'im', 'ive', 'me', 'my', 'myself', 'mine')
first_person_plural_pronouns <- c('we', 'weve', 'us', 'our', 'ours', 'ourselves')
third_person_singular_pronouns <- c('he', 'him', 'himself', 'his', 'she', 'her', 'herself', 'hers', 'it', 'its', 'itself')
third_person_plural_pronouns <- c('they', 'theyd', 'them', 'themselves', 'their', 'theirs')
# Wrap word lists with regex characters to define beginning and end of words, rather than
# parts of other words (e.g. 'wedding' should not be counted as an instance of 'we')
first_person_singular_pronouns <- paste0('\\b', first_person_singular_pronouns, '\\b')
first_person_plural_pronouns <- paste0('\\b', first_person_plural_pronouns, '\\b')
third_person_singular_pronouns <- paste0('\\b', third_person_singular_pronouns, '\\b')
third_person_plural_pronouns <- paste0('\\b', third_person_plural_pronouns, '\\b')
# Create a document term matrix and data frame for collecting data:
DTM <- DocumentTermMatrix(CleanCorpus)
DTMasDF <- as.data.frame(as.matrix(DTM))
CollectingDataframe <- data.frame(row.names=rownames(DTMasDF))
# Calculate and store the counts of first- and third-person pronouns in each text corpus,
# as well as a word count:
counter <- 1
for (i in 1:length(CleanCorpus)){
CollectingDataframe[counter,1]<-length(unlist(strsplit(as.String(CleanCorpus[[counter]]$content), " ")))
counter <- counter+1
}
# Determine the sentiment and valence values for each word in the combined text corpora,
# which words are the column names in DTMasDF
NRCMatrix <- as.data.frame(get_nrc_sentiment(colnames(DTMasDF)))
rownames(NRCMatrix) <- colnames(DTMasDF)
# Now let's add the additional columns in CollectingDataframe we'll use to log valence and sentiment,
# and add column names (the latter 10 corresponding to the NRC categories):
CollectingDataframe[, 2:11] <- 0
colnames(CollectingDataframe) <- c('word_count', colnames(NRCMatrix))
# And here, the robot runs through each row of DTMasDF, which represents the frequency of words found
# across the entire corpora, and totals the number of times each word appears in each text, per sentiment
# or valence.
counter_1 <- 1
for (i in 1:length(DTMasDF[,1])){
counter_2 <- 1
for (i in 1:length(DTMasDF[1,])){
for (i in names(CollectingDataframe[,2:11])){
CollectingDataframe[[paste(i)]][counter_1] <-
CollectingDataframe[[paste(i)]][counter_1] +
DTMasDF[counter_1,counter_2] * NRCMatrix[[paste(i)]][counter_2]
}
counter_2 <- counter_2+1
}
counter_1 <- counter_1+1
}
write.xlsx(CollectingDataframe, 'export.xlsx', row.names = TRUE, showNA = FALSE)