0

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
Nathan Griffiths
  • 12,277
  • 2
  • 34
  • 51

1 Answers1

3

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

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
  • thank you so much, yes I have the return of wordcount, but I think that part I can easly put on processing, but you help me soo much, thank you. – user2438683 Jun 10 '13 at 21:19