Learn how bigrams and trigrams count adjacent tokens in the Riverton sentences and why most of them appear only once.
Repeated words such as training stand out in the Riverton Workforce Lab’s short job-board lines and flyer headings. The harder question is whether neighbouring word pairs tell a clearer story.
The coordinator tries a simple phrase count before trusting it. If most phrases appear once, the list may describe these 28 sentences closely while helping less with new documents.
Note
The Riverton Workforce Lab, its job board, and its training flyer are fictional and were created for teaching.
TipWhat you will learn
This lesson shows how to:
define an n-gram as a run of adjacent tokens;
build bigrams and trigrams with tidytext;
count repeated n-grams in the Riverton sentences;
measure how many n-grams appear once; and
explain why phrase vocabularies can generalise badly from small data.
Load the sentences
This setup chunk reads the sentence file, prepares small tables, builds n-grams with tidytext, and draws one summary chart. A tibble is a table that prints its size and column types.
The file is small enough to inspect by eye. That convenience is also the reason its phrase counts should be treated as local examples.
Build adjacent-word phrases
An n-gram is a run of n adjacent tokens. A unigram has one token, a bigram has two, and a trigram has three. These counts use the same token rule as Lesson 15: lowercase the text and drop punctuation. Because each row here is one sentence, no n-gram spans two sentences; a file with whole documents in rows would need a different check.
The most frequent bigrams and trigrams in the 28 sentences
N-gram type
N-gram
Frequency
bigram
is required
4
bigram
training is
3
bigram
are required
2
bigram
experience is
2
bigram
is preferred
2
bigram
is provided
2
bigram
no prior
2
bigram
shifts are
2
trigram
training is provided
2
trigram
12 week training
1
trigram
a daytime schedule
1
trigram
a high school
1
trigram
a medical records
1
trigram
a paid apprenticeship
1
trigram
a portfolio is
1
trigram
a valid driver’s
1
The sentence Evening classes has only two tokens, so it has no trigram. The code drops that empty result before counting. The most frequent bigram is is required, with 4 appearances, and the most frequent trigram is training is provided, with 2.
Measure the sparsity
Sparsity means that many possible items have no examples or only one example. The general reason is arithmetic, not this particular table. Each new word adds at most one new word type, but it can also add a new pair and a new triple; the set of pairs a vocabulary could form grows faster than the vocabulary itself. The Riverton counts below illustrate that argument rather than prove it.
Distinct n-grams and one-time n-grams in the Riverton sentences
n
Unit
Distinct units
Seen once
Seen-once fraction
1
unigrams
96
72
72/96
2
bigrams
112
102
102/112
3
trigrams
96
95
95/96
The 28 sentences contain 96 distinct unigrams, 112 distinct bigrams, and 96 distinct trigrams. The seen-once fractions are 72/96 for unigrams, 102/112 for bigrams, and 95/96 for trigrams. These numbers describe only this teaching corpus.
Figure 1: Share of distinct n-grams seen once in the 28 Riverton sentences.
The chart focuses on the quantity the table asks the reader to compare: how much of each list appears only once.
What to remember
An n-gram counts adjacent tokens after a chosen token rule.
The Riverton rows contain 112 distinct bigrams and 96 distinct trigrams.
A two-token sentence contributes no trigram.
Counts from 28 sentences illustrate sparsity; they do not estimate phrase use in workforce writing.
Report the repeated phrases as local clues, then stop. The useful result here is a small list of repeats paired with a warning about sparse phrase counts.