TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Bigram Probabilities (Add-1 Smoothing)

NLP
Hard

Build a bigram language model with add-one smoothing. Sort the unique tokens to obtain vocabulary vocab. Row iii represents context token vocab[i], and column jjj represents next token vocab[j].

P(wj∣wi)=Cij+1∑u=1VCiu+VP(w_j\mid w_i)=\frac{C_{ij}+1}{\sum_{u=1}^{V}C_{iu}+V}P(wj​∣wi​)=∑u=1V​Ciu​+VCij​+1​

Here, CijC_{ij}Cij​ is the number of adjacent occurrences of wiw_iwi​ followed by wjw_jwj​, and VVV is vocabulary size. Return vocab as a list, counts as an integer NumPy matrix, and probabilities as a floating-point NumPy matrix in a dictionary.

Loading visualization...

Examples

Input: tokens = ["a", "b", "a"]

Output: {"vocab": ["a", "b"], "counts": [[0, 1], [1, 0]], "probabilities": [[0.333333, 0.666667], [0.666667, 0.333333]]}

Explanation: The observed transitions are a to b and b to a; add-one smoothing also assigns probability to unseen pairs.

Input: tokens = ["i", "love", "ml", "love", "ml"]

Output: {"vocab": ["i", "love", "ml"], "counts": [[0, 1, 0], [0, 0, 2], [0, 1, 0]], "probabilities": [[0.25, 0.5, 0.25], [0.2, 0.2, 0.6], [0.25, 0.5, 0.25]]}

Hint 1

Create an index dictionary from sorted(set(tokens)).

Hint 2

Increment counts[index[first], index[second]] for adjacent pairs.

Hint 3

Divide counts + 1 by (counts.sum(axis=1, keepdims=True) + vocab_size).

Requirements

  • Sort the unique corpus tokens to define row and column order
  • Count every adjacent token pair in the count matrix
  • Apply add-one smoothing independently to each context row
  • Return exactly vocab, counts, and probabilities in a dictionary

Constraints

  • tokens contains at least one string
  • Use Python and NumPy only
Try Similar Problems
Perplexity ComputationWord Count DictBag Of WordsTfidf VectorizerBm25

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Bigram Probabilities (Add-1 Smoothing)

NLP
Hard

Build a bigram language model with add-one smoothing. Sort the unique tokens to obtain vocabulary vocab. Row iii represents context token vocab[i], and column jjj represents next token vocab[j].

P(wj∣wi)=Cij+1∑u=1VCiu+VP(w_j\mid w_i)=\frac{C_{ij}+1}{\sum_{u=1}^{V}C_{iu}+V}P(wj​∣wi​)=∑u=1V​Ciu​+VCij​+1​

Here, CijC_{ij}Cij​ is the number of adjacent occurrences of wiw_iwi​ followed by wjw_jwj​, and VVV is vocabulary size. Return vocab as a list, counts as an integer NumPy matrix, and probabilities as a floating-point NumPy matrix in a dictionary.

Loading visualization...

Examples

Input: tokens = ["a", "b", "a"]

Output: {"vocab": ["a", "b"], "counts": [[0, 1], [1, 0]], "probabilities": [[0.333333, 0.666667], [0.666667, 0.333333]]}

Explanation: The observed transitions are a to b and b to a; add-one smoothing also assigns probability to unseen pairs.

Input: tokens = ["i", "love", "ml", "love", "ml"]

Output: {"vocab": ["i", "love", "ml"], "counts": [[0, 1, 0], [0, 0, 2], [0, 1, 0]], "probabilities": [[0.25, 0.5, 0.25], [0.2, 0.2, 0.6], [0.25, 0.5, 0.25]]}

Hint 1

Create an index dictionary from sorted(set(tokens)).

Hint 2

Increment counts[index[first], index[second]] for adjacent pairs.

Hint 3

Divide counts + 1 by (counts.sum(axis=1, keepdims=True) + vocab_size).

Requirements

  • Sort the unique corpus tokens to define row and column order
  • Count every adjacent token pair in the count matrix
  • Apply add-one smoothing independently to each context row
  • Return exactly vocab, counts, and probabilities in a dictionary

Constraints

  • tokens contains at least one string
  • Use Python and NumPy only
Try Similar Problems
Perplexity ComputationWord Count DictBag Of WordsTfidf VectorizerBm25

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.