SECTION 10 / 13

Two passes when the second decision needs final counts

3 min read
On this page 5 headings

Combining work into one pass is useful when it is correct. Some decisions need information that is not available until the entire first pass has finished.

A small example explains why frequency-based filtering needs final counts before the second pass.
A small example explains why frequency-based filtering needs final counts before the second pass.
YOUR COURSE DATA · TRY IT HERE

Count each word, ignoring case and end punctuation

Use the supplied 65 word cards. No download or coding is needed.

READY TO START

A pile of 65 cards

Start with the first card. Read its fields, do one small action, then move on.

What we remember

Read
0

Nothing has been read yet. Choose Next card to begin.

Look through all 65 original records
ItPronoun · 2 letters
SerialNumber
0
Word
It
PartOfSpeech
Pronoun
LetterCount
2
wasVerb · 3 letters
SerialNumber
1
Word
was
PartOfSpeech
Verb
LetterCount
3
MondayNoun · 6 letters
SerialNumber
2
Word
Monday
PartOfSpeech
Noun
LetterCount
6
morning.Noun · 7 letters
SerialNumber
3
Word
morning.
PartOfSpeech
Noun
LetterCount
7
SwaminathanNoun · 11 letters
SerialNumber
4
Word
Swaminathan
PartOfSpeech
Noun
LetterCount
11
wasVerb · 3 letters
SerialNumber
5
Word
was
PartOfSpeech
Verb
LetterCount
3
reluctantAdjective · 9 letters
SerialNumber
6
Word
reluctant
PartOfSpeech
Adjective
LetterCount
9
toPreposition · 2 letters
SerialNumber
7
Word
to
PartOfSpeech
Preposition
LetterCount
2
openVerb · 4 letters
SerialNumber
8
Word
open
PartOfSpeech
Verb
LetterCount
4
hisPronoun · 3 letters
SerialNumber
9
Word
his
PartOfSpeech
Pronoun
LetterCount
3
eyes.Noun · 4 letters
SerialNumber
10
Word
eyes.
PartOfSpeech
Noun
LetterCount
4
HePronoun · 2 letters
SerialNumber
11
Word
He
PartOfSpeech
Pronoun
LetterCount
2
consideredVerb · 10 letters
SerialNumber
12
Word
considered
PartOfSpeech
Verb
LetterCount
10
MondayNoun · 6 letters
SerialNumber
13
Word
Monday
PartOfSpeech
Noun
LetterCount
6
speciallyAdverb · 9 letters
SerialNumber
14
Word
specially
PartOfSpeech
Adverb
LetterCount
9
unpleasantAdjective · 10 letters
SerialNumber
15
Word
unpleasant
PartOfSpeech
Adjective
LetterCount
10
inPreposition · 2 letters
SerialNumber
16
Word
in
PartOfSpeech
Preposition
LetterCount
2
theArticle · 3 letters
SerialNumber
17
Word
the
PartOfSpeech
Article
LetterCount
3
calendar.Noun · 8 letters
SerialNumber
18
Word
calendar.
PartOfSpeech
Noun
LetterCount
8
AfterPreposition · 5 letters
SerialNumber
19
Word
After
PartOfSpeech
Preposition
LetterCount
5
theArticle · 3 letters
SerialNumber
20
Word
the
PartOfSpeech
Article
LetterCount
3
deliciousAdjective · 9 letters
SerialNumber
21
Word
delicious
PartOfSpeech
Adjective
LetterCount
9
freedomNoun · 7 letters
SerialNumber
22
Word
freedom
PartOfSpeech
Noun
LetterCount
7
ofPreposition · 2 letters
SerialNumber
23
Word
of
PartOfSpeech
Preposition
LetterCount
2
SaturdayNoun · 8 letters
SerialNumber
24
Word
Saturday
PartOfSpeech
Noun
LetterCount
8
AndConjunction · 3 letters
SerialNumber
25
Word
And
PartOfSpeech
Conjunction
LetterCount
3
Sunday,Noun · 6 letters
SerialNumber
26
Word
Sunday,
PartOfSpeech
Noun
LetterCount
6
itPronoun · 2 letters
SerialNumber
27
Word
it
PartOfSpeech
Pronoun
LetterCount
2
wasVerb · 3 letters
SerialNumber
28
Word
was
PartOfSpeech
Verb
LetterCount
3
difficultAdjective · 9 letters
SerialNumber
29
Word
difficult
PartOfSpeech
Adjective
LetterCount
9
toPreposition · 2 letters
SerialNumber
30
Word
to
PartOfSpeech
Preposition
LetterCount
2
getVerb · 3 letters
SerialNumber
31
Word
get
PartOfSpeech
Verb
LetterCount
3
intoPreposition · 4 letters
SerialNumber
32
Word
into
PartOfSpeech
Preposition
LetterCount
4
theArticle · 3 letters
SerialNumber
33
Word
the
PartOfSpeech
Article
LetterCount
3
MondayNoun · 6 letters
SerialNumber
34
Word
Monday
PartOfSpeech
Noun
LetterCount
6
moodNoun · 4 letters
SerialNumber
35
Word
mood
PartOfSpeech
Noun
LetterCount
4
ofPreposition · 2 letters
SerialNumber
36
Word
of
PartOfSpeech
Preposition
LetterCount
2
workNoun · 4 letters
SerialNumber
37
Word
work
PartOfSpeech
Noun
LetterCount
4
andConjunction · 3 letters
SerialNumber
38
Word
and
PartOfSpeech
Conjunction
LetterCount
3
discipline.Noun · 10 letters
SerialNumber
39
Word
discipline.
PartOfSpeech
Noun
LetterCount
10
HePronoun · 2 letters
SerialNumber
40
Word
He
PartOfSpeech
Pronoun
LetterCount
2
shudderedVerb · 9 letters
SerialNumber
41
Word
shuddered
PartOfSpeech
Verb
LetterCount
9
atPreposition · 2 letters
SerialNumber
42
Word
at
PartOfSpeech
Preposition
LetterCount
2
theArticle · 3 letters
SerialNumber
43
Word
the
PartOfSpeech
Article
LetterCount
3
veryAdverb · 4 letters
SerialNumber
44
Word
very
PartOfSpeech
Adverb
LetterCount
4
thoughtNoun · 7 letters
SerialNumber
45
Word
thought
PartOfSpeech
Noun
LetterCount
7
ofPreposition · 2 letters
SerialNumber
46
Word
of
PartOfSpeech
Preposition
LetterCount
2
school:Noun · 6 letters
SerialNumber
47
Word
school:
PartOfSpeech
Noun
LetterCount
6
theArticle · 3 letters
SerialNumber
48
Word
the
PartOfSpeech
Article
LetterCount
3
dismalAdjective · 6 letters
SerialNumber
49
Word
dismal
PartOfSpeech
Adjective
LetterCount
6
yellowAdjective · 6 letters
SerialNumber
50
Word
yellow
PartOfSpeech
Adjective
LetterCount
6
building;Noun · 8 letters
SerialNumber
51
Word
building;
PartOfSpeech
Noun
LetterCount
8
theArticle · 3 letters
SerialNumber
52
Word
the
PartOfSpeech
Article
LetterCount
3
fire-eyedAdjective · 8 letters
SerialNumber
53
Word
fire-eyed
PartOfSpeech
Adjective
LetterCount
8
Vedanayagam,Noun · 11 letters
SerialNumber
54
Word
Vedanayagam,
PartOfSpeech
Noun
LetterCount
11
hisPronoun · 3 letters
SerialNumber
55
Word
his
PartOfSpeech
Pronoun
LetterCount
3
classNoun · 5 letters
SerialNumber
56
Word
class
PartOfSpeech
Noun
LetterCount
5
teacher,Noun · 7 letters
SerialNumber
57
Word
teacher,
PartOfSpeech
Noun
LetterCount
7
andConjunction · 3 letters
SerialNumber
58
Word
and
PartOfSpeech
Conjunction
LetterCount
3
headmasterNoun · 10 letters
SerialNumber
59
Word
headmaster
PartOfSpeech
Noun
LetterCount
10
withPreposition · 4 letters
SerialNumber
60
Word
with
PartOfSpeech
Preposition
LetterCount
4
hisPronoun · 3 letters
SerialNumber
61
Word
his
PartOfSpeech
Pronoun
LetterCount
3
thinAdjective · 4 letters
SerialNumber
62
Word
thin
PartOfSpeech
Adjective
LetterCount
4
longAdjective · 4 letters
SerialNumber
63
Word
long
PartOfSpeech
Adjective
LetterCount
4
cane...Noun · 4 letters
SerialNumber
64
Word
cane...
PartOfSpeech
Noun
LetterCount
4
YOUR COURSE DATA · TRY IT HERE

Keep words that occur exactly once

Use the supplied 65 word cards. No download or coding is needed.

Pass 1 counts all 65 words first: 47 different word keys. Below, pass 2 uses those final counts to decide which cards to keep.

READY TO START

A pile of 65 cards

Start with the first card. Read its fields, do one small action, then move on.

What we remember

Read
0

Nothing has been read yet. Choose Next card to begin.

Look through all 65 original records
ItPronoun · 2 letters
SerialNumber
0
Word
It
PartOfSpeech
Pronoun
LetterCount
2
wasVerb · 3 letters
SerialNumber
1
Word
was
PartOfSpeech
Verb
LetterCount
3
MondayNoun · 6 letters
SerialNumber
2
Word
Monday
PartOfSpeech
Noun
LetterCount
6
morning.Noun · 7 letters
SerialNumber
3
Word
morning.
PartOfSpeech
Noun
LetterCount
7
SwaminathanNoun · 11 letters
SerialNumber
4
Word
Swaminathan
PartOfSpeech
Noun
LetterCount
11
wasVerb · 3 letters
SerialNumber
5
Word
was
PartOfSpeech
Verb
LetterCount
3
reluctantAdjective · 9 letters
SerialNumber
6
Word
reluctant
PartOfSpeech
Adjective
LetterCount
9
toPreposition · 2 letters
SerialNumber
7
Word
to
PartOfSpeech
Preposition
LetterCount
2
openVerb · 4 letters
SerialNumber
8
Word
open
PartOfSpeech
Verb
LetterCount
4
hisPronoun · 3 letters
SerialNumber
9
Word
his
PartOfSpeech
Pronoun
LetterCount
3
eyes.Noun · 4 letters
SerialNumber
10
Word
eyes.
PartOfSpeech
Noun
LetterCount
4
HePronoun · 2 letters
SerialNumber
11
Word
He
PartOfSpeech
Pronoun
LetterCount
2
consideredVerb · 10 letters
SerialNumber
12
Word
considered
PartOfSpeech
Verb
LetterCount
10
MondayNoun · 6 letters
SerialNumber
13
Word
Monday
PartOfSpeech
Noun
LetterCount
6
speciallyAdverb · 9 letters
SerialNumber
14
Word
specially
PartOfSpeech
Adverb
LetterCount
9
unpleasantAdjective · 10 letters
SerialNumber
15
Word
unpleasant
PartOfSpeech
Adjective
LetterCount
10
inPreposition · 2 letters
SerialNumber
16
Word
in
PartOfSpeech
Preposition
LetterCount
2
theArticle · 3 letters
SerialNumber
17
Word
the
PartOfSpeech
Article
LetterCount
3
calendar.Noun · 8 letters
SerialNumber
18
Word
calendar.
PartOfSpeech
Noun
LetterCount
8
AfterPreposition · 5 letters
SerialNumber
19
Word
After
PartOfSpeech
Preposition
LetterCount
5
theArticle · 3 letters
SerialNumber
20
Word
the
PartOfSpeech
Article
LetterCount
3
deliciousAdjective · 9 letters
SerialNumber
21
Word
delicious
PartOfSpeech
Adjective
LetterCount
9
freedomNoun · 7 letters
SerialNumber
22
Word
freedom
PartOfSpeech
Noun
LetterCount
7
ofPreposition · 2 letters
SerialNumber
23
Word
of
PartOfSpeech
Preposition
LetterCount
2
SaturdayNoun · 8 letters
SerialNumber
24
Word
Saturday
PartOfSpeech
Noun
LetterCount
8
AndConjunction · 3 letters
SerialNumber
25
Word
And
PartOfSpeech
Conjunction
LetterCount
3
Sunday,Noun · 6 letters
SerialNumber
26
Word
Sunday,
PartOfSpeech
Noun
LetterCount
6
itPronoun · 2 letters
SerialNumber
27
Word
it
PartOfSpeech
Pronoun
LetterCount
2
wasVerb · 3 letters
SerialNumber
28
Word
was
PartOfSpeech
Verb
LetterCount
3
difficultAdjective · 9 letters
SerialNumber
29
Word
difficult
PartOfSpeech
Adjective
LetterCount
9
toPreposition · 2 letters
SerialNumber
30
Word
to
PartOfSpeech
Preposition
LetterCount
2
getVerb · 3 letters
SerialNumber
31
Word
get
PartOfSpeech
Verb
LetterCount
3
intoPreposition · 4 letters
SerialNumber
32
Word
into
PartOfSpeech
Preposition
LetterCount
4
theArticle · 3 letters
SerialNumber
33
Word
the
PartOfSpeech
Article
LetterCount
3
MondayNoun · 6 letters
SerialNumber
34
Word
Monday
PartOfSpeech
Noun
LetterCount
6
moodNoun · 4 letters
SerialNumber
35
Word
mood
PartOfSpeech
Noun
LetterCount
4
ofPreposition · 2 letters
SerialNumber
36
Word
of
PartOfSpeech
Preposition
LetterCount
2
workNoun · 4 letters
SerialNumber
37
Word
work
PartOfSpeech
Noun
LetterCount
4
andConjunction · 3 letters
SerialNumber
38
Word
and
PartOfSpeech
Conjunction
LetterCount
3
discipline.Noun · 10 letters
SerialNumber
39
Word
discipline.
PartOfSpeech
Noun
LetterCount
10
HePronoun · 2 letters
SerialNumber
40
Word
He
PartOfSpeech
Pronoun
LetterCount
2
shudderedVerb · 9 letters
SerialNumber
41
Word
shuddered
PartOfSpeech
Verb
LetterCount
9
atPreposition · 2 letters
SerialNumber
42
Word
at
PartOfSpeech
Preposition
LetterCount
2
theArticle · 3 letters
SerialNumber
43
Word
the
PartOfSpeech
Article
LetterCount
3
veryAdverb · 4 letters
SerialNumber
44
Word
very
PartOfSpeech
Adverb
LetterCount
4
thoughtNoun · 7 letters
SerialNumber
45
Word
thought
PartOfSpeech
Noun
LetterCount
7
ofPreposition · 2 letters
SerialNumber
46
Word
of
PartOfSpeech
Preposition
LetterCount
2
school:Noun · 6 letters
SerialNumber
47
Word
school:
PartOfSpeech
Noun
LetterCount
6
theArticle · 3 letters
SerialNumber
48
Word
the
PartOfSpeech
Article
LetterCount
3
dismalAdjective · 6 letters
SerialNumber
49
Word
dismal
PartOfSpeech
Adjective
LetterCount
6
yellowAdjective · 6 letters
SerialNumber
50
Word
yellow
PartOfSpeech
Adjective
LetterCount
6
building;Noun · 8 letters
SerialNumber
51
Word
building;
PartOfSpeech
Noun
LetterCount
8
theArticle · 3 letters
SerialNumber
52
Word
the
PartOfSpeech
Article
LetterCount
3
fire-eyedAdjective · 8 letters
SerialNumber
53
Word
fire-eyed
PartOfSpeech
Adjective
LetterCount
8
Vedanayagam,Noun · 11 letters
SerialNumber
54
Word
Vedanayagam,
PartOfSpeech
Noun
LetterCount
11
hisPronoun · 3 letters
SerialNumber
55
Word
his
PartOfSpeech
Pronoun
LetterCount
3
classNoun · 5 letters
SerialNumber
56
Word
class
PartOfSpeech
Noun
LetterCount
5
teacher,Noun · 7 letters
SerialNumber
57
Word
teacher,
PartOfSpeech
Noun
LetterCount
7
andConjunction · 3 letters
SerialNumber
58
Word
and
PartOfSpeech
Conjunction
LetterCount
3
headmasterNoun · 10 letters
SerialNumber
59
Word
headmaster
PartOfSpeech
Noun
LetterCount
10
withPreposition · 4 letters
SerialNumber
60
Word
with
PartOfSpeech
Preposition
LetterCount
4
hisPronoun · 3 letters
SerialNumber
61
Word
his
PartOfSpeech
Pronoun
LetterCount
3
thinAdjective · 4 letters
SerialNumber
62
Word
thin
PartOfSpeech
Adjective
LetterCount
4
longAdjective · 4 letters
SerialNumber
63
Word
long
PartOfSpeech
Adjective
LetterCount
4
cane...Noun · 4 letters
SerialNumber
64
Word
cane...
PartOfSpeech
Noun
LetterCount
4
Complete the frequency table first, then use those final frequencies to select words.
Complete the frequency table first, then use those final frequencies to select words.

A word seen once so far may repeat later

Imagine sun, rain, sun. When you first see sun, its current frequency is 1. If you immediately keep it as a final “unique word”, the later sun invalidates that decision. The final frequencies are sun=2 and rain=1.

Pass 1:
    Build Frequency for every normalized word
Pass 2:
    For every original word card X {
        w = Normalize(X.Word)
        if (Frequency[w] == 1) { keep X }
        else { discard X }
    }

“Discard” here means move into the non-selected output of the exercise, not delete the original dataset. Retain the original records and use their IDs when you need to reconstruct order.

Use identical keys in both passes

If the first pass combines It with it but the second pass looks for the raw capitalized text, filtering can fail. Normalize case and surrounding punctuation in exactly the same way. Do not remove part of an internal hyphenated token in only one pass.

Dataset result and threshold choices

With the supplied Words table and our rule, 38 normalized keys have final count 1. Each occurs once, so 38 cards are selected. The remaining 27 occurrence cards belong to nine repeated keys. You could instead keep words with frequency at most 2; define that threshold before filtering.

Why word frequencies are useful

The lecture uses search and document comparison as a motivation: common words often tell us less about a document’s specific topic than distinctive words. This is an introductory model for why frequency matters, not a complete description of a modern search engine or a rule that frequent words should always be removed.

Sequential is not nested

Two sequential passes finish one full scan, then start another. Nested full scans would restart a second complete scan for each outer record. In this task, it is the dependence on completed global counts that explains the second pass.

Try it yourself

For a,b,a,c, which occurrence cards survive a final-frequency-equals-1 filter?

Show explained answer

The b and c cards. Both a occurrences are excluded because a has final frequency 2.

Try it yourself

Why cannot the first a be accepted permanently as unique when first seen?

Show explained answer

A later occurrence may change its final count. Current frequency 1 does not prove final frequency 1.

Try it yourself

Do the two passes need the same normalization?

Show explained answer

Yes. Both must look up exactly the same category keys.

Try it yourself

How many once-occurring cards are kept in the supplied normalized Words table?

Show explained answer

38, under the explicitly stated lowercase-and-surrounding-punctuation rule.

Source: Lectures 2.6 and 2.7. Small teaching datasets and diagrams are prepared for these notes; lecture and tutorial totals are identified explicitly.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑