Loading...
Generate word-level n-grams from a given text.
An n-gram is a contiguous sequence of n items from a given text. For word-level n-grams, the items are words.
Input format:
Output: A list of tuples, where each tuple contains n consecutive words.
Example:
Note: Do NOT lowercase the words — preserve original casing.
I love natural language processing 2
[('I', 'love'), ('love', 'natural'), ('natural', 'language'), ('language', 'processing')]Step 1: Split text into words ["I", "love", "natural", "language", "processing"] — 5 words total
Step 2: Generate bigrams (n=2) We slide a window of size 2 across the word list:
Total bigrams = 5 - 2 + 1 = 4