← 返回 xai 的题目列表Find the Most Common N-grams
类型:online_judge
Given a document string and a list of integers nlist, for each n in nlist, compute the most frequent n-gram in the document.
Implement a function find_most_common_ngrams(doc: str, nlist: List[int]) -> Dict[int, Tuple[str, int]] that takes a document string doc and a list of integers nlist, and returns a dictionary where the keys are the integers from nlist, and the values are a tuple containing the most frequent n-gram and its occurrence count.
Input:
A document string (contains only lower-case letters and spaces).
A list of integers (1 <= n <= length of the document).
Output:
A dictionary: For each n, return the most common n-gram and its count.
Example Test Cases:
Input:
"this is a test document for finding n-grams", [2, 3]
Output:
{2: ("is", 2), 3: ("gra", 1)}
Input:
"example text for testing n-gram calculation", [1, 5]
Output:
{1: ("e", 4), 5: ("ample", 1)}
Note: The most common n-gram is guaranteed to be unique.
Example
Input
"this is a test document for finding n-grams\n[2, 3]"