We have three files containg text from twitter, blog posts and news articles.
library(tidyverse);library(quanteda);library(quanteda.textplots);library(quanteda.textstats)
set.seed(240101)
twitter_data <- read_lines("./en_US/en_US.twitter.txt")
blog_data <- read_lines("./en_US/en_US.blogs.txt")
news_data <- read_lines("./en_US/en_US.news.txt")
length_twitter <- length(twitter_data)
length_news <- length(news_data)
length_blog <- length(blog_data)
words_twitter <- sum(str_count(twitter_data))
words_news <- sum(str_count(news_data))
words_blog <- sum(str_count(blog_data))
lenght_summary <- paste(
"Total Amount of lines in each dataset.\n",
"Twitter length:\t ",
format(length_twitter,big.mark=",",scientific=FALSE),
"\nNews length:\t\t ",
format(length_news,big.mark=",",scientific=FALSE),
"\nBlog Length:\t\t",
format(length_blog,big.mark=",",scientific=FALSE),
"\n"
)
Word_summary <- paste(
"Approximate number of words in each dataset.\n",
"Twitter length:\t ",
format(words_twitter,big.mark=",",scientific=FALSE),
"\nNews length:\t\t ",
format(words_news,big.mark=",",scientific=FALSE),
"\nBlog Length:\t\t",
format(words_blog,big.mark=",",scientific=FALSE)
)
cat(lenght_summary)
Total Amount of lines in each dataset. Twitter length: 2,360,148 News length: 1,010,242 Blog Length: 899,288
cat(Word_summary)
Approximate number of words in each dataset. Twitter length: 162,095,765 News length: 203,223,153 Blog Length: 206,824,247
The quanteda R package was utilized to generate word tokens and also remove punctuation, symbols, separators, URL, and english articles.
my_Text_Tokens <- tokens(c(blog_data,twitter_data,news_data),
remove_symbols = TRUE,
remove_punct = TRUE,
remove_numbers = TRUE,
remove_separators = TRUE,
remove_url =TRUE
) %>%
tokens_tolower() %>%
tokens_remove(stopwords(source = "smart"), padding = FALSE)
Quanteda was then utilized to generate a document feature matrix to help examine the frequency of each of the words
my_feature_matrix <- dfm(my_Text_Tokens)
Next a plot of the top 20 words found in the compiled data set were plotted against the frequency of them occurring. Additionally a word cloud was generated to present the data in another manner to can insight of the words utilized.
my_feature_matrix %>%
textstat_frequency(n=20) %>%
ggplot(aes(x=frequency,y= reorder(feature, frequency))) +
geom_point() +
labs(x = "Frequency", y = "Feature")+
theme_minimal()+
ggtitle("Top 20 Word frequency")
my_feature_matrix %>%
textplot_wordcloud()
The amount of data may be an issue when utilizing the feature matrix for a prediction model. A sampleing of the final feature matrix or of the initial data may be required. The computer utilized to generate this intial analysis had 16 threads and 64 Gb of RAM and the free shiny servers are limited to 1 gb ram.
sessionInfo()
## R version 4.3.1 (2023-06-16 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19045)
##
## Matrix products: default
##
##
## locale:
## [1] LC_COLLATE=English_United States.utf8
## [2] LC_CTYPE=English_United States.utf8
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.utf8
##
## time zone: America/New_York
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] quanteda.textstats_0.96.4 quanteda.textplots_0.94.3
## [3] quanteda_3.3.1 lubridate_1.9.3
## [5] forcats_1.0.0 stringr_1.5.1
## [7] dplyr_1.1.4 purrr_1.0.2
## [9] readr_2.1.4 tidyr_1.3.0
## [11] tibble_3.2.1 ggplot2_3.4.4
## [13] tidyverse_2.0.0
##
## loaded via a namespace (and not attached):
## [1] sass_0.4.8 utf8_1.2.4 generics_0.1.3 stringi_1.8.3
## [5] lattice_0.22-5 hms_1.1.3 digest_0.6.33 magrittr_2.0.3
## [9] evaluate_0.23 grid_4.3.1 timechange_0.2.0 fastmap_1.1.1
## [13] jsonlite_1.8.8 Matrix_1.6-4 stopwords_2.3 fansi_1.0.6
## [17] scales_1.3.0 jquerylib_0.1.4 cli_3.6.2 crayon_1.5.2
## [21] rlang_1.1.2 bit64_4.0.5 munsell_0.5.0 withr_2.5.2
## [25] cachem_1.0.8 yaml_2.3.8 parallel_4.3.1 tools_4.3.1
## [29] tzdb_0.4.0 colorspace_2.1-0 fastmatch_1.1-4 vctrs_0.6.5
## [33] R6_2.5.1 lifecycle_1.0.4 bit_4.0.5 vroom_1.6.5
## [37] pkgconfig_2.0.3 RcppParallel_5.1.7 pillar_1.9.0 bslib_0.6.1
## [41] gtable_0.3.4 glue_1.6.2 Rcpp_1.0.11 highr_0.10
## [45] xfun_0.41 tidyselect_1.2.0 rstudioapi_0.15.0 knitr_1.45
## [49] farver_2.1.1 htmltools_0.5.7 labeling_0.4.3 rmarkdown_2.25
## [53] compiler_4.3.1 nsyllable_1.0.1