library("tm") # for text mining
## Warning: package 'tm' was built under R version 4.2.3
## Loading required package: NLP
library("SnowballC")# for text manipulatio
## Warning: package 'SnowballC' was built under R version 4.2.3
library("wordcloud")# word-cloud generator
## Warning: package 'wordcloud' was built under R version 4.2.3
## Loading required package: RColorBrewer
library("RColorBrewer")
Sys.setenv(LANG = "en")
#Let's start by importing the text to analyze

tex <- readLines(("jane-austen-pride-prejudice.txt"), warn=FALSE, encoding = "UTF-8")
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 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
## elizabeth elizabeth  587
## will           will  421
## said           said  401
## darcy         darcy  373
## mrs             mrs  344
## much           much  329
## must           must  318
## bennet       bennet  295
## one             one  291
## every         every  283
## miss           miss  283
## jane           jane  265
## bingley     bingley  256
## know           know  237
## though       though  224
## never         never  221
## can             can  220
## well           well  219
## soon           soon  218
## think         think  211
## may             may  210
## now             now  210
## time           time  201
## might         might  200
## good           good  191
## little       little  189
## lady           lady  180
## sister       sister  178
## nothing     nothing  177
## without     without  177
## make           make  169
## shall         shall  164
## thing         thing  160
## wickham     wickham  160
## say             say  159
## dear           dear  157
## collins     collins  156
## see             see  152
## family       family  152
## first         first  146
## man             man  144
## great         great  142
## day             day  140
## however     however  135
## ever           ever  135
## two             two  135
## lydia         lydia  132
## made           made  129
## young         young  128
## give           give  127
## hope           hope  122
## away           away  120
## many           many  120
## last           last  119
## always       always  119
## long           long  117
## letter       letter  116
## father       father  115
## room           room  114
## sisters     sisters  113
## thought     thought  111
## catherine catherine  110
## way             way  109
## mother       mother  109
## enough       enough  106
## house         house  106
## friend       friend  103
## replied     replied  102
## sure           sure  102
## saw             saw  102
## felt           felt  100
## wish           wish   98
## manner       manner   96
## indeed       indeed   95
## till           till   95
## quite         quite   95
## lizzy         lizzy   94
## come           come   93
## pleasure   pleasure   93
## better       better   92
## love           love   91
## came           came   90
## often         often   90
## done           done   90
## cried         cried   89
## really       really   89
## project     project   88
## feelings   feelings   88
## believe     believe   88
## longbourn longbourn   87
## heard         heard   86
## subject     subject   86
## even           even   84
## gardiner   gardiner   84
## happy         happy   83
## take           take   80
## sir             sir   79
## whole         whole   79
## less           less   78
## aunt           aunt   78
set.seed(1234)
wordcloud(words = d$word, freq = d$freq, min.freq = 200,
          max.words=Inf, random.order=T, rot.per=0.5, 
          colors=brewer.pal(8, "Dark2"))

findFreqTerms(dtm, lowfreq = 20)
##   [1] "gutenberg"     "jane"          "pride"         "project"      
##   [5] "william"       "use"           "almost"        "away"         
##   [9] "give"          "may"           "terms"         "hurst"        
##  [13] "mary"          "made"          "also"          "see"          
##  [17] "character"     "used"          "mrs"           "three"        
##  [21] "sense"         "london"        "library"       "morning"      
##  [25] "charlotte"     "chapter"       "acknowledged"  "man"          
##  [29] "truth"         "fortune"       "good"          "must"         
##  [33] "want"          "wife"          "feelings"      "however"      
##  [37] "known"         "little"        "first"         "fixed"        
##  [41] "neighbourhood" "well"          "considered"    "daughters"    
##  [45] "one"           "bennet"        "day"           "dear"         
##  [49] "heard"         "lady"          "said"          "last"         
##  [53] "let"           "netherfield"   "park"          "replied"      
##  [57] "just"          "long"          "returned"      "told"         
##  [61] "answer"        "cried"         "know"          "taken"        
##  [65] "hearing"       "tell"          "you"           "enough"       
##  [69] "invitation"    "came"          "large"         "young"        
##  [73] "four"          "much"          "place"         "delighted"    
##  [77] "immediately"   "take"          "end"           "house"        
##  [81] "next"          "week"          "name"          "bingley"      
##  [85] "married"       "sure"          "fine"          "five"         
##  [89] "girls"         "thing"         "thousand"      "year"         
##  [93] "can"           "marrying"      "thinking"      "design"       
##  [97] "likely"        "talk"          "love"          "therefore"    
## [101] "visit"         "soon"          "occasion"      "send"         
## [105] "better"        "perhaps"       "still"         "will"         
## [109] "best"          "handsome"      "like"          "might"        
## [113] "party"         "beauty"        "certainly"     "now"          
## [117] "woman"         "often"         "think"         "indeed"       
## [121] "assure"        "consider"      "determined"    "lucas"        
## [125] "sir"           "account"       "general"       "merely"       
## [129] "new"           "impossible"    "dare"          "say"          
## [133] "glad"          "ever"          "though"        "lizzy"        
## [137] "word"          "desire"        "half"          "others"       
## [141] "always"        "giving"        "her"           "lydia"        
## [145] "something"     "sisters"       "children"      "way"          
## [149] "delight"       "poor"          "respect"       "friends"      
## [153] "mention"       "least"         "years"         "get"          
## [157] "hope"          "live"          "many"          "men"          
## [161] "come"          "since"         "depend"        "upon"         
## [165] "humour"        "make"          "mind"          "understand"   
## [169] "less"          "mean"          "understanding" "information"  
## [173] "temper"        "business"      "life"          "news"         
## [177] "among"         "intended"      "evening"       "till"         
## [181] "following"     "knowledge"     "paid"          "daughter"     
## [185] "manner"        "second"        "mother"        "elizabeth"    
## [189] "meet"          "shall"         "promised"      "believe"      
## [193] "two"           "opinion"       "find"          "reply"        
## [197] "began"         "keep"          "kitty"         "sake"         
## [201] "father"        "times"         "ill"           "ball"         
## [205] "fortnight"     "morrow"        "back"          "advantage"    
## [209] "friend"        "acquainted"    "acquaintance"  "honour"       
## [213] "really"        "else"          "kindness"      "quite"        
## [217] "great"         "read"          "knew"          "sensible"     
## [221] "wished"        "continued"     "return"        "hear"         
## [225] "sorry"         "that"          "called"        "astonishment" 
## [229] "ladies"        "rest"          "expected"      "pleased"      
## [233] "gone"          "never"         "left"          "room"         
## [237] "spoke"         "door"          "either"        "matter"       
## [241] "time"          "every"         "making"        "dance"        
## [245] "afraid"        "spent"         "ask"           "bennets"      
## [249] "dinner"        "subject"       "husband"       "obliged"      
## [253] "highly"        "agreeable"     "meant"         "whole"        
## [257] "nothing"       "certain"       "dancing"       "towards"      
## [261] "bingleys"      "heart"         "hopes"         "settled"      
## [265] "equally"       "wish"          "days"          "sat"          
## [269] "ten"           "minutes"       "whose"         "saw"          
## [273] "afterwards"    "already"       "credit"        "town"         
## [277] "imagine"       "arrival"       "fear"          "hertfordshire"
## [281] "another"       "idea"          "followed"      "gentlemen"    
## [285] "brought"       "instead"       "cousin"        "entered"      
## [289] "eldest"        "looking"       "countenance"   "easy"         
## [293] "manners"       "air"           "attention"     "darcy"        
## [297] "gentleman"     "looked"        "person"        "within"       
## [301] "admiration"    "gave"          "turned"        "company"      
## [305] "proud"         "derbyshire"    "estate"        "people"       
## [309] "early"         "talked"        "amiable"       "speak"        
## [313] "miss"          "speaking"      "walking"       "body"         
## [317] "hoped"         "world"         "dislike"       "behaviour"    
## [321] "particular"    "part"          "conversation"  "near"         
## [325] "unless"        "particularly"  "engaged"       "met"          
## [329] "several"       "pretty"        "girl"          "sitting"      
## [333] "moment"        "consequence"   "present"       "walked"       
## [337] "remained"      "disposition"   "family"        "passed"       
## [341] "seen"          "felt"          "janes"         "mentioned"    
## [345] "pleasure"      "catherine"     "without"       "care"         
## [349] "yet"           "longbourn"     "spirits"       "found"        
## [353] "curiosity"     "deal"          "event"         "rather"       
## [357] "different"     "thought"       "asked"         "going"        
## [361] "seemed"        "got"           "charming"      "added"        
## [365] "pleasing"      "given"         "set"           "alone"        
## [369] "former"        "expressed"     "praise"        "sister"       
## [373] "happy"         "compliment"    "expect"        "surprise"     
## [377] "help"          "natural"       "seeing"        "leave"        
## [381] "liked"         "eyes"          "wonder"        "equal"        
## [385] "brother"       "mistaken"      "convinced"     "listened"     
## [389] "silence"       "power"         "pounds"        "sometimes"    
## [393] "whether"       "anxious"       "means"         "table"        
## [397] "home"          "look"          "hour"          "satisfied"    
## [401] "situation"     "took"          "spite"         "greater"      
## [405] "offer"         "appeared"      "darcys"        "regard"       
## [409] "meryton"       "kind"          "interest"      "received"     
## [413] "allowed"       "object"        "short"         "walk"         
## [417] "address"       "small"         "importance"    "nature"       
## [421] "elizabeths"    "necessary"     "yes"           "suppose"      
## [425] "hardly"        "beyond"        "doubt"         "point"        
## [429] "eliza"         "purpose"       "head"          "put"          
## [433] "night"         "carriage"      "talking"       "favour"       
## [437] "express"       "right"         "easily"        "true"         
## [441] "observed"      "feeling"       "real"          "things"       
## [445] "words"         "vain"          "directly"      "younger"      
## [449] "greatest"      "even"          "whenever"      "able"         
## [453] "case"          "public"        "affection"     "opportunity"  
## [457] "gratitude"     "attachment"    "allow"         "remember"     
## [461] "endeavour"     "hours"         "together"      "question"     
## [465] "degree"        "happiness"     "entirely"      "marriage"     
## [469] "hand"          "possible"      "attentions"    "far"          
## [473] "scarcely"      "face"          "expression"    "forced"       
## [477] "perfectly"     "attended"      "notice"        "colonel"      
## [481] "forster"       "turn"          "saying"        "course"       
## [485] "neither"       "joined"        "officers"      "silent"       
## [489] "thoughts"      "thus"          "proper"        "feel"         
## [493] "society"       "instantly"     "receive"       "surprised"    
## [497] "attempt"       "repeated"      "favourite"     "pray"         
## [501] "exactly"       "serious"       "pemberley"     "mothers"      
## [505] "philips"       "aunt"          "country"       "length"       
## [509] "ready"         "prevented"     "seems"         "stay"         
## [513] "wanted"        "answered"      "breakfast"     "comfort"      
## [517] "reason"        "appearance"    "coming"        "latter"       
## [521] "supposed"      "concern"       "believed"      "she"          
## [525] "lost"          "whatever"      "sort"          "uncle"        
## [529] "relations"     "stairs"        "assured"       "work"         
## [533] "wholly"        "knowing"       "voice"         "longer"       
## [537] "forward"       "sent"          "probably"      "civility"     
## [541] "madam"         "done"          "except"        "meeting"      
## [545] "smile"         "wrote"         "inclination"   "engagement"   
## [549] "appear"        "letter"        "write"         "call"         
## [553] "resolved"      "conduct"       "exceedingly"   "change"       
## [557] "particulars"   "especially"    "side"          "frequently"   
## [561] "pain"          "full"          "farther"       "wishes"       
## [565] "usual"         "agreement"     "master"        "trouble"      
## [569] "absence"       "ago"           "collins"       "bear"         
## [573] "hunsford"      "bourgh"        "ladyship"      "satisfaction" 
## [577] "collinss"      "need"          "fair"          "rosings"      
## [581] "marry"         "state"         "written"       "lydias"       
## [585] "living"        "wickham"       "happened"      "receiving"    
## [589] "circumstances" "money"         "wickhams"      "nephew"       
## [593] "went"          "cause"         "match"         "months"       
## [597] "persuaded"     "foundation"    "journey"       "gardiner"     
## [601] "fitzwilliam"   "niece"         "brighton"      "works"        
## [605] "electronic"    "gutenbergtm"
barplot(d[1:10,]$freq, las = 2, names.arg = d[1:10,]$word,
        col ="lightblue", main ="Most frequent words",
        ylab = "Word frequencies")