Command Palette

Search for a command to run...

Hectal
Phase 4: Ingestion: From Files to Searchable Chunks
RAG and Vector Search · Exercise 2 of 6 PreviousNext
Intermediate

Near-duplicates with shingles and Jaccard

Write shingles(text, k) that lower-cases the text, takes its words (runs of letters and digits) and returns the set of every window of k consecutive words joined by single spaces. A text with fewer than k words gives an empty set.

Write jaccard(a, b) that returns the size of the overlap of two sets divided by the size of their union, as a float. If both sets are empty, return 0.0.

What Check tests

  1. Shingles are windows of k words
  2. A short text has no shingles
  3. Repeated windows count once
  4. Jaccard is overlap over union
  5. Identical sets give 1 and empty sets give 0
  6. The two refund texts are similar but not equal

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.