word = prob [word] .generate()TypeError:'float'对象不可订阅

时间:2016-05-19 16:03:31

标签: python-2.7 random floating-point

谁能告诉我为什么会收到此错误?

概率被正确打印出来,但是当我尝试生成随机序列词时,我得到了这个错误:

import nltk
from nltk.tokenize import word_tokenize
from nltk import ngrams
from nltk import FreqDist

Textfile = "TrainingSetLab2.txt"
Unigrams ={}

for line in open(Textfile):
    line = line.rstrip()
    tokens = word_tokenize(line)
    fdist = FreqDist(Unigrams)
    for word in tokens:     
        if word in Unigrams:
            Unigrams[word] = Unigrams[word]+1


        else:
            Unigrams[word] = 1

    for word in Unigrams:
         n = fdist.N()



for k,v in Unigrams.items():
    prob = (v/n)

    print(k,v,prob)
print("Total word: ", n)
#761
word = "put"
for index in range(5):
    word = prob[word].generate()
    print (word)

0 个答案:

没有答案