本文整理汇总了Python中nltk.probability.FreqDist.sorted_samples方法的典型用法代码示例。如果您正苦于以下问题:Python FreqDist.sorted_samples方法的具体用法?Python FreqDist.sorted_samples怎么用?Python FreqDist.sorted_samples使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.probability.FreqDist
的用法示例。
在下文中一共展示了FreqDist.sorted_samples方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: next
# 需要导入模块: from nltk.probability import FreqDist [as 别名]
# 或者: from nltk.probability.FreqDist import sorted_samples [as 别名]
def next(self, s, method = MOST_LIKELY):
# Pick a transition leaving state s and return a state that would
# likely follow. The next state is chosen according to the method
# specified. The default is to choose and return the most likely
# transition state.
# determine all states adjacent to s
transitions = self._adjacentVertices[s]
freqDist = FreqDist()
# determine the weights of the edges between state s and all adjacent states
for state in transitions:
freqDist.inc(state)
if method == MarkovChain.MOST_LIKELY:
return freqDist.max()
elif method == MarkovChain.LEAST_LIKELY:
# NLTK provides no built-in method to return the minimum of a
# frequency distribution so for now, we get a list of samples
# sorted in decreasing order and grab the last one.
return freqDist.sorted_samples()[-1]
else:
# choose a real number between 0 and 1
x = uniform(0,1)
# choose next state based on weights of the edges. Randomness plays a part here.
for i in range(len(transitions)):
probability = freqDist.freq(transitions[i])
if x < probability:
return transitions[i]
x = x - probability
exc = "Error in MarkovChain.next(). Did not find next state.\n"
raise exc