Markov Chain Text Gen

alice-in-wonderland[1].jpg

Markov chains are a list ngrams (grouped portions of text) from a work of text with probabilities of what will come next grouped to each ngram. You can then use this to generate new text based on those probabilities.

Here’s some made with Alice in Wonderland:

cupboards as long ago anything; she had began dream. There isn't,' said to Alice laughed so far

'Ugh!' said the puppy began, in a low, trembling voice. 'I've offended tone, and raised him declare it's

ven't think I can't help that,' said the Hatter. He had caught there ought Alice thought she went on,


HashMap> createTextMarkov(String txt, int order) {
    HashMap> ngrams = new HashMap>();
    for (int i = 0; i < txt.length() - order; i++) {
      String gram = txt.substring(i, i + order);

      if (!ngrams.containsKey(gram)) {
        ngrams.put(gram, new ArrayList());
      }
      ngrams.get(gram).add(txt.charAt(i + order));
    }
    return ngrams;
  }
  
  
  String genText(HashMap> markovMap) {
    Object[] keyArray = markovMap.keySet().toArray();
    String starting = (String) keyArray[(int) random(0, keyArray.length)];
    int order = starting.length();
    String result = starting;
    String currentGram = starting;

    for (int i = 0; i < 100; i++) {
      if (!markovMap.containsKey(currentGram)) {
        break;
      }
      ArrayList possibilities = markovMap.get(currentGram);

      String next = "" + possibilities.get((int) random(0, possibilities.size()));
      result += next;
      currentGram = result.substring(result.length() - order, result.length());
    }
    return result;
  }


A Markov chain can also be made with words instead of ngrams.

dogs The Pool of uglifying it flashed across her draw the baby at last concert on in a crimson velvet cushion resting their slates when he went Alice its voice Fetch me for some executions I make it unfolded the Pigeon but it was too much of course the other the Mouse who will you want to the March Hare I didnt interrupted in Alice who had put his scaly friend replied counting off her hand round THAT direction waving its full of mine said to set the Duchess asked triumphantly pointing to your waist the slate with Seaography then another moment he knows it is wrong and marked poison or something better



HashMap> createTextMarkovByWord(String txt) { 
  HashMap> ngrams = new HashMap>();
  
  txt = txt.replace('\n',' ').replace("-", "").replace("\"", "").replace("'", "").replace(".", "").replace("?", "").replace("!", "").replace(",", "").replace("\t", "").replace("\r", "").replace(":", "").replace(";", "");
  
  String[] words = txt.split(" ");
  for (int i = 0; i < words.length - 1; i++) {
      String gram = words[i];

      if (!ngrams.containsKey(gram)) {
        ngrams.put(gram, new ArrayList());
      }
      ngrams.get(gram).add(words[i+1]);
    }
  
  
  return ngrams;
}

String genWordsText(HashMap> markovMap) {
    Object[] keyArray = markovMap.keySet().toArray();
    String starting = (String) keyArray[(int) random(0, keyArray.length)];
    String result = starting;
    String currentGram = starting;

    for (int i = 0; i < 1000; i++) {
      if (!markovMap.containsKey(currentGram)) {
        break;
      }
      ArrayList possibilities = markovMap.get(currentGram);

      String next = possibilities.get((int) random(0, possibilities.size()));
      result += " " + next;
      currentGram = next;
    }
    return result;
  }