Question

I made a python project based on markov chains to create sentences. Now I have to make the same thing but in processing. Here is the python code I need help with: def createProbabilityHash(words):

def createProbabilityHash(words):
    numWords = len(words)
    wordCount = {}
    for word in words:
        if wordCount.has_key(word):
            wordCount[word] += 1
        else:
            wordCount[word] = 1
Was it helpful?

Solution

Processing is essentially a wrapper around Java, so you would use Java syntax to reimplement this code. Something like:

HashMap<String, int> wordCount;

int createProbabilityHash(words):
    int numWords = words.size();
    wordCount = new HashMap<String, int>();
    for (int i = 0;i < numWords; ++i) {
        if wordCount.containsKey(words[i]) {
            wordCount.put(words[i], wordCount.get(words[i]) + 1);
        } else {
            wordCount.put(words[i], 1);
        }
    }

Or you can use pyprocessing and use all the Processing coolness from Python.

(I assume there is more to this method, since your original Python code never returns anything, nor actually does anything with wordCount, or actually computes any kind of hash value.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top