Sys.setenv(LANG = "en")
Este analisis de texto sera hecho sobre el discurso de Martin Luther King Jr en la guerra civil de EUA.
# Load libraries
library("tm")
## Loading required package: NLP
library("SnowballC")
library("wordcloud")
## Loading required package: RColorBrewer
library("RColorBrewer")
tex <- readLines(("/Users/nathaliacarcoba/Downloads/MLKjr.txt"), warn=FALSE)
text <- iconv(tex,"WINDOWS-1252","UTF-8")
#We need to convert the text to a corpus
docs <- Corpus(VectorSource(text))
#inspect(docs)
#General text cleaning
# Convert the text to lower case
docs <- tm_map(docs, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(docs, content_transformer(tolower)):
## transformation drops documents
# Remove numbers
docs <- tm_map(docs, removeNumbers)
## Warning in tm_map.SimpleCorpus(docs, removeNumbers): transformation drops
## documents
# Remove english common stopwords
docs <- tm_map(docs, removeWords, stopwords("english"))
## Warning in tm_map.SimpleCorpus(docs, removeWords, stopwords("english")):
## transformation drops documents
# Remove your own stop word
# specify your stopwords as a character vector
docs <- tm_map(docs, removeWords, c("blabla1", "blabla2"))
## Warning in tm_map.SimpleCorpus(docs, removeWords, c("blabla1", "blabla2")):
## transformation drops documents
# Remove punctuations
docs <- tm_map(docs, removePunctuation)
## Warning in tm_map.SimpleCorpus(docs, removePunctuation): transformation drops
## documents
# Eliminate extra white spaces
docs <- tm_map(docs, stripWhitespace)
## Warning in tm_map.SimpleCorpus(docs, stripWhitespace): transformation drops
## documents
#Term-document matrix. Document matrix is a table containing the frequency of the words.
dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
head(d, 100)
## word freq
## will will 27
## freedom freedom 20
## negro negro 13
## one one 13
## let let 13
## day day 12
## ring ring 12
## dream dream 11
## nation nation 10
## come come 10
## every every 10
## today today 9
## back back 9
## justice justice 8
## must must 8
## satisfied satisfied 8
## able able 8
## together together 7
## long long 6
## men men 6
## white white 6
## now now 6
## great great 5
## years years 5
## free free 5
## america america 5
## check check 5
## children children 5
## time time 5
## new new 5
## shall shall 5
## faith faith 5
## american american 4
## hope hope 4
## hundred hundred 4
## land land 4
## later later 4
## still still 4
## black black 4
## can can 4
## mississippi mississippi 4
## mountain mountain 4
## join join 3
## injustice injustice 3
## stand stand 3
## note note 3
## people people 3
## rights rights 3
## words words 3
## brotherhood brotherhood 3
## gods gods 3
## make make 3
## rise rise 3
## valley valley 3
## sweltering sweltering 3
## alabama alabama 3
## georgia georgia 3
## never never 3
## state state 3
## little little 3
## made made 3
## sing sing 3
## last last 3
## history history 2
## came came 2
## end end 2
## slaves slaves 2
## life life 2
## segregation segregation 2
## cash cash 2
## color color 2
## funds funds 2
## insufficient insufficient 2
## liberty liberty 2
## promissory promissory 2
## believe believe 2
## refuse refuse 2
## racial racial 2
## urgency urgency 2
## content content 2
## continue continue 2
## negros negros 2
## allow allow 2
## creative creative 2
## dignity dignity 2
## force force 2
## physical physical 2
## say say 2
## struggle struggle 2
## brothers brothers 2
## destiny destiny 2
## realize realize 2
## walk walk 2
## brutality brutality 2
## cities cities 2
## jail jail 2
## knowing knowing 2
## like like 2
## mighty mighty 2
## police police 2
set.seed(1234)
wordcloud(words = d$word, freq = d$freq, min.freq = 5,
max.words=Inf, random.order=T, rot.per=0.5,
colors=brewer.pal(8, "Dark2"))
findFreqTerms(dtm, lowfreq = 20)
## [1] "freedom" "will"
findAssocs(dtm, terms = "negro", corlimit = 0.2)
## $negro
## years chains condition corners crippled
## 0.71 0.69 0.69 0.69 0.69
## discrimination dramatize exile finds hundred
## 0.69 0.69 0.69 0.69 0.69
## island languished later lives lonely
## 0.69 0.69 0.69 0.69 0.69
## manacles material midst ocean poverty
## 0.69 0.69 0.69 0.69 0.69
## prosperity sadly shameful society vast
## 0.69 0.69 0.69 0.69 0.69
## still come life continue negros
## 0.65 0.64 0.60 0.60 0.60
## one rights long back areas
## 0.59 0.58 0.53 0.51 0.51
## asking basic battered believes bodies
## 0.51 0.51 0.51 0.51 0.51
## brutality can carolina cells changed
## 0.51 0.51 0.51 0.51 0.51
## cities civil devotees fatigue fresh
## 0.51 0.51 0.51 0.51 0.51
## gain ghetto ghettos heavy highways
## 0.51 0.51 0.51 0.51 0.51
## horrors hotels larger left like
## 0.51 0.51 0.51 0.51 0.51
## lodging louisiana mobility motels narrow
## 0.51 0.51 0.51 0.51 0.51
## never northern nothing persecution police
## 0.51 0.51 0.51 0.51 0.51
## quest redemptive righteousness robbed rolls
## 0.51 0.51 0.51 0.51 0.51
## satisfied selfhood signs situation slums
## 0.51 0.51 0.51 0.51 0.51
## smaller somehow staggered stating storms
## 0.51 0.51 0.51 0.51 0.51
## stream stripped suffering travel trials
## 0.51 0.51 0.51 0.51 0.51
## tribulations unearned unmindful unspeakable veterans
## 0.51 0.51 0.51 0.51 0.51
## victim vote waters whites winds
## 0.51 0.51 0.51 0.51 0.51
## american segregation mississippi will today
## 0.49 0.47 0.40 0.36 0.35
## end creative dignity jail knowing
## 0.34 0.34 0.34 0.34 0.34
## mighty south work york autumn
## 0.34 0.34 0.34 0.34 0.33
## awakening beginning blow bright business
## 0.33 0.33 0.33 0.33 0.33
## citizenship discontent emerges equality fatal
## 0.33 0.33 0.33 0.33 0.33
## foundations granted invigorating legitimate moment
## 0.33 0.33 0.33 0.33 0.33
## needed neither nineteen overlook pass
## 0.33 0.33 0.33 0.33 0.33
## rest returns revolt rude shake
## 0.33 0.33 0.33 0.33 0.33
## sixtythree steam summer tranquility usual
## 0.33 0.33 0.33 0.33 0.33
## whirlwinds great georgia children new
## 0.33 0.29 0.27 0.26 0.26
## hope free america urgency content
## 0.22 0.22 0.22 0.22 0.22
## freedom nation alabama
## 0.21 0.20 0.20
barplot(d[1:10,]$freq, las = 2, names.arg = d[1:10,]$word,
col ="purple", main ="Most frequent words",
ylab = "Word frequencies")