Complete the code to create bigrams from a list of words.
bigrams = [(words[i], words[i+[1]]) for i in range(len(words)-1)]
The bigram pairs each word with the next word, so the index offset is 1.
Complete the code to count the frequency of each trigram in the text.
from collections import Counter trigrams = [(words[i], words[i+1], words[i+[1]]) for i in range(len(words)-2)] trigram_counts = Counter(trigrams)
For trigrams, the third word is two positions ahead of the first word, so the offset is 2.
Fix the error in the code that calculates the probability of a bigram using counts.
bigram_prob = bigram_counts[bigram] / [1]The probability of a bigram is its count divided by the total count of all bigrams.
Fill both blanks to create a dictionary of bigram probabilities from counts.
bigram_probs = {bg: bigram_counts[bg] / [1] for bg in [2]We divide each bigram count by the total count of all bigrams (sum of values) and iterate over all bigram keys.
Fill the two blanks to build a conditional probability dictionary for bigrams.
cond_probs = {}
for (w1, w2), count in bigram_counts.items():
w1_total = sum(c for (word1, _), c in bigram_counts.items() if word1 == [1])
cond_probs.setdefault(w1, {})[[2]] = count / w1_totalThe conditional probability P(w2|w1) is count divided by total counts of bigrams starting with w1. We use w2 as the key in the nested dict, divide by w1_total, and sum counts where word1 equals w1.