猿问

马可夫链python进行处理

我做了一个基于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


萧十郎
浏览 163回答 1
1回答

呼唤远方

处理本质上是Java的包装,因此您将使用Java语法重新实现此代码。就像是:HashMap<String, int> wordCount;int createProbabilityHash(words):&nbsp; &nbsp; int numWords = words.size();&nbsp; &nbsp; wordCount = new HashMap<String, int>();&nbsp; &nbsp; for (int i = 0;i < numWords; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; if wordCount.containsKey(words[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wordCount.put(words[i], wordCount.get(words[i]) + 1);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wordCount.put(words[i], 1);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }或者,您可以使用pyprocessing并使用来自Python的所有Processing技巧。(我认为此方法还有更多功能,因为您的原始Python代码从不返回任何内容,也没有对wordCount进行任何操作,或者实际上不计算任何类型的哈希值。)
随时随地看视频慕课网APP

相关分类

Python
我要回答