Skip to content

Prefix match for keywords ending in * - #190

Merged
A21-devs merged 2 commits into
mainfrom
feat/keyword-prefix-match
Dec 5, 2026
Merged

A21-devs merged 2 commits into
mainfrom
feat/keyword-prefix-match

Conversation

@A21-devs

@A21-devs A21-devs commented Dec 5, 2026

Copy link
Copy Markdown
Owner

Closes #123

--keywords win only matched the word "win", so "wins" and "winning" were missed and the log just said "win" 0x. This adds an opt-in prefix match: a keyword word ending in * matches any transcript word that starts with the rest of it.

How:

  • keywords.keyword_tokens() splits a keyword the same way tokens() does, but keeps a * that directly follows a word. parse/parse_list use it, so the CLI, serve and the MCP tool all get it.
  • find() compares each phrase word with _matches(): startswith for a starred word, == otherwise. The phrase keeps its *, so the log and the reasons show "win*" the way it was typed.
  • Edge cases: win** is win*; * on its own (or *, **) has no words, so it's dropped and the usual "needs at least one word" error applies instead of matching everything; *win is win; wi*n splits into wi n the same way any other symbol inside a word would. Transcript text is still tokenized with tokens(), so stars whisper writes (f***ing) aren't treated as wildcards.
  • README keywords section and the --keywords help mention it.

Irregular forms ("won") are still out of scope.

Testing

  • uv run ruff check src tests, uv run mypy, uv run pytest -q: 780 passed.
  • New tests in test_keywords.py: win vs win* on win/wins/Winning!/window/won, a star at the end and in the middle of a phrase (no way*, no* way), parsing of WIN**, let's*, no-scope*, wi*n, *win, lone stars, and the transcript side ignoring stars.
  • Real run on a 4 min TTS-over-gameplay video, whisper tiny, -n 2:
    • --keywords "jump, no way*": log "jump" 3x, "no way*" 3x, clips 101-156s and 174-194s
    • --keywords "jump*, no way*": log "jump*" 6x, "no way*" 3x (the extra three are "jumping"), clip 2 moved to 169-224s to take in more of them, and the reasons read said "no way*", "jump*"

win* finds wins and winning; plain words stay whole-word matches. A
lone * or one inside a word is a separator like before.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked that keyword_tokens splits exactly like tokens when there's no *: both produce runs of [\w'] minus _ with the apostrophes stripped at the ends, so existing keywords parse the same as before. The transcript side still goes through tokens(), so stars in whisper output aren't wildcards, and the starred phrase string goes through to the reasons and explain.py unchanged (that one is escaped). One real problem, with the length of the prefix.

Comment thread src/clipwright/keywords.py Outdated
for m in _KEY_WORD.finditer(_normalise(text)):
word = m.group(1).strip("'")
if word:
out.append(word + (PREFIX if m.group(2) else ""))

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing limits how short the prefix can be. On the TTS test transcript (502 words) t* hits 96 times, th* 75 and a* 36. With hits that dense every window reaches KEYWORD_CAP, so the boost is the same for every window: it can't change the pick, and every clip's reasons say said "t*", which looks like it worked. It's an easy thing to type by accident (no way, i*). I'd refuse a prefix under 3 characters with a message saying why, from parse_list, so the CLI, serve and MCP all show it. win* is 3, so the README's example still works.

t* hit 96 of 502 words on a test transcript, so every window reached
the keyword cap and every clip said it. The CLI, serve and MCP all
show the error.
@A21-devs

A21-devs commented Dec 5, 2026

Copy link
Copy Markdown
Owner Author

Fixed in 8969e5b. keyword_tokens raises ValueError for a starred word with fewer than 3 characters (MIN_PREFIX). It's a ValueError and not an empty result, so the user is told why instead of getting "needs at least one word".

  • CLI: clipwright talk.mp4 --keywords "no way, th*" prints error: --keywords: "th*" would match too many words, put at least 3 letters before the * and exits 2, with no traceback.
  • serve/MCP: _keywords turns it into a 400 keywords: .... While doing this I noticed that BadRequest subclasses ValueError, so wrapping the whole branch in except ValueError would also have caught and re-labelled the "must be a comma separated string" error. The type check now runs first, and only parse_list sits inside the try. The string and list forms still go through the same parse_list they did before (parse is just parse_list(text.split(","))).
  • README says 3 letters. test_star_inside_a_phrase used no* way, which is now refused, so it uses nop* way.
  • New tests: t*, no way, th* and i* win refused by parse and parse_list; two new rows in the server's bad-request table; a CLI test for the exit code and message.

Full suite: 786 passed, ruff and mypy clean.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second pass on 8969e5b. The short-prefix finding is fixed for all three entry points: the CLI returns the message with exit 2, and parse_job gives a 400 (covered by the two new table rows). Since the type check now runs before the try, a non-string list item still gets keywords must be ... and not a re-labelled error. That case is still covered by the ["clutch", 3] row, which passes.

A tradeoff to know about rather than a bug: the limit counts characters, so a real two-letter stem like go* (goes, going) is now refused. I think that's the right default given t*/th*, and goi*/goes written out still work.

Comment thread src/clipwright/server.py
else:
raise BadRequest("keywords must be a comma separated string or a list of strings")
try:
phrases = keywords.parse_list(items)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 500-character size check now runs after parse_list, so an oversized string that also has t* in it reports the prefix error first. The order was the same before this PR (parse, then size), and parsing 500-ish characters is cheap, so this is just a note. Nothing to change.

@A21-devs
A21-devs merged commit a46866e into main Dec 5, 2026
4 checks passed
@A21-devs
A21-devs deleted the feat/keyword-prefix-match branch December 5, 2026 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--keywords misses other word forms (win vs wins, winning)

1 participant