jeudi 19 mai 2016

word = prob[word].generate() TypeError: 'float' object is not subscriptable

can anyone tell me why i got this error? the probability was printed out correctly but when i try to generate random sequence words, i got this error..

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)




Aucun commentaire:

Enregistrer un commentaire