C.W.K.
Stream
Lesson 05 of 05 · published

Complete, at Keystroke Speed

~12 min · complete, keystroke, phrase-prefix, punctuation-faithful

Level 0Unlit Wick
0 XP0/33 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
"Search finds someone else's words that match. Completion hands you back your own words, the way you actually wrote them."

A Different Question Entirely

Completion is not a smaller search. Search asks "which passages are relevant to this?" Completion asks something far more intimate: "I've typed the only question a long-term investor — how does my own corpus finish that phrase?" The answer isn't a ranked list of documents; it's a handful of continuations, mined from your writing, that could plausibly come next. It's less a librarian and more your own voice, echoed back mid-sentence.

How Phrase-Prefix Mining Works

The mechanism is elegant and entirely model-free. Take the tail of what you've typed and use it as a phrase-prefix query against the full-text index — matching where that exact sequence of words begins in the corpus. For each place it anchors, look at what comes immediately after, and gather those continuations. Rank them by how often each recurs, and you have your suggestions. It's not predicting the next word with a neural net; it's reporting how your corpus actually continued that phrase, every time it appeared.

No Embeddings, No Network, No Model

Because it's pure phrase-prefix lookup plus counting, completion touches only the local index. No embedding call, no vector store, no model server, no network hop of any kind. That's what lets it fire on every keystroke and still come back in tens of milliseconds. The determinism from the earlier track isn't just a nice property here — it's the enabling constraint. A completion path with a network call in it would be unusable, so there simply isn't one.

Emit raw source slices — don't reconstruct the text. A subtle trap: if you tokenize the continuation into words and re-join them with spaces, you quietly destroy punctuation. "investor's" becomes "investor s"; "QE & QT" loses its ampersand spacing. The fix is to return the exact substring of the source, apostrophes and ampersands and all, rather than rebuilding it from tokens. Faithful completion means the bytes you emit are the bytes that were written — never a lossy reconstruction.

A Resurrected Feature, Made Precise

Corpus phrase-completion is an old idea — writing tools have long offered to finish your sentence from your own past text. What makes Lantern's version sharp is the discipline stacked underneath it: a keystroke-grade budget honored by touching only local state, punctuation-faithful raw slices instead of reconstructions, and a junk guard so malformed source fragments don't surface as garbage suggestions. The feature feels magical when it finishes your thought in your own words — and the magic is entirely deterministic machinery underneath.

Code

Phrase-prefix + continuation mining, punctuation-faithful, no model·python
def complete(prefix: str, k: int = 5) -> list[str]:
    tail = last_phrase(prefix)                     # e.g. 'a long-term investor'
    # Phrase-prefix match: find where this exact word-sequence begins in the corpus.
    anchors = fts_phrase_prefix(tail)              # local index only, no network

    continuations: Counter[str] = Counter()
    for hit in anchors:
        # Take the EXACT source substring after the match — never re-join tokens,
        # or "investor's" degrades to "investor s" and "QE & QT" loses its spacing.
        raw = source_slice_after(hit)              # punctuation-faithful
        if not is_markdown_shrapnel(raw):          # junk guard
            continuations[raw] += 1

    # Rank by how often the corpus actually continued this way.
    return [text for text, _ in continuations.most_common(k)]

External links

Exercise

Imagine building autocomplete from your own past writing. Sketch the algorithm: given the last few words you typed, how would you find where that phrase occurred before, and how would you gather what came next? Now stress-test it on punctuation — write a phrase with an apostrophe or an ampersand and trace whether your approach would preserve it or mangle it. What would you have to return to stay faithful?
Hint
The faithful move is to return the exact substring of the original source, offsets and all, rather than splitting into words and rejoining. The instant you tokenize-and-rejoin, you've committed to guessing the spacing and punctuation back — and you'll guess wrong on contractions and symbols.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.