Calculate Unigram Probability from Corpus

Easy
NLP

Implement a function that calculates the unigram probability of a given word in a corpus of sentences. Include start <s> and end </s> tokens in the calculation. The probability should be rounded to 4 decimal places.

Examples

Example 1:
Input: corpus = "<s> Jack I like </s> <s> Jack I do like </s>", word = "Jack"
Output: 0.1818
Explanation: The corpus has 11 total tokens. 'Jack' appears twice. So, probability = 2 / 11

Starter Code

def unigram_probability(corpus: str, word: str) -> float:
    # Your code here
    pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews