SMALLTALK LAB
Jaccard Similarity in Squeak: Compare Two Sets
Compare recipe tags by dividing shared tags by all distinct tags. Build an exact Smalltalk score and give empty sets an explicit policy.
Before you begin
Open a Squeak Workspace. Use Print it on the complete checkpoint, including its temporary variable declarations.
These are native Squeak 6.1 examples. Each checkpoint is independent: open the download as text, copy one complete expression into Workspace and choose Print it. Do not file in the checkpoint collection as a class definition.
Expected values are Smalltalk expressions. The printer may show explicit Symbol markers: #(A B) and #(#A #B) represent the same array of Symbols.
01 - Count each tag once
A set represents presence, not frequency. Converting a tag list to a set collapses repeated tags.
(#(tea tea jam) asSet) sizeExpected: 2

Try it: Try adding one more tea; the size does not change.
02 - Find only shared tags
Select an item from the first set only when the second set contains it. Sort the returned array for a stable printed order.
| a b both either |
a := #(tea toast jam) asSet.
b := #(tea eggs jam) asSet.
both := a select: [:item | b includes: item].
either := a copy.
either addAll: b.
(both asArray sorted)Expected: #(jam tea)

Try it: Replace eggs with toast in the second list. The intersection grows by one.
03 - Count the union
Only the copy receives addAll:. The original sets both retain three members; the union has four, not six.
| a b both either |
a := #(tea toast jam) asSet.
b := #(tea eggs jam) asSet.
both := a select: [:item | b includes: item].
either := a copy.
either addAll: b.
{a size. b size. either size}Expected: #(3 3 4)

Try it: Why is the sum of the two sizes a poor denominator? The shared members would be counted twice.
04 - Compute the exact score
The ratio is two shared tags divided by four distinct tags. Smalltalk represents the result exactly as a fraction.
| a b both either |
a := #(tea toast jam) asSet.
b := #(tea eggs jam) asSet.
both := a select: [:item | b includes: item].
either := a copy.
either addAll: b.
both size / either sizeExpected: 1/2

Try it: Add an unshared tag to either side; the numerator stays fixed while the denominator grows.
05 - Choose a rule for empty sets
With both sets empty, the formula would divide by zero. This lesson returns one to treat two empty sets as equal; one empty side and a nonempty side still score zero.
| compare |
compare := [:left :right | | a b union common |
a := left asSet. b := right asSet.
union := a copy.
union addAll: b.
common := a count: [:item | b includes: item].
union isEmpty ifTrue: [1] ifFalse: [common / union size]].
{compare value: #() value: #(). compare value: #(tea) value: #()}Expected: #(1 0)

Try it: Use a different explicit policy if empty means unknown rather than no tags.
06 - Check symmetry and duplicate handling
Swapping input changes neither membership test counts nor the union. The last test checks that repeated tea does not inflate anything.
| compare |
compare := [:left :right | | a b union common |
a := left asSet. b := right asSet.
union := a copy.
union addAll: b.
common := a count: [:item | b includes: item].
union isEmpty ifTrue: [1] ifFalse: [common / union size]].
{compare value: #(tea tea jam) value: #(tea eggs).
compare value: #(tea eggs) value: #(tea tea jam).
compare value: #(tea tea) value: #(tea)}Expected: {1/3. 1/3. 1}

Try it: A recipe with only tea versus one with only eggs should score zero.
Replay the checkpoints

Reason about correctness and limits
Set membership ignores order. Use a frequency-based measure for cases where five mentions should count more than one.
Define how tags are normalized before comparing. In this example, Symbol names are already consistently spelled.
Three practice challenges
Identical lists
What should tea and jam score against jam and tea?
Show solution
1, because the two sets have the same members.
One new tag
How does adding toast to only one of two identical two-tag sets change the score?
Show solution
2/3: the overlap remains two and the union grows to three.
Rank two candidates
For query tea and jam, choose between tea only and tea, jam, eggs.
Show solution
The tea-only recipe scores 1/2, and the second scores 2/3; rank the second higher.
Continue learning
First Set | Collections | Document Search
Environment reference: official Squeak documentation. This lesson and its examples are original. Verified using native Squeak 6.1 build 23976 on Linux.