forked from Edilmo/DataScienceCapstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotebook.Rmd
1790 lines (1691 loc) · 81.3 KB
/
Notebook.Rmd
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "NoteBook"
author: "Edilmo Palencia"
output: html_document
---
# Task 0 - Obtaining the data & Familiarizing with NLP
The corpus:
```{r corpusDescription, echo=FALSE, cache=TRUE, message=FALSE}
currentTime <- Sys.time()
library(knitr)
library(R.utils)
# Check if there is a previous run already saved
if(file.exists("Corpus.RData")){
load("Corpus.RData")
}else{
# Create a empty dataFrame where the corpus description is going to be stored
# One row per document
corpusDescription <- data.frame(
row.names = c("src", "lan", "src-type", "f-name", "f-path"),
stringsAsFactors = FALSE)
# Check if the corpus has been downloaded
if(!file.exists("corpus")){
download.file(
"https://d396qusza40orc.cloudfront.net/dsscapstone/dataset/Coursera-SwiftKey.zip",
"corpus.zip", "curl")
unzip("corpus.zip")
dir.create("corpus")
file.rename("final","corpus/HC Corpora")
file.remove("corpus.zip")
}
# There is a directory per Source, so build a list of sources.
corpusSources <- list.dirs(path = "corpus", full.names = FALSE,
recursive = FALSE)
# Loop over the sources
for(s in corpusSources){
# There is a directory per Language, so build a list of
# languages for this source.
corpusLanguages <- list.dirs(path = filePath("corpus",s),
full.names = FALSE, recursive = FALSE)
for(l in corpusLanguages){
# There is a corpus document per source type, so build a list
# of documents with its source type
filelist <- list.files(filePath("corpus",s,l),
full.names = FALSE, recursive = FALSE)
for(f in filelist){
# Let's extract the source type from the file name
st <- strsplit(f,".",fixed = TRUE)
# Let's build the full path of the file
fname <- filePath("corpus",s,l,f)
# Create the row to add in the data frame
r <- c(s,l,st[[1]][2],f,fname)
# Add the new row as column
corpusDescription <- cbind(corpusDescription,r)
}
}
}
# Let's transpose the data frame because all the rows were added as columns
corpusDescription <- t(corpusDescription)
# Print time elapsed
Sys.time() - currentTime
}
# Print the data frame
kable(corpusDescription)
```
## Questions about the HC Corpora
```{r corpusLoading, echo=FALSE, cache=TRUE, message=FALSE}
library(tm)
if(!exists("readCorpus")){
currentTime <- Sys.time()
# Let's load the corpus using the tm package
readCorpus <- function(src,lan){
Corpus(DirSource(directory = filePath("corpus",src,lan),
encoding = "",
pattern = NULL,
recursive = FALSE,
ignore.case = FALSE,
mode = "text"),
readerControl = list(reader = readPlain,
language = lan,
load = FALSE))
}
corpusHCcorporaEnUS <- readCorpus("HC Corpora","en_US")
#corpusHCcorporaDeDE <- readCorpus("HC Corpora","de_DE")
#corpusHCcorporaFiFI <- readCorpus("HC Corpora","fi_FI")
#corpusHCcorporaRuRU <- readCorpus("HC Corpora","ru_RU")
# Print time elapsed
Sys.time() - currentTime
}
```
```{r corpusQuestions, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("charCountPerLineEnUSblogs")){
currentTime <- Sys.time()
# Let's count the amount of characters per line in each document of the english
# corpus
charCountPerLineEnUSblogs <- sapply(
corpusHCcorporaEnUS[["en_US.blogs.txt"]]$content,nchar)
charCountPerLineEnUSnews <- sapply(
corpusHCcorporaEnUS[["en_US.news.txt"]]$content,nchar)
charCountPerLineEnUStwitter <- sapply(
corpusHCcorporaEnUS[["en_US.twitter.txt"]]$content,nchar)
# Let's compute the lenght of the longest line in each document
longestLineLenghtEnUSblogs <- max(charCountPerLineEnUSblogs)
longestLineLenghtEnUSnews <- max(charCountPerLineEnUSnews)
longestLineLenghtEnUStwitter <- max(charCountPerLineEnUStwitter)
# Let's count the amount of times that "love" and "hate" appears in the
# twitter document of the english corpus
loveTimesEnUStwitter <- length(which(grepl("love",
corpusHCcorporaEnUS[["en_US.twitter.txt"]]$content)))
hateTimesEnUStwitter <- length(which(grepl("hate",
corpusHCcorporaEnUS[["en_US.twitter.txt"]]$content)))
# Let's look for the twitts that contains the word "biostats"
twittsWithBiostats <- grep("biostats",
corpusHCcorporaEnUS[["en_US.twitter.txt"]]$content, value = TRUE)
# Let's look for the exact twitt "A computer once beat me at chess, but it was
# no match for me at kickboxing"
twittsWithSpecificText <- grep(
"^A computer once beat me at chess, but it was no match for me at kickboxing$",
corpusHCcorporaEnUS[["en_US.twitter.txt"]]$content, value = FALSE)
# Print time elapsed
Sys.time() - currentTime
}
# Let's compute the amount of characters in the blogs document
charCountTemp <- sum(charCountPerLineEnUSblogs)
# Let's compute the amount of lines in the twitter document
lineCountTemp <- length(corpusHCcorporaEnUS[["en_US.twitter.txt"]]$content)
```
1. The en_US.blogs.txt file is how many megabytes?
* Araoud `r charCountTemp/1024/1024` Mega bytes.
2. The en_US.twitter.txt has how many lines of text?
* Araoud `r lineCountTemp/1000/1000` million lines.
3. What is the length of the longest line seen in any of the three en_US data
sets?
* EnUSblogs `r longestLineLenghtEnUSblogs`
* EnUSnews `r longestLineLenghtEnUSnews`
* EnUStwitter `r longestLineLenghtEnUStwitter`
4. In the en_US twitter data set, if you divide the number of lines where the
word "love" (all lowercase) occurs by the number of lines the word "hate"
(all lowercase) occurs, about what do you get?
* `r loveTimesEnUStwitter/hateTimesEnUStwitter`
5. The one tweet in the en_US twitter data set that matches the word "biostats"
says what?
* `r twittsWithBiostats[[1]]`
6. How many tweets have the exact characters "A computer once beat me at chess,
but it was no match for me at kickboxing". (I.e. the line matches those
characters exactly.)
* `r length(twittsWithSpecificText)`
7. What does the data look like?
* The data present in the corpus is completed unstructured documents of
natural language texts in 4 languages. There are no pre-proccessing
actions taken.
8. Where does the data come from?
* The data comes from 3 kind of sources: news articles, blogs and tweets.
9. Can you think of any other data sources that might help you in this project?
* Dictionaries, theasaurus and ontologies.
10. What are the common steps in natural language processing?
* Tokenize: corpus segmentation and transformation.
+ Word separation
+ Multi-word recognition
+ Character n-gram
+ Lexicon matching
+ Punctuation handling
+ Stop-words ignoring
+ Hyphenation handling
* Normalize: many different strings convey identical meanings.
+ Case folding: problematic because case and accent change
meaning sometimes.
+ Stemming: problematic because agglutinative languages has many
concepts combined in a single word.
+ Lemmatize.
+ Profanity Handling.
* Annotation: identical strings may have different meaning.
+ Part of the speech tagging.
+ Word sense tagging.
+ Parsing: making words according to their grammatical role.
11. What are some common issues in the analysis of text data?
* Polysemy: it is the capacity of a sign to have multiple meanings.
* Paralanguage recognition: component of meta-communication that may
modify or nuance meaning, or convey emotion.
* Curse of dimensionality - Data Sparsity: curse of dimensionality are
problems that arise when the data is high dimensional. Data sparsity is
specific problem that happens when exist a lot of dimension combinations
that are not populated at all (maybe because it doesn’t make any sense
or just because the data set is incomplete).
* Noisy Morphological Segmentation: morphological analysis is the task
of segmenting a word into morphemes, the smallest meaning- bearing
elements of natural languages. Normally this can be spoiled for
orthographic errors and so.
* Order-Invariance of Factor Composition: happens when a model is not
able to differentiate things like "hangover" de "overhang", which are
two words compose of two morphemes "over" and "hang".
* Inherent Ambiguity of Factor Composition: happens when a model doesn’t
know how to compose in cases like "un[[lock]able]" vs "[un[lock]]able]".
12. What is the relationship between NLP and the concepts you have learned in
the Specialization?
* Now days, the different problems attacked by NLP techniques are
considered machine learning problems, and the best techniques in this
field are the same or some extension of the tools tought in the
specialization.
# Task 1 - Tokenization & Profanity filtering
```{r matrixTermDoc, echo=FALSE, cache=TRUE, message=FALSE}
#currentTime <- Sys.time()
# Let's create a term-document matrix with the frequency of each word
#termDocMatrixEnUS <- TermDocumentMatrix(corpusHCcorporaEnUS)
# This method is very slow.
# Print time elapsed
#Sys.time() - currentTime
```
```{r simpleTokenization, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("corpusHCcorporaEnUS.OnlyLetters")){
currentTime <- Sys.time()
# Removing the numbers from the corpus
corpusHCcorporaEnUS.OnlyLetters <- tm_map(corpusHCcorporaEnUS, removeNumbers,
lazy = FALSE)
# Removing the puntuations from the corpus
corpusHCcorporaEnUS.OnlyLetters <- tm_map(corpusHCcorporaEnUS.OnlyLetters,
removePunctuation, lazy = FALSE)
# Removing the extra white spaces from the corpus
corpusHCcorporaEnUS.OnlyLetters <- tm_map(corpusHCcorporaEnUS.OnlyLetters,
stripWhitespace, lazy = FALSE)
# Removing the white spaces at the begging and the end
corpusHCcorporaEnUS.OnlyLetters <- tm_map(corpusHCcorporaEnUS.OnlyLetters,
content_transformer(trim))
# Dictionary with the vocabulary and the index of each word
# Use a hashed environment that contains one object per word
# The object name is the word itself and the value is the index
# of the word. The index is used for the tokenization
vocabularyHCcorporaEnUSbyWord <- new.env(hash = TRUE)
# Dictionary vector where the index points to the corresponding word
vocabularyHCcorporaEnUSbyIndex <- NULL
# Dictionary vector where the index points to the frequency of the
# corresponding word in the blogs document of the english corpus
wordFreqHCcorporaEnUSblogs <- NULL
# Dictionary vector where the index points to the tokenized version of the
# corresponding line in the blogs document of the english corpus. The tokenized
# version of a line is a vector of integers with the index of each word.
tokenizedHCcorporaEnUSblogs <- NULL
# Dictionary vector where the index points to the frequency of the
# corresponding word in the news document of the english corpus
wordFreqHCcorporaEnUSnews <- NULL
# Dictionary vector where the index points to the tokenized version of the
# corresponding line in the news document of the english corpus. The tokenized
# version of a line is a vector of integers with the index of each word.
tokenizedHCcorporaEnUSnews <- NULL
# Dictionary vector where the index points to the frequency of the
# corresponding word in the twitter document of the english corpus
wordFreqHCcorporaEnUStwitter <- NULL
# Dictionary vector where the index points to the tokenized version of the
# corresponding line in the twitter document of the english corpus. The tokenized
# version of a line is a vector of integers with the index of each word.
tokenizedHCcorporaEnUStwitter <- NULL
# temporal variable used to store word-frequency dictionaries
temp2 <- NULL
# Function used to tokenize a line o a document
procDocLine <- function(docLine){
# result variable to store the tokenize version of the line
tokenizeLine <- numeric(length = length(docLine))
# index variable of the word to tokenize
i <- 1
for(w in docLine){
# get the index of the word in our vocabulary dictionary
wIndex <- vocabularyHCcorporaEnUSbyWord[[w]]
# check if the word exist in our vocabulary
if(is.null(wIndex)){
# the index for a new word it's just the lenght of the
# vocabulary
wIndex <- length(vocabularyHCcorporaEnUSbyIndex) + 1
# add the word to the vocabulary dictionary by word
vocabularyHCcorporaEnUSbyWord[[w]] <<- wIndex
# add the word to the vocabulary dictionary by index
vocabularyHCcorporaEnUSbyIndex[wIndex] <<- w
# initialize the frequency for the word to 0
temp2[wIndex] <<- 0
}
# increase the frequency of the word
temp2[wIndex] <<- temp2[wIndex] + 1
# append the index of the word the tokenized line
tokenizeLine[i] <- wIndex
# increase the index of the word to process
i <- i + 1
}
# return the tokenized line
tokenizeLine
}
# Print time elapsed
Sys.time() - currentTime
currentTime <- Sys.time()
# split each line by white spaces in the blogs document
temp <- strsplit(
corpusHCcorporaEnUS.OnlyLetters[["en_US.blogs.txt"]]$content," ",
fixed = TRUE)
# clear the temp2 variable
temp2 <- NULL
# process each splitted line
tokenizedHCcorporaEnUSblogs <- lapply(temp, procDocLine)
# save the temporal frequency dictionary
wordFreqHCcorporaEnUSblogs <- temp2
# save a image of the environment
save.image("Corpus.RData")
# Print time elapsed
Sys.time() - currentTime
currentTime <- Sys.time()
# split each line by white spaces in the blogs document
temp <- strsplit(
corpusHCcorporaEnUS.OnlyLetters[["en_US.news.txt"]]$content," ",
fixed = TRUE)
# clear the temp2 variable
temp2 <- NULL
# process each splitted line
tokenizedHCcorporaEnUSnews <- lapply(temp, procDocLine)
# save the temporal frequency dictionary
wordFreqHCcorporaEnUSnews <- temp2
# save a image of the environment
save.image("Corpus.RData")
# Print time elapsed
Sys.time() - currentTime
currentTime <- Sys.time()
# split each line by white spaces in the blogs document
temp <- strsplit(
corpusHCcorporaEnUS.OnlyLetters[["en_US.twitter.txt"]]$content," ",
fixed = TRUE)
# clear the temp2 variable
temp2 <- NULL
# process each splitted line
tokenizedHCcorporaEnUStwitter <- lapply(temp, procDocLine)
# save the temporal frequency dictionary
wordFreqHCcorporaEnUStwitter <- temp2
# save a image of the environment
save.image("Corpus.RData")
# Print time elapsed
Sys.time() - currentTime
}
```
```{r profanitySourcing, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("badWords")){
currentTime <- Sys.time()
# Load a bad word list
temp <- url("http://www.cs.cmu.edu/~biglou/resources/bad-words.txt")
badWords <- readLines(temp)
# Convert the frame to vector
badWords <- tolower(badWords)
badWords <- trim(badWords)
badWords <- badWords[which(badWords!="")]
close(temp)
# Load a swear word list
temp <- url("http://www.bannedwordlist.com/lists/swearWords.txt")
swearWords <- readLines(temp)
# Convert the frame to vector
swearWords <- tolower(swearWords)
swearWords <- trim(swearWords)
swearWords <- swearWords[which(swearWords!="")]
close(temp)
# Merge bad and swear words
temp <- swearWords %in% badWords
bad_swearWords <- c(badWords, swearWords[which(!temp)])
# Print time elapsed
Sys.time() - currentTime
}
```
## Vocabulary & profanities
Two list of bad words from internet, one of bad words and other of swear words.
```{r profanityTally, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("profanyDFEnUStwitter")){
currentTime <- Sys.time()
# Logical vector with the index of the vocabulary words present in the
# swear and bad word list
bad_swearWordsInVocabulary <- vocabularyHCcorporaEnUSbyIndex %in% bad_swearWords
# Frequency of the bad-swear words
profanyFreqEnUSblogs <-
wordFreqHCcorporaEnUSblogs[which(bad_swearWordsInVocabulary)]
profanyFreqEnUSnews <-
wordFreqHCcorporaEnUSnews[which(bad_swearWordsInVocabulary)]
profanyFreqEnUStwitter <-
wordFreqHCcorporaEnUStwitter[which(bad_swearWordsInVocabulary)]
# bad-swear words
profanyEnUS <-
vocabularyHCcorporaEnUSbyIndex[which(bad_swearWordsInVocabulary)]
# Data frame of Frequency of the bad-swear words
profanyDFEnUSblogs <- data.frame(profanyEnUS,profanyFreqEnUSblogs)
profanyDFEnUSblogs <- profanyDFEnUSblogs[order(profanyFreqEnUSblogs,profanyEnUS,
decreasing = TRUE),]
profanyDFEnUSnews <- data.frame(profanyEnUS,profanyFreqEnUSnews)
profanyDFEnUSnews <- profanyDFEnUSnews[order(profanyFreqEnUSnews,profanyEnUS,
decreasing = TRUE),]
profanyDFEnUStwitter <- data.frame(profanyEnUS,profanyFreqEnUStwitter)
profanyDFEnUStwitter <- profanyDFEnUStwitter[order(profanyFreqEnUStwitter,profanyEnUS,
decreasing = TRUE),]
ammountOfBad_SwearWordsInVocabularyEnUS <- length(profanyEnUS)
# Print time elapsed
Sys.time() - currentTime
kable(profanyDFEnUSblogs[1:30,],
caption = "First 30 more frequent bad-swear words in EN-US blogs")
kable(profanyDFEnUSnews[1:30,],
caption = "First 30 more frequent bad-swear words in EN-US news")
kable(profanyDFEnUStwitter[1:30,],
caption = "First 30 more frequent bad-swear words in EN-US twitter")
}
```
The above table shows the first 30 swear-bad words of the vocabularry and their
frequency. The total of swear-bad words is
`r ammountOfBad_SwearWordsInVocabularyEnUS`.
As can be see it, the majority of the words are not bad words by definition. It's
depends on the context. So we prefer do not remove the bad and swear words and
look for approaches that consider just the usage and not the context.
# Task 2 - Exploratory analysis & Understand frequencies of words and word pairs
## Distribution of the frequencies of the vocabullary in En US blogs
```{r vocabullaryStatsEnUSblogs, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("wordTotalHCcorporaEnUSblogs")){
# Total of words in the document
wordTotalHCcorporaEnUSblogs <- sum(
wordFreqHCcorporaEnUSblogs[which(wordFreqHCcorporaEnUSblogs != 0)])
# Compute the amount of words with a frequency less or equal to 5
wordFreq0_5lHCcorporaEnUSblogs <-
length(which(wordFreqHCcorporaEnUSblogs == 1))
wordFreq0_5lHCcorporaEnUSblogs <- wordFreq0_5lHCcorporaEnUSblogs +
length(which(wordFreqHCcorporaEnUSblogs == 2))
wordFreq0_5lHCcorporaEnUSblogs <- wordFreq0_5lHCcorporaEnUSblogs +
length(which(wordFreqHCcorporaEnUSblogs == 3))
wordFreq0_5lHCcorporaEnUSblogs <- wordFreq0_5lHCcorporaEnUSblogs +
length(which(wordFreqHCcorporaEnUSblogs == 4))
wordFreq0_5lHCcorporaEnUSblogs <- wordFreq0_5lHCcorporaEnUSblogs +
length(which(wordFreqHCcorporaEnUSblogs == 5))
# Compute the amount of words with a frequency less or equal to 9
wordFreq0_9lHCcorporaEnUSblogs <- wordFreq0_5lHCcorporaEnUSblogs +
length(which(wordFreqHCcorporaEnUSblogs == 6))
wordFreq0_9lHCcorporaEnUSblogs <- wordFreq0_9lHCcorporaEnUSblogs +
length(which(wordFreqHCcorporaEnUSblogs == 7))
wordFreq0_9lHCcorporaEnUSblogs <- wordFreq0_9lHCcorporaEnUSblogs +
length(which(wordFreqHCcorporaEnUSblogs == 8))
wordFreq0_9lHCcorporaEnUSblogs <- wordFreq0_9lHCcorporaEnUSblogs +
length(which(wordFreqHCcorporaEnUSblogs == 9))
# represents the percentege of the vocabullary that is cover by the words that
# have a frequency less or equal to 5
wordFreq0_5lHCcorporaEnUSblogsPercentege <-
wordFreq0_5lHCcorporaEnUSblogs/
length(which(wordFreqHCcorporaEnUSblogs != 0))
# represents the percentege of the vocabullary that is cover by the words that
# have a frequency less or equal to 9
wordFreq0_9lHCcorporaEnUSblogsPercentege <-
wordFreq0_9lHCcorporaEnUSblogs/
length(which(wordFreqHCcorporaEnUSblogs != 0))
# Word indexes order by frequency
wordOrderHCcorporaEnUSblogs <- order(wordFreqHCcorporaEnUSblogs, decreasing = TRUE)
# Represents the 10000 word more frequents of the vocabulary
percentege10kWordMoreFreqHCcorporaEnUSblogs <- sum(
wordFreqHCcorporaEnUSblogs[wordOrderHCcorporaEnUSblogs[1:10000]]) /
wordTotalHCcorporaEnUSblogs
# Represents the 2700 word more frequents of the vocabulary
percentege2700WordMoreFreqHCcorporaEnUSblogs <- sum(
wordFreqHCcorporaEnUSblogs[wordOrderHCcorporaEnUSblogs[1:2700]]) /
wordTotalHCcorporaEnUSblogs
# Represents the 140 word more frequents of the vocabulary
percentege140WordMoreFreqHCcorporaEnUSblogs <- sum(
wordFreqHCcorporaEnUSblogs[wordOrderHCcorporaEnUSblogs[1:140]]) /
wordTotalHCcorporaEnUSblogs
# Represents the 15 word more frequents of the vocabulary
percentege15WordMoreFreqHCcorporaEnUSblogs <- sum(
wordFreqHCcorporaEnUSblogs[wordOrderHCcorporaEnUSblogs[1:15]]) /
wordTotalHCcorporaEnUSblogs
}
hist(log10(wordFreqHCcorporaEnUSblogs[wordOrderHCcorporaEnUSblogs[1:10000]]),
main = "Log10 Distribution of 2700 more frequents word of EnUSblogs",
xlab = "log10 of word frequency")
```
Total of words in the vocabulary is `r length(wordFreqHCcorporaEnUSblogs)`
Facts for the EnUS blogs document:
* The percentege of words used is
`r length(which(wordFreqHCcorporaEnUSblogs != 0))/length(wordFreqHCcorporaEnUSblogs)*100`%
* The percentege of the vocabullary that is cover by the words that have a
frequency less or equal to 5 is
`r wordFreq0_5lHCcorporaEnUSblogsPercentege*100`%
* The percentege of the vocabullary that is cover by the words that have a
frequency less or equal to 9 is
`r wordFreq0_9lHCcorporaEnUSblogsPercentege*100`%
* The percentege of words in the document that is cover by the 10K (`r (10000/length(which(wordFreqHCcorporaEnUSblogs != 0)))*100`%) more
frequent words is `r percentege10kWordMoreFreqHCcorporaEnUSblogs*100`%
* The percentege of words in the document that is cover by the 2,7K (`r (2700/length(which(wordFreqHCcorporaEnUSblogs != 0)))*100`%) more
frequent words is `r percentege2700WordMoreFreqHCcorporaEnUSblogs*100`%
* The percentege of words in the document that is cover by the 140 (`r (140/length(which(wordFreqHCcorporaEnUSblogs != 0)))*100`%) more
frequent words is `r percentege140WordMoreFreqHCcorporaEnUSblogs*100`%
* The percentege of words in the document that is cover by the 15 (`r (15/length(which(wordFreqHCcorporaEnUSblogs != 0)))*100`%) more
frequent words is `r percentege15WordMoreFreqHCcorporaEnUSblogs*100`%
* The 15 more frequent words are:
`r vocabularyHCcorporaEnUSbyIndex[wordOrderHCcorporaEnUSblogs[1:15]]`
## Distribution of the frequencies of the vocabullary in En US news
```{r vocabullaryStatsEnUSnews, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("wordTotalHCcorporaEnUSnews")){
# Total of words in the document
wordTotalHCcorporaEnUSnews <- sum(
wordFreqHCcorporaEnUSnews[which(wordFreqHCcorporaEnUSnews != 0)])
# Compute the amount of words with a frequency less or equal to 2
wordFreq0_2lHCcorporaEnUSnews <-
length(which(wordFreqHCcorporaEnUSnews == 1))
wordFreq0_2lHCcorporaEnUSnews <- wordFreq0_2lHCcorporaEnUSnews +
length(which(wordFreqHCcorporaEnUSnews == 2))
# Compute the amount of words with a frequency less or equal to 5
wordFreq0_5lHCcorporaEnUSnews <- wordFreq0_2lHCcorporaEnUSnews +
length(which(wordFreqHCcorporaEnUSnews == 3))
wordFreq0_5lHCcorporaEnUSnews <- wordFreq0_5lHCcorporaEnUSnews +
length(which(wordFreqHCcorporaEnUSnews == 4))
wordFreq0_5lHCcorporaEnUSnews <- wordFreq0_5lHCcorporaEnUSnews +
length(which(wordFreqHCcorporaEnUSnews == 5))
# Compute the amount of words with a frequency less or equal to 9
wordFreq0_9lHCcorporaEnUSnews <- wordFreq0_5lHCcorporaEnUSnews +
length(which(wordFreqHCcorporaEnUSnews == 6))
wordFreq0_9lHCcorporaEnUSnews <- wordFreq0_9lHCcorporaEnUSnews +
length(which(wordFreqHCcorporaEnUSnews == 7))
wordFreq0_9lHCcorporaEnUSnews <- wordFreq0_9lHCcorporaEnUSnews +
length(which(wordFreqHCcorporaEnUSnews == 8))
wordFreq0_9lHCcorporaEnUSnews <- wordFreq0_9lHCcorporaEnUSnews +
length(which(wordFreqHCcorporaEnUSnews == 9))
# represents the percentege of the vocabullary that is cover by the words that
# have a frequency less or equal to 2
wordFreq0_2lHCcorporaEnUSnewsPercentege <-
wordFreq0_2lHCcorporaEnUSnews/
length(which(wordFreqHCcorporaEnUSnews != 0))
# represents the percentege of the vocabullary that is cover by the words that
# have a frequency less or equal to 5
wordFreq0_5lHCcorporaEnUSnewsPercentege <-
wordFreq0_5lHCcorporaEnUSnews/
length(which(wordFreqHCcorporaEnUSnews != 0))
# represents the percentege of the vocabullary that is cover by the words that
# have a frequency less or equal to 9
wordFreq0_9lHCcorporaEnUSnewsPercentege <-
wordFreq0_9lHCcorporaEnUSnews/
length(which(wordFreqHCcorporaEnUSnews != 0))
# Word indexes order by frequency
wordOrderHCcorporaEnUSnews <- order(wordFreqHCcorporaEnUSnews, decreasing = TRUE)
# Represents the 165000 word more frequents of the vocabulary
percentege165kWordMoreFreqHCcorporaEnUSnews <- sum(
wordFreqHCcorporaEnUSnews[wordOrderHCcorporaEnUSnews[1:165000]]) /
wordTotalHCcorporaEnUSnews
# Represents the 115000 word more frequents of the vocabulary
percentege115KWordMoreFreqHCcorporaEnUSnews <- sum(
wordFreqHCcorporaEnUSnews[wordOrderHCcorporaEnUSnews[1:115000]]) /
wordTotalHCcorporaEnUSnews
# Represents the 23000 word more frequents of the vocabulary
percentege23KWordMoreFreqHCcorporaEnUSnews <- sum(
wordFreqHCcorporaEnUSnews[wordOrderHCcorporaEnUSnews[1:23000]]) /
wordTotalHCcorporaEnUSnews
# Represents the 4000 word more frequents of the vocabulary
percentege4kWordMoreFreqHCcorporaEnUSnews <- sum(
wordFreqHCcorporaEnUSnews[wordOrderHCcorporaEnUSnews[1:4000]]) /
wordTotalHCcorporaEnUSnews
}
hist(log10(wordFreqHCcorporaEnUSnews[wordOrderHCcorporaEnUSnews[1:115000]]),
main = "Log10 Distribution of 115000 more frequents word of EnUSnews",
xlab = "log10 of word frequency")
```
Facts for the EnUS news document:
* The percentege of words used is
`r length(which(wordFreqHCcorporaEnUSnews != 0))/length(wordFreqHCcorporaEnUSnews)*100`%
* The percentege of the vocabullary that is cover by the words that have a
frequency less or equal to 2 is
`r wordFreq0_2lHCcorporaEnUSnewsPercentege*100`%
* The percentege of the vocabullary that is cover by the words that have a
frequency less or equal to 5 is
`r wordFreq0_5lHCcorporaEnUSnewsPercentege*100`%
* The percentege of the vocabullary that is cover by the words that have a
frequency less or equal to 9 is
`r wordFreq0_9lHCcorporaEnUSnewsPercentege*100`%
* The percentege of words in the document that is cover by the 165K (`r (165000/length(which(wordFreqHCcorporaEnUSnews != 0)))*100`%) more
frequent words is `r percentege165kWordMoreFreqHCcorporaEnUSnews*100`%
* The percentege of words in the document that is cover by the 115K (`r (115000/length(which(wordFreqHCcorporaEnUSnews != 0)))*100`%) more
frequent words is `r percentege115KWordMoreFreqHCcorporaEnUSnews*100`%
* The percentege of words in the document that is cover by the 23K (`r (23000/length(which(wordFreqHCcorporaEnUSnews != 0)))*100`%) more
frequent words is `r percentege23KWordMoreFreqHCcorporaEnUSnews*100`%
* The percentege of words in the document that is cover by the 4K (`r (4000/length(which(wordFreqHCcorporaEnUSnews != 0)))*100`%) more
frequent words is `r percentege4kWordMoreFreqHCcorporaEnUSnews*100`%
* The 15 more frequent words are:
`r vocabularyHCcorporaEnUSbyIndex[wordOrderHCcorporaEnUSnews[1:15]]`
## Distribution of the frequencies of the vocabullary in En US tweets
```{r vocabullaryStatsEnUStwitter, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("wordTotalHCcorporaEnUStwitter")){
# Total of words in the document
wordTotalHCcorporaEnUStwitter <- sum(
wordFreqHCcorporaEnUStwitter[which(wordFreqHCcorporaEnUStwitter != 0)])
# Compute the amount of words with a frequency less or equal to 2
wordFreq0_2lHCcorporaEnUStwitter <-
length(which(wordFreqHCcorporaEnUStwitter == 1))
wordFreq0_2lHCcorporaEnUStwitter <- wordFreq0_2lHCcorporaEnUStwitter +
length(which(wordFreqHCcorporaEnUStwitter == 2))
# Compute the amount of words with a frequency less or equal to 5
wordFreq0_5lHCcorporaEnUStwitter <- wordFreq0_2lHCcorporaEnUStwitter +
length(which(wordFreqHCcorporaEnUStwitter == 3))
wordFreq0_5lHCcorporaEnUStwitter <- wordFreq0_5lHCcorporaEnUStwitter +
length(which(wordFreqHCcorporaEnUStwitter == 4))
wordFreq0_5lHCcorporaEnUStwitter <- wordFreq0_5lHCcorporaEnUStwitter +
length(which(wordFreqHCcorporaEnUStwitter == 5))
# Compute the amount of words with a frequency less or equal to 9
wordFreq0_9lHCcorporaEnUStwitter <- wordFreq0_5lHCcorporaEnUStwitter +
length(which(wordFreqHCcorporaEnUStwitter == 6))
wordFreq0_9lHCcorporaEnUStwitter <- wordFreq0_9lHCcorporaEnUStwitter +
length(which(wordFreqHCcorporaEnUStwitter == 7))
wordFreq0_9lHCcorporaEnUStwitter <- wordFreq0_9lHCcorporaEnUStwitter +
length(which(wordFreqHCcorporaEnUStwitter == 8))
wordFreq0_9lHCcorporaEnUStwitter <- wordFreq0_9lHCcorporaEnUStwitter +
length(which(wordFreqHCcorporaEnUStwitter == 9))
# represents the percentege of the vocabullary that is cover by the words that
# have a frequency less or equal to 2
wordFreq0_2lHCcorporaEnUStwitterPercentege <-
wordFreq0_2lHCcorporaEnUStwitter/
length(which(wordFreqHCcorporaEnUStwitter != 0))
# represents the percentege of the vocabullary that is cover by the words that
# have a frequency less or equal to 5
wordFreq0_5lHCcorporaEnUStwitterPercentege <-
wordFreq0_5lHCcorporaEnUStwitter/
length(which(wordFreqHCcorporaEnUStwitter != 0))
# represents the percentege of the vocabullary that is cover by the words that
# have a frequency less or equal to 9
wordFreq0_9lHCcorporaEnUStwitterPercentege <-
wordFreq0_9lHCcorporaEnUStwitter/
length(which(wordFreqHCcorporaEnUStwitter != 0))
# Word indexes order by frequency
wordOrderHCcorporaEnUStwitter <- order(wordFreqHCcorporaEnUStwitter, decreasing = TRUE)
# Represents the 350000 word more frequents of the vocabulary
percentege350kWordMoreFreqHCcorporaEnUStwitter <- sum(
wordFreqHCcorporaEnUStwitter[wordOrderHCcorporaEnUStwitter[1:350000]]) /
wordTotalHCcorporaEnUStwitter
# Represents the 285000 word more frequents of the vocabulary
percentege285KWordMoreFreqHCcorporaEnUStwitter <- sum(
wordFreqHCcorporaEnUStwitter[wordOrderHCcorporaEnUStwitter[1:285000]]) /
wordTotalHCcorporaEnUStwitter
# Represents the 80000 word more frequents of the vocabulary
percentege80KWordMoreFreqHCcorporaEnUStwitter <- sum(
wordFreqHCcorporaEnUStwitter[wordOrderHCcorporaEnUStwitter[1:80000]]) /
wordTotalHCcorporaEnUStwitter
# Represents the 11000 word more frequents of the vocabulary
percentege11KWordMoreFreqHCcorporaEnUStwitter <- sum(
wordFreqHCcorporaEnUStwitter[wordOrderHCcorporaEnUStwitter[1:11000]]) /
wordTotalHCcorporaEnUStwitter
}
hist(log10(wordFreqHCcorporaEnUStwitter[wordOrderHCcorporaEnUStwitter[1:285000]]),
main = "Log10 Distribution of 285000 more frequents word of EnUStwitter",
xlab = "log10 of word frequency")
```
Facts for the EnUS tweets document:
* The percentege of words used is
`r length(which(wordFreqHCcorporaEnUStwitter != 0))/length(wordFreqHCcorporaEnUStwitter)*100`%
* The percentege of the vocabullary that is cover by the words that have a
frequency less or equal to 5 is
`r wordFreq0_5lHCcorporaEnUStwitterPercentege*100`%
* The percentege of the vocabullary that is cover by the words that have a
frequency less or equal to 9 is
`r wordFreq0_9lHCcorporaEnUStwitterPercentege*100`%
* The percentege of words in the document that is cover by the 350K (`r (350000/length(which(wordFreqHCcorporaEnUStwitter != 0)))*100`%) more
frequent words is `r percentege350kWordMoreFreqHCcorporaEnUStwitter*100`%
* The percentege of words in the document that is cover by the 285K (`r (285000/length(which(wordFreqHCcorporaEnUStwitter != 0)))*100`%) more
frequent words is `r percentege285KWordMoreFreqHCcorporaEnUStwitter*100`%
* The percentege of words in the document that is cover by the 80K (`r (80000/length(which(wordFreqHCcorporaEnUStwitter != 0)))*100`%) more
frequent words is `r percentege80KWordMoreFreqHCcorporaEnUStwitter*100`%
* The percentege of words in the document that is cover by the 11K (`r (11000/length(which(wordFreqHCcorporaEnUStwitter != 0)))*100`%) more
frequent words is `r percentege11KWordMoreFreqHCcorporaEnUStwitter*100`%
* The 15 more frequent words are:
`r vocabularyHCcorporaEnUSbyIndex[wordOrderHCcorporaEnUStwitter[1:15]]`
## Analysis of frequencies of LAST WORDS
```{r lastGramFreqTwitter, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("lastGramFreqEnUsTwitter")){
# Environment used as dictionary for 3-grams in last position for twitter source
vocabullarySimplifiedByWord <- new.env(hash = TRUE)
vocabullarySimplifiedByIndex <- NULL
vocabullarySimplifiedFreqByIndex <- NULL
tokenizeLine <- function(line){
line <- removeNumbers(line)
line <- removePunctuation(line)
line <- stripWhitespace(line)
line <- str_trim(line)
line <- str_to_lower(line)
words <- strsplit(line," ",fixed = TRUE)
words <- unlist(words)
# result variable to store the tokenize version of the line
tokenizeLine <- numeric(length = length(words))
# index variable of the word to tokenize
i <- 1
for(w in words){
# get the index of the word in our vocabulary dictionary
wIndex <- vocabullarySimplifiedByWord[[w]]
# check if the word exist in our vocabulary
if(is.null(wIndex)){
# the index for a new word it's just the lenght of the
# vocabulary
wIndex <- length(vocabullarySimplifiedByIndex) + 1
# add the word to the vocabulary dictionary by word
vocabullarySimplifiedByWord[[w]] <<- wIndex
# add the word to the vocabulary dictionary by index
vocabullarySimplifiedByIndex[wIndex] <<- w
# initialize the frequency for the word to 0
vocabullarySimplifiedFreqByIndex[wIndex] <<- 0
}
# increase the frequency of the word
vocabullarySimplifiedFreqByIndex[wIndex] <<- vocabullarySimplifiedFreqByIndex[wIndex] + 1
# append the index of the word the tokenized line
tokenizeLine[i] <- wIndex
# increase the index of the word to process
i <- i + 1
}
# return the tokenized line
tokenizeLine
}
# Environment used as dictionary for 3-grams in last position for twitter source
lastGramFreqEnUsTwitter <- new.env(hash = TRUE)
lastGramFreqEnUsTwitterList <- new.env(hash = TRUE)
addLastGramFreqEnUsTiwtter <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4],"_",gramVec[5],"_",gramVec[6])
pC <- lastGramFreqEnUsTwitter[[pI]]
if(is.null(pC))
pC <- 0
lastGramFreqEnUsTwitter[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4],"_",gramVec[5])
lC <- lastGramFreqEnUsTwitterList[[pI]]
if(is.null(lC))
lC <- gramVec[6]
else
lC <- c(lC, gramVec[6])
lastGramFreqEnUsTwitterList[[pI]] <<- lC
}
biGramFreqEnUsTwitter <- new.env(hash = TRUE)
biGramFreqEnUsTwitterList <- new.env(hash = TRUE)
addBiGramFreqEnUsTiwtter <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2])
pC <- biGramFreqEnUsTwitter[[pI]]
if(is.null(pC))
pC <- 0
biGramFreqEnUsTwitter[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1])
lC <- biGramFreqEnUsTwitterList[[pI]]
if(is.null(lC))
lC <- gramVec[2]
else
lC <- c(lC, gramVec[2])
biGramFreqEnUsTwitterList[[pI]] <<- lC
}
triGramFreqEnUsTwitter <- new.env(hash = TRUE)
triGramFreqEnUsTwitterList <- new.env(hash = TRUE)
addTriGramFreqEnUsTiwtter <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3])
pC <- triGramFreqEnUsTwitter[[pI]]
if(is.null(pC))
pC <- 0
triGramFreqEnUsTwitter[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1],"_",gramVec[2])
lC <- triGramFreqEnUsTwitterList[[pI]]
if(is.null(lC))
lC <- gramVec[3]
else
lC <- c(lC, gramVec[3])
triGramFreqEnUsTwitterList[[pI]] <<- lC
}
fourGramFreqEnUsTwitter <- new.env(hash = TRUE)
fourGramFreqEnUsTwitterList <- new.env(hash = TRUE)
addFourGramFreqEnUsTiwtter <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4])
pC <- fourGramFreqEnUsTwitter[[pI]]
if(is.null(pC))
pC <- 0
fourGramFreqEnUsTwitter[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3])
lC <- fourGramFreqEnUsTwitterList[[pI]]
if(is.null(lC))
lC <- gramVec[4]
else
lC <- c(lC, gramVec[4])
fourGramFreqEnUsTwitterList[[pI]] <<- lC
}
fiveGramFreqEnUsTwitter <- new.env(hash = TRUE)
fiveGramFreqEnUsTwitterList <- new.env(hash = TRUE)
addFiveGramFreqEnUsTiwtter <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4],"_",gramVec[5])
pC <- fiveGramFreqEnUsTwitter[[pI]]
if(is.null(pC))
pC <- 0
fiveGramFreqEnUsTwitter[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4])
lC <- fiveGramFreqEnUsTwitterList[[pI]]
if(is.null(lC))
lC <- gramVec[5]
else
lC <- c(lC, gramVec[5])
fiveGramFreqEnUsTwitterList[[pI]] <<- lC
}
procEndOfTweet <- function(tLt){
temp2 <- length(tLt)
if(temp2 > 5){
iN <- tLt[temp2]
iN_1 <- tLt[temp2-1]
iN_2 <- tLt[temp2-2]
i3 <- tLt[3]
i2 <- tLt[2]
i1 <- tLt[1]
ind <- c(i1,i2,i3,iN_2,iN_1,iN)
addLastGramFreqEnUsTiwtter(ind)
addBiGramFreqEnUsTiwtter(ind)
addTriGramFreqEnUsTiwtter(ind)
addFourGramFreqEnUsTiwtter(ind)
addFiveGramFreqEnUsTiwtter(ind)
# for(sW in 1:5){
# wildcard <- rep("*",sW)
# for(j in 1:(6-sW)){
# indTemp <- replace(ind, j:(j+sW-1), wildcard)
# addLastGramFreqEnUsTiwtter(indTemp)
# }
# }
}
}
text <- corpusHCcorporaEnUS.OnlyLetters[["en_US.twitter.txt"]]$content[1]
text <- "How are you Btw thanks for the RT You gonna be in DC anytime soon Love to see you Been way way too"
text <- "When you meet someone special youll know Your heart will beat more rapidly and youll smile for no"
vocabullarySimplifiedByWord <- new.env(hash = TRUE)
vocabullarySimplifiedByIndex <- NULL
vocabullarySimplifiedFreqByIndex <- NULL
lastGramFreqEnUsTwitter <- new.env(hash = TRUE)
lastGramFreqEnUsTwitterList <- new.env(hash = TRUE)
system.time(sapply(tokenizedHCcorporaEnUStwitter[1:10], procEndOfTweet))
system.time(sapply(tokenizedHCcorporaEnUStwitter[11:110], procEndOfTweet))
system.time(sapply(tokenizedHCcorporaEnUStwitter[111:1110], procEndOfTweet))
system.time(sapply(tokenizedHCcorporaEnUStwitter[1111:10000], procEndOfTweet))
system.time(sapply(tokenizedHCcorporaEnUStwitter[10001:20000], procEndOfTweet))
system.time(sapply(tokenizedHCcorporaEnUStwitter[20001:50000], procEndOfTweet))
iLe <- length(tokenizedHCcorporaEnUStwitter)
iLi <- iLe - 20000
system.time(sapply(tokenizedHCcorporaEnUStwitter[iLi:iLe], procEndOfTweet))
iLe <- iLi-1
iLi <- iLe - 20000
system.time(sapply(tokenizedHCcorporaEnUStwitter[iLi:iLe], procEndOfTweet))
save(vocabularyHCcorporaEnUSbyWord, vocabularyHCcorporaEnUSbyIndex, lastGramFreqEnUsTwitterList, lastGramFreqEnUsTwitter, file = "ReleaseTwitter.RData")
vocabullarySimplifiedByWord <- new.env(hash = TRUE)
vocabullarySimplifiedByIndex <- NULL
vocabullarySimplifiedFreqByIndex <- NULL
lastGramFreqEnUsTwitter <- new.env(hash = TRUE)
lastGramFreqEnUsTwitterList <- new.env(hash = TRUE)
biGramFreqEnUsTwitter <- new.env(hash = TRUE)
biGramFreqEnUsTwitterList <- new.env(hash = TRUE)
triGramFreqEnUsTwitter <- new.env(hash = TRUE)
triGramFreqEnUsTwitterList <- new.env(hash = TRUE)
fourGramFreqEnUsTwitter <- new.env(hash = TRUE)
fourGramFreqEnUsTwitterList <- new.env(hash = TRUE)
fiveGramFreqEnUsTwitter <- new.env(hash = TRUE)
fiveGramFreqEnUsTwitterList <- new.env(hash = TRUE)
iLe <- length(corpusHCcorporaEnUS.OnlyLetters[["en_US.twitter.txt"]]$content)
iLi <- iLe - 20000
temp <- c(corpusHCcorporaEnUS.OnlyLetters[["en_US.twitter.txt"]]$content[1:20000],
corpusHCcorporaEnUS.OnlyLetters[["en_US.twitter.txt"]]$content[iLi:iLe])
system.time(tokenizedSimplifiedEnUStwitter <- lapply(temp, tokenizeLine))
system.time(sapply(tokenizedSimplifiedEnUStwitter, procEndOfTweet))
}
```
```{r lastGramFreqNews, echo=FALSE, cache=TRUE, message=FALSE}
if(!exists("lastGramFreqEnUsNews")){
# Environment used as dictionary for 3-grams in last position for twitter source
lastGramFreqEnUsNews <- new.env(hash = TRUE)
lastGramFreqEnUsNewsList <- new.env(hash = TRUE)
addLastGramFreqEnUsNews <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4],"_",gramVec[5],"_",gramVec[6])
pC <- lastGramFreqEnUsNews[[pI]]
if(is.null(pC))
pC <- 0
lastGramFreqEnUsNews[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4],"_",gramVec[5])
lC <- lastGramFreqEnUsNewsList[[pI]]
if(is.null(lC))
lC <- gramVec[6]
else
lC <- c(lC, gramVec[6])
lastGramFreqEnUsNewsList[[pI]] <<- lC
}
biGramFreqEnUsNews <- new.env(hash = TRUE)
biGramFreqEnUsNewsList <- new.env(hash = TRUE)
addBiGramFreqEnUsNews <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2])
pC <- biGramFreqEnUsNews[[pI]]
if(is.null(pC))
pC <- 0
biGramFreqEnUsNews[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1])
lC <- biGramFreqEnUsNewsList[[pI]]
if(is.null(lC))
lC <- gramVec[2]
else
lC <- c(lC, gramVec[2])
biGramFreqEnUsNewsList[[pI]] <<- lC
}
triGramFreqEnUsNews <- new.env(hash = TRUE)
triGramFreqEnUsNewsList <- new.env(hash = TRUE)
addTriGramFreqEnUsNews <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3])
pC <- triGramFreqEnUsNews[[pI]]
if(is.null(pC))
pC <- 0
triGramFreqEnUsNews[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1],"_",gramVec[2])
lC <- triGramFreqEnUsNewsList[[pI]]
if(is.null(lC))
lC <- gramVec[3]
else
lC <- c(lC, gramVec[3])
triGramFreqEnUsNewsList[[pI]] <<- lC
}
fourGramFreqEnUsNews <- new.env(hash = TRUE)
fourGramFreqEnUsNewsList <- new.env(hash = TRUE)
addFourGramFreqEnUsNews <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4])
pC <- fourGramFreqEnUsNews[[pI]]
if(is.null(pC))
pC <- 0
fourGramFreqEnUsNews[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3])
lC <- fourGramFreqEnUsNewsList[[pI]]
if(is.null(lC))
lC <- gramVec[4]
else
lC <- c(lC, gramVec[4])
fourGramFreqEnUsNewsList[[pI]] <<- lC
}
fiveGramFreqEnUsNews <- new.env(hash = TRUE)
fiveGramFreqEnUsNewsList <- new.env(hash = TRUE)
addFiveGramFreqEnUsNews <- function(gramVec){
# Counter dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4],"_",gramVec[5])
pC <- fiveGramFreqEnUsNews[[pI]]
if(is.null(pC))
pC <- 0
fiveGramFreqEnUsNews[[pI]] <<- pC + 1
# Last word list dictionary
pI <- paste0(gramVec[1],"_",gramVec[2],"_",gramVec[3],"_",gramVec[4])
lC <- fiveGramFreqEnUsNewsList[[pI]]
if(is.null(lC))
lC <- gramVec[5]
else
lC <- c(lC, gramVec[5])
fiveGramFreqEnUsNewsList[[pI]] <<- lC
}
procEndOfNew <- function(tLt){
temp2 <- length(tLt)
if(temp2 > 5){
iN <- tLt[temp2]
iN_1 <- tLt[temp2-1]
iN_2 <- tLt[temp2-2]
i3 <- tLt[3]