In this report I will present the first part of exploratory analysis on multiple datasets regarding the development of the text prediction application. At my disposal are three datasets: news, blogs and twitter posts in English. For the analysis, I have used three R packages: tidytext, tidyverse and ggplot2.
library("tidytext")
library("tidyverse")
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library("ggplot2")
twitter_path <- "en_US/en_US.twitter.txt"
news_path <- "en_US/en_US.news.txt"
blogs_path <- "en_US/en_US.blogs.txt"
First I cleaned the data by removing stopwords. In this light, I used a dataframe of stopwords from multiple lexicons. Then, I counted the frequency of individual word and plotted the barplot of the top 20 words in each dataset. The number of lines used for this part is: twitter = 13000, blogs = 8000 and news = 8000.
data <- data.frame(readLines(con = twitter_path, n = 13000))
colnames(data) <- c("lines")
data %>%
unnest_tokens(output = word, input = lines) %>%
# removing stopwords by having a
# dataframe of stopwords from multiple lexicons.
# picking the words (that are present in the left df (data)
#but not present in the right df (stop_words))
anti_join(stop_words) %>%
filter(
!str_detect(word, pattern = "[[:digit:]]"), # removes any words with numeric digits
!str_detect(word, pattern = "[[:punct:]]"), # removes any remaining punctuations
!str_detect(word, pattern = "(.)\\1{2,}"), # removes any words with 3 or more repeated letters
!str_detect(word, pattern = "\\b(.)\\b") # removes any remaining single letter words
) %>%
count(word, sort = TRUE) %>%
# Plotting top 20 words in a given dataset
slice(1:20) %>%
ggplot() + geom_bar(aes(word, n), stat = "identity", fill = "grey") +
theme_minimal() + coord_flip() +
labs(title = "Twitter word count data")
Joining with `by = join_by(word)`
Joining with `by = join_by(word)`
Joining with `by = join_by(word)`
Depending on the dataset, the most common words are: time, people, day, percent, day and love.
Turning to bigrams, the code is a bit different and includes 15000 lines from each dataset.
data <- data.frame(readLines(con = twitter_path, n = 15000))
colnames(data) <- c("lines")
data %>%
unnest_tokens(word, lines, token = "ngrams", n = 2) %>%
# separating string into sets of two words
separate(word, c("word1", "word2"), sep = " ") %>%
filter(
!word1 %in% stop_words$word, # remove stopwords from both words in bi-gram
!word2 %in% stop_words$word,
!str_detect(word1, pattern = "[[:digit:]]"), # removes any words with numeric digits
!str_detect(word2, pattern = "[[:digit:]]"),
!str_detect(word1, pattern = "[[:punct:]]"), # removes any remaining punctuations
!str_detect(word2, pattern = "[[:punct:]]"),
!str_detect(word1, pattern = "(.)\\1{2,}"), # removes any words with 3 or more repeated letters
!str_detect(word2, pattern = "(.)\\1{2,}"),
!str_detect(word1, pattern = "\\b(.)\\b"), # removes any remaining single letter words
!str_detect(word1, pattern = "\\b(.)\\b")
) %>%
# merging two parts of the bigram
unite(word,word1, word2, sep = " ") %>%
count(word, sort = TRUE) %>%
slice(1:20) %>%
ggplot() + geom_bar(aes(word, n), stat = "identity", fill = "gray") +
theme_minimal() +
coord_flip() +
labs(title = "Twitter top bigrams")