Class TwitterBot

java.lang.Object
TwitterBot

public class TwitterBot
extends 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.

  • Field Summary

    Fields
    Modifier and Type Field Description
    (package private) static int MAX_TWEET_LENGTH  
    (package private) MarkovChain mc  
    (package private) NumberGenerator ng  
    (package private) static String PATH_TO_OUTPUT_TWEETS  
    (package private) static String PATH_TO_TWEETS
    This is a path to the CSV file containing the tweets.
    (package private) static int TWEET_COLUMN  
  • Constructor Summary

    Constructors
    Constructor Description
    TwitterBot​(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​(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
    void fixDistribution​(List<String> tweet)
    Modifies all MarkovChains to output sentences in the order specified
    int fixPunctuation​(char punc)
    A helper function to return the numerical index of the punctuation.
    String generateTweet​(int length)
    Generates a tweet of a given length by using the populated MarkovChain.
    List<String> generateTweets​(int numTweets, int tweetLength)
    Generates a series of tweets using generateTweet().
    static boolean isPunctuated​(String s)
    A helper function to determine if a string ends in punctuation.
    boolean isPunctuation​(String s)
    Returns true if the passed in string is punctuation.
    static void main​(String[] args)
    Prints ten generated tweets to the console so you can see how your bot is performing!
    String randomPunctuation()
    A helper function for providing a random punctuation String.
    void writeStringsToFile​(List<String> stringsToWrite, 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, String filePath, boolean append)
    Generates tweets and writes them to a file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_TWEET_LENGTH

      static final int MAX_TWEET_LENGTH
      See Also:
      Constant Field Values
    • PATH_TO_TWEETS

      static final String PATH_TO_TWEETS
      This is a path to the CSV file containing the tweets. The main method below uses the tweets in this file when calling Twitterbot. If you want to run the Twitterbot on the other files we provide, change this path to a different file. (You may need to adjust the TWEET_COLUMN too.)
      See Also:
      Constant Field Values
    • TWEET_COLUMN

      static final int TWEET_COLUMN
      See Also:
      Constant Field Values
    • PATH_TO_OUTPUT_TWEETS

      static final String PATH_TO_OUTPUT_TWEETS
      See Also:
      Constant Field Values
    • mc

    • ng

  • Constructor Details

    • TwitterBot

      public TwitterBot​(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 data
      tweetColumn - - the column in that CSV where the text of the tweet itself is stored
    • TwitterBot

      public TwitterBot​(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.
      Parameters:
      csvFile - - a path to a CSV file containing tweet data
      tweetColumn - - the column in that CSV where the text of the tweet itself is stored
      ng - - A NumberGenerator for the ng field, also to be passed to MarkovChain
  • Method Details

    • writeStringsToFile

      public void writeStringsToFile​(List<String> stringsToWrite, 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 file
      filePath - - the string containing the path to the file where the tweets should be written
      append - - 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, String filePath, boolean append)
      Generates tweets and writes them to a file.
      Parameters:
      numTweets - - the number of tweets that should be written
      tweetLength - - the approximate length (in characters) of each tweet
      filePath - - the path to a file to write the tweets to
      append - - a boolean indicating whether the new tweets should be appended to the current file or should overwrite its previous contents
    • generateTweet

      public 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 ever makes your tweet's length greater than the desired length, you should include it and end the tweet with randomPunctuation(). 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:
      IllegalArgumentException - if length is less than 1 or greater than MAX_TWEET_LENGTH
    • generateTweets

      public List<String> generateTweets​(int numTweets, int tweetLength)
      Generates a series of tweets using generateTweet().
      Parameters:
      numTweets - - the number of tweets to generate
      tweetLength - - the length that each generated tweet should be.
      Returns:
      a List of Strings where each element is a tweet
    • randomPunctuation

      public 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
    • fixPunctuation

      public int fixPunctuation​(char punc)
      A helper function to return the numerical index of the punctuation.
      Parameters:
      punc - - an input char to return the index of
      Returns:
      the numerical index of the punctuation
    • isPunctuation

      public boolean isPunctuation​(String s)
      Returns true if the passed in string is punctuation.
      Parameters:
      s - - a string to check whether or not it's punctuation
      Returns:
      true if the string is punctuation, false otherwise.
    • isPunctuated

      public static boolean isPunctuated​(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​(String[] args)
      Prints ten generated tweets to the console so you can see how your bot is performing!
    • fixDistribution

      public void fixDistribution​(List<String> tweet)
      Modifies all MarkovChains to output sentences in the order specified
      Parameters:
      tweet - - an ordered list of words that the Markov chains should output