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
有帮助吗?

解决方案

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.)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top