Class TwitterBot
java.lang.Object
TwitterBot
public class TwitterBot
extends java.lang.Object
This is the class where everything you've worked on thus far comes together!
You can see that we've provided a path to a CSV file full of tweets and the
column from which they can be extracted. When run as an application, this
program builds a Markov Chain from the training data in the CSV file,
generates 10 random tweets, and prints them to the terminal.
This class also provides the writeTweetsToFile method, which can be used to
create a file containing randomly generated tweets.
Note: All IOExceptions thrown by writers should be caught and handled
properly.
-
Constructor Summary
Constructors Constructor Description TwitterBot(java.lang.String csvFile, int tweetColumn)
Given a column and a path to the csvFile, initializes the TwitterBot by training the MarkovChain with sentences sourced from that CSV file.TwitterBot(java.lang.String csvFile, int tweetColumn, NumberGenerator ng)
Given a column and a path to the csvFile, initializes the TwitterBot by training the MarkovChain with all the sentences obtained as training data from that CSV file. -
Method Summary
Modifier and Type Method Description java.lang.String
generateTweet(int length)
Generates a tweet of a given length by using the populated MarkovChain.java.util.List<java.lang.String>
generateTweets(int numTweets, int tweetLength)
Generates a series of tweets using generateTweet().static boolean
isPunctuated(java.lang.String s)
A helper function to determine if a string ends in punctuation.static void
main(java.lang.String[] args)
Prints ten generated tweets to the console so you can see how your bot is performing!java.lang.String
randomPunctuation()
A helper function for providing a random punctuation String.void
writeStringsToFile(java.util.List<java.lang.String> stringsToWrite, java.lang.String filePath, boolean append)
Given a List of Strings, prints those Strings to a file (one String per line in the file).void
writeTweetsToFile(int numTweets, int tweetLength, java.lang.String filePath, boolean append)
Generates tweets and writes them to a file.
-
Constructor Details
-
TwitterBot
public TwitterBot(java.lang.String csvFile, int tweetColumn)Given a column and a path to the csvFile, initializes the TwitterBot by training the MarkovChain with sentences sourced from that CSV file. Uses the RandomNumberGenerator().- Parameters:
csvFile
- - a path to a CSV file containing tweet datatweetColumn
- - the column in that CSV where the text of the tweet itself is stored
-
TwitterBot
Given a column and a path to the csvFile, initializes the TwitterBot by training the MarkovChain with all the sentences obtained as training data from that CSV file.- Parameters:
csvFile
- - a path to a CSV file containing tweet datatweetColumn
- - the column in that CSV where the text of the tweet itself is storedng
- - A NumberGenerator for the ng field, also to be passed to MarkovChain
-
-
Method Details
-
writeStringsToFile
public void writeStringsToFile(java.util.List<java.lang.String> stringsToWrite, java.lang.String filePath, boolean append)Given a List of Strings, prints those Strings to a file (one String per line in the file). This method uses BufferedWriter, the flip side to BufferedReader. Ensure that each tweet you generate is written on its own line in the file produced. You may assume none of the arguments or strings passed in will be null. If the process of writing the data triggers an IOException, you should catch it and stop writing. (You can also print an error message to the terminal, but we will not test that behavior.)- Parameters:
stringsToWrite
- - A List of Strings to write to the filefilePath
- - the string containing the path to the file where the tweets should be writtenappend
- - a boolean indicating whether the new tweets should be appended to the current file or should overwrite its previous contents
-
writeTweetsToFile
public void writeTweetsToFile(int numTweets, int tweetLength, java.lang.String filePath, boolean append)Generates tweets and writes them to a file.- Parameters:
numTweets
- - the number of tweets that should be writtentweetLength
- - the approximate length (in characters) of each tweetfilePath
- - the path to a file to write the tweets toappend
- - a boolean indicating whether the new tweets should be appended to the current file or should overwrite its previous contents
-
generateTweet
public java.lang.String generateTweet(int length)Generates a tweet of a given length by using the populated MarkovChain. Remember in the writeup where we explained how to use MarkovChain to pick a random starting word and then pick each subsequent word based on the probability that it follows the one before? This is where you implement that core logic! Use the (assumed to be trained) MarkovChain as an iterator to build up a String that represents the tweet that's returned. 1. validate the length argument 2. reset the MarkovChain (to prepare it to generate a new sentence) 3. repeatedly generate new words to add to the tweet: 3.a If the MarkovChain has no more values in its Iterator but the tweet is not yet at the required length, use randomPunctuation() to end the sentence and then reset() to begin the next sentence with a random start word. If appending a word or punctuation ever makes your tweet's length >= the desired length, you should include it and end the tweet with randomPunctuation() (assuming it's not punctuated already). The resulting tweet may be slightly longer than the desired length (and possibly MAX_TWEET_LENGTH), but it should contain no spaces past the input length. Your tweet should be properly formatted with one space between each word and between sentences. It should not contain any leading or trailing whitespace. You should leave the words uncapitalized, just as they are from TweetParser. You should return an empty string if there were no sentences available to train the Markov Chain. You also need to do some input validation to make sure the length is appropriate.- Parameters:
length
- - The desired (approximate) length of the tweet (in characters) to be produced- Returns:
- a String representing a generated tweet
- Throws:
java.lang.IllegalArgumentException
- if length is less than 1 or greater than MAX_TWEET_LENGTH
-
generateTweets
public java.util.List<java.lang.String> generateTweets(int numTweets, int tweetLength)Generates a series of tweets using generateTweet().- Parameters:
numTweets
- - the number of tweets to generatetweetLength
- - the length that each generated tweet should be.- Returns:
- a List of Strings where each element is a tweet
-
randomPunctuation
public java.lang.String randomPunctuation()A helper function for providing a random punctuation String. Returns '.' 70% of the time and ';', '?', and '!' each 10% of the time.- Returns:
- a string containing just one punctuation character
-
isPunctuated
public static boolean isPunctuated(java.lang.String s)A helper function to determine if a string ends in punctuation.- Parameters:
s
- - an input string to check for punctuation- Returns:
- true if the string s ends in punctuation
-
main
public static void main(java.lang.String[] args)Prints ten generated tweets to the console so you can see how your bot is performing!
-