📘
Bigram Probability Calculator
Given a corpus of text, compute bigram probabilities.
A bigram probability is P(word2 | word1) = count(word1, word2) / count(word1).
Input format:
- Line 1: The training corpus (single line of text, lowercase)
- Line 2: The bigram to query (two words separated by space)
Round the probability to 4 decimal places.
Output: The bigram probability as a float.
Example:
Input:
the cat sat on the mat the cat
Output:
0.5
Reasoning:
Step 1: Count bigrams starting with "the" "the cat" appears 1 time "the mat" appears 1 time Total: "the" appears 2 times as first word of a bigram
Step 2: Calculate probability P(cat | the) = count("the cat") / count("the") = 1 / 2 = 0.5
Constraints:
- All text is lowercase
- Bigram probability = count(bigram) / count(first word)
- Round to 4 decimal places
- If the first word never appears, output 0.0
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.