N-gram


An n-gram is a sequence of n adjacent symbols in a particular order. The symbols may be n adjacent letters, syllables, or rarely whole words found in a language dataset; or adjacent phonemes extracted from a speech-recording dataset, or adjacent base pairs extracted from a genome. They are collected from a text corpus or speech corpus.
If Latin numerical prefixes are used, then n-gram of size 1 is called a "unigram", size 2 a "bigram" etc. If, instead of the Latin ones, the English cardinal numbers are furtherly used, then they are called "four-gram", "five-gram", etc. Similarly, Greek numerical prefixes such as "monomer", "dimer", "trimer", "tetramer", "pentamer", etc., or English cardinal numbers, "one-mer", "two-mer", "three-mer", etc. are used in computational biology for polymers or oligomers of a known size, called k-mers. When the items are words, -grams may also be called shingles.
In the context of natural language processing, the use of n-grams allows bag-of-words models to capture information such as word order, which would not be possible in the traditional bag of words setting.

Examples

In 1951, Shannon discussed n-gram models of English. For example:
  • 3-gram character model : in no ist lat whey cratict froure birs grocid pondenome of demonstures of the retagin is regiactiona of cre
  • 2-gram word model : the head and in frontal attack on an english writer that the character of this point is therefore another method for the letters that the time of who ever told the problem for an unexpected
FieldUnitSample sequence1-gram sequence2-gram sequence3-gram sequence
Vernacular nameunigrambigramtrigram
Order of resulting Markov model012
Protein sequencingamino acid... Cys-Gly-Leu-Ser-Trp......, Cys, Gly, Leu, Ser, Trp,......, Cys-Gly, Gly-Leu, Leu-Ser, Ser-Trp,......, Cys-Gly-Leu, Gly-Leu-Ser, Leu-Ser-Trp,...
DNA sequencingbase pair...AGCTTCGA......, A, G, C, T, T, C, G, A,......, AG, GC, CT, TT, TC, CG, GA,......, AGC, GCT, CTT, TTC, TCG, CGA,...
Character n-gram language modelcharacter...to_be_or_not_to_be......, t, o, _, b, e, _, o, r, _, n, o, t, _, t, o, _, b, e,......, to, o_, _b, be, e_, _o, or, r_, _n, no, ot, t_, _t, to, o_, _b, be,......, to_, o_b, _be, be_, e_o, _or, or_, r_n, _no, not, ot_, t_t, _to, to_, o_b, _be,...
Word n-gram language modelword... to be or not to be......, to, be, or, not, to, be,......, to be, be or, or not, not to, to be,......, to be or, be or not, or not to, not to be,...

Figure 1 shows several example sequences and the corresponding 1-gram, 2-gram and 3-gram sequences.
Here are further examples; these are word-level 3-grams and 4-grams from the Google n-gram corpus.
3-grams
  • ceramics collectables collectibles
  • ceramics collectables fine
  • ceramics collected by
  • ceramics collectible pottery
  • ceramics collectibles cooking
4-grams
  • serve as the incoming
  • serve as the incubator
  • serve as the independent
  • serve as the index
  • serve as the indication
  • serve as the indicator