feat(caveman-compress): deterministic phrase-lookup pre-pass (216 entries) - #813
Open
drewg2009 wants to merge 2 commits into
Open
feat(caveman-compress): deterministic phrase-lookup pre-pass (216 entries)#813drewg2009 wants to merge 2 commits into
drewg2009 wants to merge 2 commits into
Conversation
Multi-word wordy phrases ("due to the fact that", "in order to", "with
regard to", ...) collapse to a single word with a fixed lookup table,
skipping code blocks and inline code. This runs before the Claude
compression call, shrinking the prompt for free instead of spending a
model call on something a dictionary already knows.
Measured with tiktoken (o200k_base): 0% match on this repo's own terse
engineering fixtures, ~32% token cut on deliberately wordy/corporate-style
prose. Value is conditional on how verbose the source writing is β see
phrase_map.py's docstring and the new SKILL.md/README sections for the
full reasoning and the reproducible measurement in test_phrase_map.py.
β¦boundary bug
Expanded PHRASE_MAP from ~60 to 216 entries by researching plain-language,
legal/government, academic, and technical-documentation style guides
(plainlanguage.gov, Federal Plain Language Guidelines, Microsoft's and
Google's developer-docs style guides, university writing-center
conciseness handouts). Every candidate was filtered against the same rule
as the original set: the replacement must be a grammatically valid drop-in
regardless of what follows it. A meaningful chunk of raw source-list
entries failed this and were excluded β e.g. "perform an analysis of" ->
"analyze of" is broken (dangling preposition), "have a discussion about"
-> "discuss about" repeats a preposition "discuss" doesn't take, and
"table this" -> "postpone" is flipped in British English. See the module
docstring for the full reasoning.
Also fixes two real bugs found while vetting the expanded set:
- Phrase matching had no word-boundary anchoring, so a short entry like
"point in time" could match mid-word inside unrelated text (e.g. the
literal substring "point in time" occurs inside "checkpoint in time-
series data"). Added \b anchors on both ends of the compiled regex.
- "close proximity to" -> "near" broke on the common "in close proximity
to X" phrasing, producing the double-preposition "in near X". Added the
longer "in close proximity to" variant so it wins via longest-first
matching.
Substitution now runs to a fixed point (bounded by _MAX_PASSES) instead of
a single pass, so a replacement that creates a new matchable phrase gets
caught too β proven with a synthetic map in the tests since real entries
rarely chain by design.
Re-measured with tiktoken: this repo's own terse fixtures still see near-
zero benefit (~0.02%), corporate-style prose cuts ~32%, and README-style
instructional prose ("please make sure that...", "this allows you to...")
cuts ~26% β the expanded table adds real coverage for the software-docs
style caveman-compress is most likely to actually run on.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
scripts/phrase_map.py, 216 entries) that runs before the Claude compression call incaveman-compress, collapsing multi-word phrases like"due to the fact that"β"because"and"please make sure that"β"confirm"for free β no model call needed for the part a dictionary already knows."extensive"β"big"costs the same either way, so it's not in the table).Why
compress.pycurrently only compresses via an LLM call β every phrase gets rewritten by Claude even when the correct rewrite is a fixed, unambiguous fact of English ("due to the fact that" always means "because"). Pre-collapsing those phrases with a lookup table shrinks the prompt Claude receives, for zero extra tokens or latency.Sourcing and filtering
Entries were pulled from plain-language style guides across four domains β general plain-language (plainlanguage.gov, Federal Plain Language Guidelines), legal/government, academic/scientific writing, and technical documentation (Microsoft's and Google's own developer-docs style guides) β then hand-filtered against one hard rule: a phrase only qualifies if swapping it in verbatim, at any position in any sentence, produces grammatically valid text.
A meaningful chunk of raw source-list entries failed this and were deliberately excluded (see the module docstring for the full list and reasoning):
"perform an analysis of X"β"analyze of X"β dangling preposition, broken."have a discussion about X"β"discuss about X"βdiscussdoesn't takeabout."table this"β"postpone"β means the opposite in British English (real regional ambiguity)."as prescribed by"β"under"β breaks on the common "as prescribed by the doctor" (person) sense vs. the statute sense."will allow you to"β"lets you"β collapses future tense to present, which could misdescribe an unshipped feature as already live."it was demonstrated that"β"evidently"β weakens a scientific claim's epistemic strength, not just wordiness.Two bugs found and fixed while vetting the expanded set
"point in time"could match mid-word inside unrelated text β e.g. the literal substring"point in time"occurs inside"checkpoint in time-series data", which an unanchored regex would mangle. Added\banchors on both ends."close proximity to"β"near"broke on the common"in close proximity to X"phrasing, producing"in near X". Fixed by adding the longer"in close proximity to"variant, which wins via longest-first matching.Honest measurement (no fabricated numbers β see
tests/test_phrase_map.py::PhraseMapTokenReductionTests)Measured with
tiktoken(o200k_base, same tokenizerevals/measure.pyuses):tests/caveman-compress/*.original.mdfixtures (terse engineering notes)This repo's own fixtures barely move β that style of prose doesn't use the phrases in the table. It helps most on verbose writing (meeting notes, policy docs, over-explained setup instructions, corporate email pasted into a memory file) and does close to nothing on prose that's already terse. Documented this conditional impact directly in
SKILL.mdandREADME.mdrather than overstating it.Changes
skills/caveman-compress/scripts/phrase_map.pyβ new module,apply_phrase_map(), 216-entryPHRASE_MAP, word-boundary-anchored, fence-aware, case-preserving, runs to a fixed point.skills/caveman-compress/scripts/compress.pyβ callsapply_phrase_map()on the body before building the Claude prompt.skills/caveman-compress/SKILL.md/README.mdβ sections explaining the pre-pass, sourcing, the two bug fixes, and the measured (conditional) impact.tests/test_phrase_map.pyβ 17 tests: fence/code-span safety, case preservation, longest-phrase-wins ordering, word-boundary mid-word false-match prevention, the double-preposition regression, recursive-pass convergence (proven with a synthetic map), and tiktoken-based reduction tests (skip cleanly iftiktokenisn't installed, matching the existing optional-dependency pattern inevals/).Test plan
python3 -m unittest tests.test_phrase_map -vβ 15/17 pass without tiktoken installed (2 skip)uv run --with tiktoken python3 -m unittest tests.test_phrase_map -vβ 17/17 pass including token-count assertionspython3 -m unittest discover -s tests -p "test_*.py"β full suite, 73 tests, no regressionsPHRASE_MAPfor duplicate keys (none β 216 unique)