-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathI have a dream.R
More file actions
87 lines (44 loc) · 1.7 KB
/
Copy pathI have a dream.R
File metadata and controls
87 lines (44 loc) · 1.7 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
install.packages("sentimentr")
library(sentimentr)
Speech_file<-readLines('C:\\Users\\Mina Doss\\OneDrive\\Desktop\\i_have_a_dream.txt')
Speech_file
Speech_sentences <- get_sentences(Speech_file)
Sentiment_Score<-sentiment_by(Speech_sentences )
Sentiment_Score
plot(Sentiment_Score $element_id, Sentiment_Score $ave_sentiment,xlab="Sentence Number" ,ylab="Polarity", main = "Sentiment of I Have a Dream Speech")
install.packages("tm") # for text mining
install.packages("SnowballC") # for text stemming
install.packages("wordcloud") # word-cloud generator
install.packages("RColorBrewer") # color palettes
# Load
library("tm")
library("SnowballC")
library("wordcloud")
library("RColorBrewer")
docs <- Corpus(VectorSource(Speech_file))
inspect(docs)
# Preprocessing Steps :
docs <- tm_map(docs, content_transformer(tolower))
# Remove numbers
docs <- tm_map(docs, removeNumbers)
# Remove english common stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))
# Remove your own stop word
# specify your stopwords as a character vector
docs <- tm_map(docs, removeWords, c("the", "instead"))
# Remove punctuations
docs <- tm_map(docs, removePunctuation)
# Eliminate extra white spaces
docs <- tm_map(docs, stripWhitespace)
# Text stemming
#docs <- tm_map(docs, stemDocument)
dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
m
v <- sort(rowSums(m),decreasing=TRUE)
v
d <- data.frame(word = names(v),freq=v)
head(d, 10)
wordcloud(words = d$word, freq = d$freq, min.freq = 1,
max.words=200, random.order=FALSE, rot.per=0.35,
colors=brewer.pal(8, "Dark2"))