📘
Next Word Predictor
Given a corpus and a context word, predict the most likely next word using bigram counts.
If there are ties, return the word that comes first alphabetically.
Input format:
- Line 1: The training corpus (lowercase)
- Line 2: The context word
Output: The predicted next word and its probability (rounded to 4 decimal places), separated by a space.
Example:
Input:
i like cats i like dogs i hate rain i
Output:
like 0.6667
Reasoning:
Step 1: Find all bigrams starting with "i" i→like (2 times), i→hate (1 time)
Step 2: Find most frequent "like" has count 2, "hate" has count 1 Most frequent: "like"
Step 3: Calculate probability P(like | i) = 2/3 = 0.6667
Constraints:
- All text is lowercase
- Return most frequent next word after the context word
- Ties broken alphabetically
- Output: "word probability"
- If context word never appears, output "N/A 0.0"
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.