Skip to content

fix: match router keywords as whole words - #31

Merged
anikchand461 merged 2 commits into
smollabs:mainfrom
starkhushi:fix/5-router-whole-word-keywords
Nov 20, 2026
Merged

anikchand461 merged 2 commits into
smollabs:mainfrom
starkhushi:fix/5-router-whole-word-keywords

Conversation

@starkhushi

Copy link
Copy Markdown
Contributor

Classify counted complexKws with strings.Contains, so a keyword matched inside other words and one word could count twice (plan in "planets", code in "barcode", bug inside "debug", how do inside "how does").

Each keyword is now compiled once into a regexp that matches it as a whole word or phrase:

  • single words may take a regular ending (s, es, d, ed, ing), so "explained", "bugs" and "summarised" still count, while "designer", "reasonable" and "programme" don't;
  • the stems analy and optimi still match any word that starts with them (analysis, optimise, optimization);
  • phrases match exactly, so how do no longer also matches "how does".

The keyword list, the weights and the rest of Classify are unchanged.

Query Before After
tell me about planets moderate simple
what is a barcode moderate simple
what is a reasonable price moderate simple
how does rain form over the sea in the middle of a cold winter complex (how does + how do) moderate

Closes #5

How I tested it

  • go test ./tests -run "Classify|SelectModel": all pass. Two new tests in tests/router_test.go: keywords match only as whole words (the cases above plus endings and stems), and how does / debug are counted once. Both fail on main.
  • gofmt -l prints nothing for the two files and go vet ./app ./tests is clean.
  • I ran this on Windows with CGO_ENABLED=0, so the three database tests that need cgo sqlite (TestConversationLifecycle, TestSearchAndOrdering, TestPersistsAcrossReopen) can't run there; they fail the same way on main. No device or emulator test, since this is router logic only.

@anikchand461

Copy link
Copy Markdown
Member

Thanks @starkhushi , this is a nice fix and the tests are clear! One problem: the endings miss words that double their last letter or drop a final "e". "planning", "debugging" and "programming" now classify as simple (they were moderate on main), and "coding", "comparing" and "summarizing" never matched.

Could you handle both cases in compileKeywords? Something like:

case len(words) == 1 && strings.HasSuffix(k, "e"):
// code → codes, coded, coding (but not "cod")
p = regexp.QuoteMeta(strings.TrimSuffix(k, "e")) + (?:e|es|ed|ing)
case len(words) == 1:
// plan → plans, planned, planning
last := regexp.QuoteMeta(k[len(k)-1:])
p += (?:s|es|ed|ing| + last + ed| + last + ing)?

Please also add "planning", "debugging", "coding" and "comparing" to the tests. Thanks!

@starkhushi

Copy link
Copy Markdown
Contributor Author

Thanks @anikchand461, good catch. Done in the latest commit, using your two cases:

  • keywords ending in "e" match the stem plus e, es, ed or ing (code, codes, coded, coding; compare, comparing; summarize, summarizing), without matching the bare stem ("a cod for dinner" stays simple);
  • other single words also accept a doubled last letter before ed / ing (planned, planning, debugging, programming).

Added "planning", "debugging", "programming", "coding", "comparing" and "summarizing" (all moderate) plus "a cod for dinner" (simple) to the tests. gofmt and go vet are clean and the router tests pass.

@anikchand461
anikchand461 merged commit 2f3a9ec into smollabs:main Nov 20, 2026
1 check passed
@starkhushi

Copy link
Copy Markdown
Contributor Author

Thanks for merging, @anikchand461! Could you add the wocs and level 1 labels (issue #5 is level 1) so it counts on the WOCS leaderboard? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto router keyword matching counts substrings and overlaps (planets, barcode, how does)

2 participants