vendredi 7 février 2020

Why does my code never enter the if statement even if the argument is true?

I am trying to create sentances using bigrams from the brown corpus. However my code will not enter into the first if statement and I can not figure out why? The ifstatement in question is bolded

import nltk
import random
from nltk.corpus import brown
from nltk import ngrams

words = brown.words()
bigramVal = ngrams(words, 2)
unigramVal = ngrams(words, 1)
possibleNextWords = []
currentSentence = []
nextWord = []
sentenceCount = 0
highestProb = 0
# Get four random bigrams from corpus
randomWords = random.sample(list(unigramVal), 4)

while sentenceCount < 4:
    currentSentence.append(randomWords[sentenceCount])

    while len(currentSentence) < 12:
        # Find possible next words
        bigramVal = ngrams(words, 3)

#This is the if statement that does not work
        for i in bigramVal:
            if i[0] == currentSentence[len(currentSentence) - 1]:
                possibleNextWords.append(i[1])

        uniqueNextWord = set(possibleNextWords)

        for j in uniqueNextWord:
            if possibleNextWords.count(j) > highestProb:
                nextWord.append(j)
                highestProb = possibleNextWords.count(j)
        currentSentence.append(nextWord.pop())
        highestProb = 0
        possibleNextWords.clear()

    for k in currentSentence:
        print(k, end=" ")
    print("\n")
    currentSentence.clear()
    sentenceCount += 1
else:
    print("Done!")

Aucun commentaire:

Enregistrer un commentaire