InferenceAI

Enter at least two characters to search.

Start free

Generate N-grams

easyNLP12 min10 XP

Return contiguous n-grams as lists. Return [] when n is invalid or too large.

Examples

EXAMPLE WALKTHROUGHbigrams

A guided view of the published example, not a trace of your code.

INPUT[["a","b","c"],2]
TASKngrams()
EXPECTED OUTPUT[["a","b"],["b","c"]]

Start with the input values shown in this example.

Example 1 of 2

Input: tokens = ["a","b","c"], n = 2

Output: [["a","b"],["b","c"]]

Explanation: bigrams

Input: tokens = [1], n = 2

Output: []

Explanation: too large

Hint 1

There are len(tokens)-n+1 valid starts.

Topics

PythonNLP