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:
i like cats i like dogs i hate rain i
like 0.6667
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"
Background Knowledge
Language Modeling is a fundamental task in Natural Language Processing (NLP) that involves predicting the next word in a sequence of words. This task is crucial for various applications, such as text generation, language translation, and speech recognition. A key concept in language modeling is the use of n-grams, which are sequences of n items from a given text. In this problem, we will be working with bigrams, which are sequences of 2 items.
The probability of a word given its context is calculated using the conditional probability formula: P(w2​∣w1​)=P(w1​)P(w1​,w2​)​, where w1​ is the context word and w2​ is the word we want to predict. To calculate these probabilities, we need to count the occurrences of bigrams in the training corpus. This is where bigram counts come into play. By counting the occurrences of each bigram, we can estimate the probability of a word given its context.
In this problem, we will use the Maximum Likelihood Estimation (MLE) approach to predict the next word. The MLE approach involves finding the word that maximizes the probability P(w2​∣w1​). If there are ties, we will return the word that comes first alphabetically. This approach is simple and effective, but it has its limitations. For example, it does not take into account the context beyond the immediate previous word.
Algorithm/Approach
The general approach to solve this problem involves the following steps:
- Preprocess the training corpus to extract bigrams
- Calculate the bigram counts and probabilities
- Use the MLE approach to predict the next word given the context word
- Handle ties by returning the word that comes first alphabetically
This approach is a simple example of a language model, which is a probabilistic model that predicts the next word in a sequence of words.
Step-by-Step Strategy
To implement the solution, follow these steps:
- Preprocess the training corpus: Convert the corpus to lowercase and split it into individual words.
- Extract bigrams: Iterate through the list of words and extract bigrams.
- Calculate bigram counts: Count the occurrences of each bigram.
- Calculate probabilities: Calculate the probability of each word given the context word using the bigram counts.
- Predict the next word: Use the MLE approach to predict the next word given the context word.
- Handle ties: If there are ties, return the word that comes first alphabetically.
Common Pitfalls
Some common pitfalls to watch out for when implementing this solution include:
- Not handling out-of-vocabulary words
- Not handling ties correctly
- Not normalizing the probabilities correctly
- Not using the correct data structures to store the bigram counts and probabilities
Time & Space Complexity
The time complexity of this solution is O(n), where n is the length of the training corpus. This is because we need to iterate through the corpus to extract bigrams and calculate bigram counts. The space complexity is also O(n), as we need to store the bigram counts and probabilities in memory. However, the space complexity can be reduced by using a more efficient data structure, such as a hash table, to store the bigram counts and probabilities.