Definition
N-gram
Let
be a sequence of tokens and let . An n-gram is a contiguous subsequence of exactly tokens. Its occurrences in are The corresponding collection is
The parameter
fixes the length of the context: gives a unigram, a bigram, and a trigram. The items may be words, characters, or subword tokens, depending on the chosen tokenisation. Because the subsequence is contiguous, order and adjacency are preserved; repeated n-grams remain distinct occurrences in the collection.
Examples
Bigrams
For the token sequence
, The pair
is not a bigram because the tokens are not adjacent.
Unigrams and trigrams
For
, the unigrams are the individual tokens: The trigrams contain all three tokens because the sequence has length three:
Repeated occurrences
For
, The bigram
occurs twice. Its two occurrences have different positions even though their token values are equal.
Character n-grams
N-grams need not contain words. For the character sequence
, A character n-gram model therefore represents text through short character sequences rather than word sequences.