s LearnSqueak.com

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.

6 tested checkpointsNative Squeak screenshotsComputed visual replay

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.

Independent Workspace expression - select all, then Print it
(#(tea tea jam) asSet) size

Expected: 2

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 1: Count each tag once
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Try adding one more tea; the size does not change.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 2: Find only shared tags
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Replace eggs with toast in the second list. The intersection grows by one.

Download this checkpoint

03 - Count the union

Only the copy receives addAll:. The original sets both retain three members; the union has four, not six.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 3: Count the union
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Why is the sum of the two sizes a poor denominator? The shared members would be counted twice.

Download this checkpoint

04 - Compute the exact score

The ratio is two shared tags divided by four distinct tags. Smalltalk represents the result exactly as a fraction.

Independent Workspace expression - select all, then Print it
| 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 size

Expected: 1/2

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 4: Compute the exact score
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Add an unshared tag to either side; the numerator stays fixed while the denominator grows.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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)

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 5: Choose a rule for empty sets
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

Try it: Use a different explicit policy if empty means unknown rather than no tags.

Download this checkpoint

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.

Independent Workspace expression - select all, then Print it
| 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}

NATIVE SQUEAK CAPTURE
Native Squeak checkpoint 6: Check symmetry and duplicate handling
Genuine native Squeak Workspace capture. The validation harness evaluated this code and displayed its actual result below it.

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

Download this checkpoint

Replay the checkpoints

NATIVE SQUEAK CAPTURE
Jaccard Similarity in Squeak: Compare Two Sets native Workspace replay
Replay of successive native Squeak Workspace evaluations, not an interaction recording. The complete static checkpoints appear above.

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.