-2

I've been looking in to generate text.

What I've learned so far is that I will have to use word-level Markov-text generation. I've found a few examples of those on this site. here

Now knowing this wouldn't work I tried it anyways and copied it to Processing. With the errors of not finding the correct libraries.

Is there anyone out there that has done this or can point me in a good direction to find more about doing text generation with processing. Or even somebody who want's to do a collab. Being open source and what not.

What I want isn't that more different than the example on the site, except the letter count should be word based and the database is given by words I put in there. The last part could be altered to an other source which I'm still brainstorming about. But could be everything actually with words. If you have any ideas please be free to contribute.

I'll edit this post when I know more from other forums. So when there's a solution I can pass it to others.

EDIT: SOLUTION CLICKBASED GENERATING

// required imports for Processing
import java.util.Hashtable;
import java.util.Vector;


String inputFile = "Sonnet51.txt";
Markov markovChain1;
String sentence = ""; 

void setup() {
  size (900, 500);
  background(0);

  markovChain1 = new Markov();

  // load text
  String[] input = loadStrings(inputFile);
  for (String line : input) {
    markovChain1.addWords(line);
    println(line);
  }

  // generate a sentence!
  sentence = markovChain1.generateSentence();

  println("-------------");
}


void draw() {
  background(0);
  // noLoop();
  fill(255);
  text(sentence, 19, 190);

  fill(2, 255, 2);
  text("Please press mouse", 19, height-33);
}

void mousePressed() {
  // generate a sentence!
  sentence = markovChain1.generateSentence();
  println(sentence);
}

// ==========================================

class Markov {
  Hashtable<String, Vector<String>> markovChain = 
        new Hashtable<String, Vector<String>>();

  Markov() {
    markovChain.put("_start", new Vector<String>());
    markovChain.put("_end", new Vector<String>());
  }

  void addWords(String line) {
    String[] words = line.split(" ");

    for (int i=0; i<words.length; i++) {

      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);
        }
      }
    }
  }

  String generateSentence() {
    String newPhrase = "";
    String nextWord  = "";
    Vector<String> startWords = markovChain.get("_start");
    int startWordsLen = startWords.size();
    nextWord = startWords.get(int(random(startWordsLen)));
    newPhrase += " " + nextWord;
    while (nextWord.charAt (nextWord.length ()-1) != '.') {
      Vector<String> wordSelection=null; 
      wordSelection = markovChain.get(nextWord);
      if (wordSelection!=null) {
        int wordSelectionLen = wordSelection.size();
        nextWord = wordSelection.get(int(random(wordSelectionLen-1)));
        newPhrase += " " + nextWord;
      }
      else
      {
        return newPhrase.toString();
      }
    }
    return newPhrase.toString();
  }
} // class
//

use following text to use for the generator.

Thus can my love excuse the slow offence
Of my dull bearer when from thee I speed
From where thou art why should I haste me thence
Till I return of posting is no need
O! what excuse will my poor beast then find
When swift extremity can seem but slow
Then should I spur though mounted on the wind.
In winged speed no motion shall I know.
Then can no horse with my desire keep pace.
Therefore desire of perfectst love being made.
Shall neigh no dull flesh in his fiery race;
But love for love thus shall excuse my jade.
Since from thee going, he went wilful-slow
Towards thee Ill run, and give him leave to go.

It works completely and now I can begin to change it for making bigger texts. I anybody have ideas let me know. But this case is solved for me. Thanks to ChrisIr from Processing forum.

chelmertz
  • 20,399
  • 5
  • 40
  • 46
Blckpstv
  • 117
  • 3
  • 17
  • 1
    Please show some code. Please explain concrete problems. – DrKoch Jan 13 '15 at 12:48
  • The code appears to create a sorted list of words, and performs a binary search on it. There are no frequencies / weights / coefficients, so hardly a Markov process. Oh, he appears to store multiple words into one "word". Yuck. – joop Jan 13 '15 at 13:49
  • I didn't write the code my question was where can I find such thing as a markov based text generating code for processing. The guy above you asked for a code, So I'm giving the one from the resources I found. Either you could help me by giving me another resource or put your analysis to good use for other, ex. me. Otherwise I see really no point what so ever for your comment. Please explain if I'm wrong. – Blckpstv Jan 13 '15 at 14:54
  • If you want "word based" Markov: tokenize the text and use (old_state X token_number) -->> new_state as the tranferfunction. (maybe add some weights or frequencies, too) – wildplasser Jan 14 '15 at 17:45
  • wildplasser could you contact me for more information, I'm dutch aswell. Maybe it's easier to understand then!? Would be a great help. – Blckpstv Jan 15 '15 at 12:00
  • https://twitter.com/Hubert_B_Both is powered by wakkerbot. – wildplasser Jan 15 '15 at 19:08
  • Nice wildplasser and JeffThompson thanks for the help!! – Blckpstv Jan 16 '15 at 16:54

2 Answers2

2

The RiTa library already does this if you want to take a look at it. Or just use it. http://rednoise.org/rita/

justincouch
  • 101
  • 3
0

I think that code you're using is making things more complicated. This Java example is much clearer, and should work "out of the box" in Processing – just copy/paste!

Here's the Processing-ified version that should work, though I think it might need some tweaking.

// required imports for Processing
import java.util.Hashtable;
import java.util.Vector;


String inputFile = "Sonnet51.txt";
Markov markovChain;


void setup() {
  markovChain = new Markov();

  // load text
  String[] input = loadStrings(inputFile);
  for (String line : input) {
    markovChain.addWords(line);
  }

  // generate a sentence!
  String sentence = markovChain.generateSentence();
  println(sentence);
}


class Markov {
  Hashtable<String, Vector<String>> markovChain = new Hashtable<String, Vector<String>>();

  Markov() {
    markovChain.put("_start", new Vector<String>());
    markovChain.put("_end", new Vector<String>());
  }

  void addWords(String line) {
    String[] words = line.split(" ");
    for (int i=0; i<words.length; i++) {
      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);
        }
      }
    }
  }

  String generateSentence() {
    String newPhrase= "";
    String nextWord = "";
    Vector<String> startWords = markovChain.get("_start");
    int startWordsLen = startWords.size();
    nextWord = startWords.get(int(random(startWordsLen)));
    newPhrase += " " + nextWord;
    while (nextWord.charAt (nextWord.length()-1) != '.') {
      Vector<String> wordSelection = markovChain.get(nextWord);
      int wordSelectionLen = wordSelection.size();
      nextWord = wordSelection.get(int(random(wordSelectionLen)));
      newPhrase += " " + nextWord;
    }
    return newPhrase.toString();
  }
} 
JeffThompson
  • 1,538
  • 3
  • 24
  • 47
  • public static Hashtable> markovChain = new Hashtable>() 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 – Blckpstv Jan 19 '15 at 01:11
  • Not sure where the `>` characters are coming from. You'll need to get rid of those. After that, you can just delete the `public static` part and declare the `Hashtable` like any other variable! – JeffThompson Jan 19 '15 at 13:43
  • 1
    int Hashtable{String, Vector{String}} markovChain = new Hashtable{String, Vector{String}}(); Like this? I do need to keep the Hashtable declaration, no? – Blckpstv Jan 19 '15 at 21:38
  • Ah, I think you had some weird formatting issues when you pasted your code. You'll need to import the following: `import java.util.Hashtable;` and `import java.util.Vector;`. And then: `Hashtable> markovChain = new Hashtable>();` – JeffThompson Jan 19 '15 at 23:06
  • Ah ok my fault, well that was the first thing I did is searching for those libraries to put them in the processing sketch. (copy/paste in the sketch normally does the trick) Though now I can't find those libraries. – Blckpstv Jan 20 '15 at 11:35
  • I've find this site for more information about those imports. http://www.dotnetperls.com/hashmap So if i get correct now the import isn't resembling a file that has to be located in the map of my sketch? Is this correct. – Blckpstv Jan 20 '15 at 16:09
  • I think that looks like a good intro to hashmaps, but this is pretty outside the scope of your original question. If it my answer was enough for you to get started, I'd suggest accepting the answer here and posting for help on the [Processing forum](http://forum.processing.org/two/). – JeffThompson Jan 21 '15 at 02:09
  • I've already voted your answer as accepted. But because the libraries aren't in that out of the box solution. I'm still stuck. And I've already posted my question on the forums of processing. – Blckpstv Jan 21 '15 at 10:22
  • I get this error ArrayIndexOutOfBoundsException 1 on line 41. From what I understand it has something to do with i+1 one being limited or range. (found an old post on the processing forum about it, so that what I get from it is that the variable can't store more? or read more?) – Blckpstv Jan 22 '15 at 17:19