4

I have a program that I am trying to make a Markov text generator for. I plan on splitting some text up at a set interval and then storing that into a class. The problem that I don't know how to solve is how to handle naming the instances of the class I am going to make. I was planning on generating the instances in a for loop. The user will pass the method some amount of text (the length of which is not known beforehand). Pseudo-code below:

    create vector for sets and tail letter;
for (int c = 0; c < text.length; c++) {
    Check to make sure overflow doesnt happen;
    Create instance of set named c;
    store set and tailLetter into vector;
}

public class set {
    String characters;
    char tailLetter;
}

I'm sorry if that's not clear enough. I'm teaching myself Java and this is my first post here.

3
  • what do you mean by "storing text into a class"? Also, what do you mean by "the instance of the class you are going to make"? Are you planning to dynamically generate java code and compile it at runtime? I'd say that's probably a bit too involved for a Markov chain class.
    – Cheeso
    Apr 28, 2011 at 21:37
  • I will be splitting the text up into chunks based on a user entered integer. I need to store these chunks, the letter that follows the chunks, and a count of how often the chunks and letters occur in the text. Since I don't know the length of the text that is passed, I can't create names before-hand for each instance that might be created. I'm still working this out on paper right now, but I'm running into obstacles Apr 28, 2011 at 22:03
  • Ah, ok, well you've got two tasks then. First is how to design the data structures to hold all that information (chunks and frequencies and so on), and 2nd is how to fill that data structure given a seed text. You want some kind of "collection class" to hold the data download.oracle.com/javase/tutorial/collections/index.html; you won't need to dynamically generate names of classes or variables. To be capable in Java you must not only be familiar with the syntax (curlies, semicolons, etc), but also the pre-existing building-blocks, like the classes in Java's builtin class library.
    – Cheeso
    Apr 28, 2011 at 23:25

4 Answers 4

4

If you are learning Java, I'd suggest that you first focus on how to model the problem with Java's classes and methods.

A Markov Chain is a model or statistical elaboration of the seed text, right? Using it to model a text, it normally describes how often each word is followed by each other word. (normally you'd split the text on word boundaries). That feels like it needs a class; it might be called MarkovChain.

Within the MarkovChain class, you need something to hold each word that occurs in the text, and maps that word to the other words in the text, and the count of frequency of those other words.

Suppose the word is 'and'. In the text, 'and' is followed by "the" four times, and "then" 3 times. So you'd need some data structure to hold something like this:

 and --> 
        the (4)
        then (3) 

One way to do this is to use an ArrayList to hold all words, then a Map<T1,T2> that holds the relationship between words and the frequency of following words. In this case T1 is probably a string, and the T2 is probably an ArrayList of pairs - a string and the (integer) count for that string.

But wait, now you don't need the base ArrayList<> to store the words, because they are just the keys in the map.

...and so on. The next step would be to figure out how to populate that data structure. That's probably an internal (private) method that gets called when a caller instantiates the MarkovChain class with a seed text.

Probably you also want that MarkovChain class to expose another method, a public one, that callers invoke when they want to generate some random sequence from the chain, relying on probabilities based on the frequency counts.

...

This is just one way to think about the modelling of the problem.

Anyway I would focus on that modelling/design exercise, before writing code.

3

Can't you use a Map<String, Set> where the key is the generated name?

0

You can use an ArrayList to manage the instances. I like the Map idea better so you can dynamically set the names instead of trying to access instances by an index number.

0

I don't see the point of the names:

  • If they are just so that 'set' objects will have some distinct String for debugging, the default toString() implementation will give you that.

  • If you specifically need to do lookup of these 'set' objects, then a numeric identifier or a sequence number will work better.

If you explained the purpose of the names, and how you intend to use them, maybe we could give you better advice.

1
  • I intend to randomly call a chunk of text and randomly add one of the tail letters assigned to that chunk to a string. Then, I will read the end of this string, search the chunks for a match, and when I find a chunk, I will randomly assign another tail letter that is associated with the found chunk. Here is a website that probably can explain it much better than I can: link Apr 28, 2011 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.