Chapter 14 Word Perfect
In the previous chapter we mastered some of the most basic and important functions for examining and manipulating text. Now we are in a position to analyze the actual words that appear in text documents. Some of the most basic functions of the Internet, such as keyword search, are accomplished by analyzing the "content" i.e., the words in a body of text.
Sources and R Code.
Sources Used in This Chapter
R Code for CleanTweets() Function
# CleanTweets() - Takes the junk out of a vector of
# tweet texts
CleanTweets<-function(tweets)
{
# Remove redundant spaces
tweets <- str_replace_all(tweets," "," ")
# Get rid of URLs
tweets <- str_replace_all(tweets, + β¨ "http://t.co/[a-z,A-Z,0-9]{8}","")
# Take out retweet header, there is only one
tweets <- str_replace(tweets,"RT @[a-z,A-Z]*: ","")
# Get rid of hashtags
tweets <- str_replace_all(tweets,"#[a-z,A-Z]*","")
# Get rid of references to other screennames
tweets <- str_replace_all(tweets,"@[a-z,A-Z]*","")
return(tweets)
}
