Skip to content

Instantly share code, notes, and snippets.

@veryphatic
Created July 27, 2012 23:11
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save veryphatic/3190969 to your computer and use it in GitHub Desktop.
Save veryphatic/3190969 to your computer and use it in GitHub Desktop.
Simple Markov chain text generator
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.Random;
import java.util.Vector;
public class Markov {
// Hashmap
public static Hashtable<String, Vector<String>> markovChain = new Hashtable<String, Vector<String>>();
static Random rnd = new Random();
/*
* Main constructor
*/
public static void main(String[] args) throws IOException {
// Create the first two entries (k:_start, k:_end)
markovChain.put("_start", new Vector<String>());
markovChain.put("_end", new Vector<String>());
while(true) {
// Get some words
System.out.print("Enter your phrase > ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String sInput = in.readLine() + ".";
// Add the words to the hash table
addWords(sInput);
}
}
/*
* Add words
*/
public static void addWords(String phrase) {
// put each word into an array
String[] words = phrase.split(" ");
// Loop through each word, check if it's already added
// if its added, then get the suffix vector and add the word
// if it hasn't been added then add the word to the list
// if its the first or last word then select the _start / _end key
for (int i=0; i<words.length; i++) {
// Add the start and end words to their own
if (i == 0) {
Vector<String> startWords = markovChain.get("_start");
startWords.add(words[i]);
Vector<String> suffix = markovChain.get(words[i]);
if (suffix == null) {
suffix = new Vector<String>();
suffix.add(words[i+1]);
markovChain.put(words[i], suffix);
}
} else if (i == words.length-1) {
Vector<String> endWords = markovChain.get("_end");
endWords.add(words[i]);
} else {
Vector<String> suffix = markovChain.get(words[i]);
if (suffix == null) {
suffix = new Vector<String>();
suffix.add(words[i+1]);
markovChain.put(words[i], suffix);
} else {
suffix.add(words[i+1]);
markovChain.put(words[i], suffix);
}
}
}
generateSentence();
}
/*
* Generate a markov phrase
*/
public static void generateSentence() {
// Vector to hold the phrase
Vector<String> newPhrase = new Vector<String>();
// String for the next word
String nextWord = "";
// Select the first word
Vector<String> startWords = markovChain.get("_start");
int startWordsLen = startWords.size();
nextWord = startWords.get(rnd.nextInt(startWordsLen));
newPhrase.add(nextWord);
// Keep looping through the words until we've reached the end
while (nextWord.charAt(nextWord.length()-1) != '.') {
Vector<String> wordSelection = markovChain.get(nextWord);
int wordSelectionLen = wordSelection.size();
nextWord = wordSelection.get(rnd.nextInt(wordSelectionLen));
newPhrase.add(nextWord);
}
System.out.println("New phrase: " + newPhrase.toString());
}
}
@quintendewilde
Copy link

public static Hashtable<String, Vector> markovChain = new Hashtable<String, Vector>();

=> error : The field markovChain cannot be declared static; static fields can only be declared in static or top level types

Can you explain this error, I'm using processing

@quintendewilde
Copy link

where are the libraries to import (hashtable & vector)

@gotsingh-zz
Copy link

Thanks for posting this. By looking through it, I was able to gain a better understanding of the basic Markov chain model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment