NGramTracker tracker; String buffer = "abc"; // stores input from keyboard int n = 3; // length of n-grams (if you change this, change the initial value of buffer) PFont font; PFont bigFont; float fontSize = 11; float bigFontSize = 48; ArrayList displayCurrent = new ArrayList(); void setup() { size(800, 480); smooth(); tracker = new NGramTracker(n); font = createFont("Arial", fontSize); bigFont = createFont("Arial", bigFontSize); // feed data into our n-gram tracker object String[] words = loadStrings("sowpods.txt"); for (int i = 0; i < words.length; i++) { tracker.feed(words[i]); } // fill arraylist with words to display for initial n-gram displayCurrent = tracker.getWordsForGram(buffer); } void draw() { background(255); fill(0); // draw input from keyboard, with trailing underscore to encourage // interaction!! textFont(bigFont); text(buffer + "_", 0, 64); // draw text from displayCurrent in columns textFont(font); float currentX = 0; float currentY = bigFontSize * 2; float delta = fontSize + 2; for (int i = 0; i < displayCurrent.size(); i++) { String display = (String)displayCurrent.get(i); text(display, currentX, currentY); currentY += delta; if (currentY > height) { currentY = bigFontSize * 2; currentX += fontSize * 8; } } } void keyPressed() { // if the key was alphanumeric, add it to the buffer if (key >= 'a' && key <= 'z') { buffer += key; } // if the buffer's too long, take only the last n characters if (buffer.length() > n) { buffer = buffer.substring(1, n+1); } // reload words from tracker displayCurrent = tracker.getWordsForGram(buffer); }