Skip to the workspace
Pebellslearn by building
String Algorithms  ›  Capstone Project

Capstone: Quarry's Text Index

50 minChoose the matcher by what it remembers: a border, a fingerprint or a box of known matches

dsa / Capstone: Quarry's Text Index

Explore each scene at your own pace.

01 · Picking the tool

Picking the tool

Capstone: Quarry's Text Index

Definition
A text index here is a single function that sends each query to the matching tool: KMP for one exact phrase, a rolling hash for many phrases of the same length, and two Z-arrays for a phrase that allows one typo. All of them run in linear time.
In plain words
It is a search box with three helpers behind it. One reads for an exact phrase, one checks many phrases at once, and one forgives a single wrong letter. The box decides who to ask, and each helper remembers something different about the text.

In short: one text_index function routes each query to whichever linear-time tool fits it best — KMP for an exact phrase, a rolling hash's fingerprint set for many same-length phrases at once, and two Z-arrays (forwards and backwards) for a query that allows one typo. None of the tools is faster than the others; what differs is what each one remembers between characters: a border, a fingerprint, or a matched box.

One text_index box routing to three matchers: KMP for a plain query, two Z-arrays for a ~typo query, rolling hash for many same-length queries

Three lessons have given Quarry three linear-time ways to look at text. This one puts them behind a single function, text_index, that takes a set of documents and a batch of queries and returns every place each query occurs. A query may also ask for one typo of slack. By the end, the piece you build is the one a user actually types into.

Picking the tool

Each technique answers a slightly different question, and the index uses each one where it fits best:

question best tool why
where does this one phrase occur? KMP (or Z) exact, worst-case O(n + m), no false alarms
does this phrase occur in a live stream? KMP the only state is j, so the text is read once
which of many same-length phrases occur? rolling hash one pass, and a set lookup per window covers every phrase
which passages repeat across documents? rolling hash fingerprint every window, group by fingerprint
where does it occur with at most one typo? Z, forwards and backwards front match plus back match covers all but one character

None of them is "the fast one". They are all linear. What differs is what they remember: a fingerprint, a border, or a box of known matches.

Scene 1 of 11
Project context and further resources
You're building a piece ofQuarry — the text index
This piece — text_index(): Answers exact and one-typo queries over a set of documents.
Scenario An on-call engineer searches Quarry for "~disc full" across last night's node logs. The one wrong letter still finds every "disk full", each with its node and position, in one pass over the text.
text_index.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
text_index(docs, queries) → dictEach query mapped to every [document, position] where it occurs, exactly or with one typo.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.