library(tidytext)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
en_twitter_path <- 'Coursera-SwiftKey/final/en_US/en_US.twitter.txt'
en_twitter_lines <- readLines(en_twitter_path)
en_news_path <- 'Coursera-SwiftKey/final/en_US/en_US.news.txt'
en_news_lines <- readLines(en_news_path)
en_blogs_path <- 'Coursera-SwiftKey/final/en_US/en_US.blogs.txt'
en_blogs_lines <- readLines(en_blogs_path)
Here is a quick summary of the data:
# Displaying the first few lines of each dataset
head(en_twitter_lines)
## [1] "How are you? Btw thanks for the RT. You gonna be in DC anytime soon? Love to see you. Been way, way too long."
## [2] "When you meet someone special... you'll know. Your heart will beat more rapidly and you'll smile for no reason."
## [3] "they've decided its more fun if I don't."
## [4] "So Tired D; Played Lazer Tag & Ran A LOT D; Ughh Going To Sleep Like In 5 Minutes ;)"
## [5] "Words from a complete stranger! Made my birthday even better :)"
## [6] "First Cubs game ever! Wrigley field is gorgeous. This is perfect. Go Cubs Go!"
head(en_news_lines)
## [1] "He wasn't home alone, apparently."
## [2] "The St. Louis plant had to close. It would die of old age. Workers had been making cars there since the onset of mass automotive production in the 1920s."
## [3] "WSU's plans quickly became a hot topic on local online sites. Though most people applauded plans for the new biomedical center, many deplored the potential loss of the building."
## [4] "The Alaimo Group of Mount Holly was up for a contract last fall to evaluate and suggest improvements to Trenton Water Works. But campaign finance records released this week show the two employees donated a total of $4,500 to the political action committee (PAC) Partners for Progress in early June. Partners for Progress reported it gave more than $10,000 in both direct and in-kind contributions to Mayor Tony Mack in the two weeks leading up to his victory in the mayoral runoff election June 15."
## [5] "And when it's often difficult to predict a law's impact, legislators should think twice before carrying any bill. Is it absolutely necessary? Is it an issue serious enough to merit their attention? Will it definitely not make the situation worse?"
## [6] "There was a certain amount of scoffing going around a few years ago when the NFL decided to move the draft from the weekend to prime time -- eventually splitting off the first round to a separate day."
head(en_blogs_lines)
## [1] "In the years thereafter, most of the Oil fields and platforms were named after pagan “gods”."
## [2] "We love you Mr. Brown."
## [3] "Chad has been awesome with the kids and holding down the fort while I work later than usual! The kids have been busy together playing Skylander on the XBox together, after Kyan cashed in his $$$ from his piggy bank. He wanted that game so bad and used his gift card from his birthday he has been saving and the money to get it (he never taps into that thing either, that is how we know he wanted it so bad). We made him count all of his money to make sure that he had enough! It was very cute to watch his reaction when he realized he did! He also does a very good job of letting Lola feel like she is playing too, by letting her switch out the characters! She loves it almost as much as him."
## [4] "so anyways, i am going to share some home decor inspiration that i have been storing in my folder on the puter. i have all these amazing images stored away ready to come to life when we get our home."
## [5] "With graduation season right around the corner, Nancy has whipped up a fun set to help you out with not only your graduation cards and gifts, but any occasion that brings on a change in one's life. I stamped the images in Memento Tuxedo Black and cut them out with circle Nestabilities. I embossed the kraft and red cardstock with TE's new Stars Impressions Plate, which is double sided and gives you 2 fantastic patterns. You can see how to use the Impressions Plates in this tutorial Taylor created. Just one pass through your die cut machine using the Embossing Pad Kit is all you need to do - super easy!"
## [6] "If you have an alternative argument, let's hear it! :)"
Here are the word counts per file:
twitter_word_count <- sum(sapply(strsplit(en_twitter_lines, " "), length))
news_word_count <- sum(sapply(strsplit(en_news_lines, " "), length))
blogs_word_count <- sum(sapply(strsplit(en_blogs_lines, " "), length))
# Creating a data frame for summary
word_counts <- data.frame(Dataset = c("Twitter", "News", "Blogs"),
Word_Count = c(twitter_word_count, news_word_count, blogs_word_count))
word_counts
## Dataset Word_Count
## 1 Twitter 30373543
## 2 News 2643969
## 3 Blogs 37334131
Here are the most common words overall:
# Combine the lines from all datasets
all_lines <- c(en_twitter_lines, en_news_lines, en_blogs_lines)
# Create a data frame with the text data
text_df <- data.frame(text = all_lines)
# Tokenize the text
tokens <- text_df %>%
unnest_tokens(word, text)
# Remove stop words
tokens <- tokens %>%
anti_join(stop_words)
## Joining with `by = join_by(word)`
# Count the occurrences of each word
word_counts <- tokens %>%
count(word, sort = TRUE)
# Select the top 20 words
top_20_words <- word_counts[1:20, ]
# Plot the top 20 words
ggplot(top_20_words, aes(x = reorder(word, n), y = n)) +
geom_bar(stat = "identity", fill = "skyblue") +
coord_flip() +
labs(title = "Top 20 Most Occurring Words",
x = "Word",
y = "Frequency")
Here are the number of lines per file:
twitter_line_count <- length(en_twitter_lines)
news_line_count <- length(en_news_lines)
blogs_line_count <- length(en_blogs_lines)
# Creating a data frame for summary
line_counts <- data.frame(Dataset = c("Twitter", "News", "Blogs"),
Line_Count = c(twitter_line_count, news_line_count, blogs_line_count))
line_counts
## Dataset Line_Count
## 1 Twitter 2360148
## 2 News 77259
## 3 Blogs 899288
Bar Plot:
# Creating a bar plot of line counts
barplot(c(twitter_line_count, news_line_count, blogs_line_count),
names.arg = c("Twitter", "News", "Blogs"),
main = "Number of Lines in Each Dataset",
xlab = "Dataset",
ylab = "Line Count",
col = c("lightgreen", "lightblue", "lightcoral"))
he final objective of the capstone project is to create a predictive algorithm that will be implemented as a Shiny app, providing a user-friendly interface. The primary functionality of the Shiny app will involve predicting the next word based on user-input phrases.
To achieve this, the predictive algorithm will employ an n-gram model, incorporating a word frequency lookup mechanism akin to the exploratory data analysis conducted earlier in this report. The strategy for building the model will leverage insights gained from the exploratory analysis. Notably, the analysis revealed an inverse relationship between n-gram size and term frequency, indicating that as n increased, the frequency decreased for each of its terms.
A potential strategy for constructing the model is as follows: initially, the algorithm will identify the most likely unigram to follow the entered text. Once a complete term is entered, followed by a space, the model will then seek the most common bigram, and so forth. This sequential approach aligns with the observed pattern in the data, enhancing the predictive capabilities of the algorithm.