I want to generate sonnets using nltk with bigrams. I have generated bigrams and computed probability of each bigram and stored in default dict like that.
[('"Let', defaultdict(<function <lambda>.<locals>.<lambda> at0x1a17f98bf8>,
{'the': 0.2857142857142857, 'dainty':
0.14285714285714285, 'it': 0.14285714285714285, 'those':
0.14285714285714285, 'me': 0.14285714285714285, 'us':
0.14285714285714285}))]
Probability of each word appearing after let is given. Like that I have bigram model for my corpus. Now I want to generate 4 lines sonnet with 15 words in each line. I have tried this code but it is not working.
def generate_sonnet(word):
lines = 4
words= 15
for i in range(lines):
line = ()
for j in range(words):
#I am selecting max probability but not that word. How I can select that word which has max probability of occurring with word?
nword = float(max(model[word].values()))
word += nword
word = random.choice(poetrylist)
generate_sonnet(word)
I select a random word and pass it to my function. where I want to join 15 words using bigrams and when 1 line completes the next 3 should be done.