我做了一个基于markov链的python项目来创建句子。现在,我必须做同样的事情,但要进行处理。这是我需要帮助的python代码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
1 回答

呼唤远方
TA贡献1856条经验 获得超11个赞
处理本质上是Java的包装,因此您将使用Java语法重新实现此代码。就像是:
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);
}
}
或者,您可以使用pyprocessing并使用来自Python的所有Processing技巧。
(我认为此方法还有更多功能,因为您的原始Python代码从不返回任何内容,也没有对wordCount进行任何操作,或者实际上不计算任何类型的哈希值。)
添加回答
举报
0/150
提交
取消