feat: codebook Compile — prose-in token abbreviation map#15
Merged
Conversation
Scans prose for high-frequency tokens (len > 6, freq > 1) and returns a substitution map of original → unique short abbreviation, enabling the prose-in codebook gateway described in issue #7. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds a new internal/identity/codebook authoring utility that derives abbreviation substitutions from free-form prose, intended as a building block for the “prose-in” codebook workflow discussed in #7.
Changes:
- Introduces
codebook.Compile(text string) (map[string]string, error)to extract high-frequency tokens and generate unique abbreviations. - Adds unit tests covering frequency/length filters, case normalization, uniqueness, and empty input.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| internal/identity/codebook/compile.go | Implements token frequency scanning and abbreviation generation for substitution maps. |
| internal/identity/codebook/compile_test.go | Adds tests validating extraction rules, normalization, and uniqueness constraints. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+25
to
+46
| // Collect candidates: tokens that appear more than once. | ||
| var candidates []string | ||
| for tok, count := range freq { | ||
| if count > 1 { | ||
| candidates = append(candidates, tok) | ||
| } | ||
| } | ||
|
|
||
| if len(candidates) == 0 { | ||
| return map[string]string{}, nil | ||
| } | ||
|
|
||
| result := make(map[string]string, len(candidates)) | ||
| usedAbbrs := make(map[string]struct{}) | ||
|
|
||
| for _, tok := range candidates { | ||
| abbr, err := uniqueAbbreviation(tok, usedAbbrs) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| result[tok] = abbr | ||
| usedAbbrs[abbr] = struct{}{} |
Comment on lines
+62
to
+70
| // Fall back: append a numeric suffix to a 3-char prefix. | ||
| base := string(runes[:3]) | ||
| for i := 2; i <= len(runes); i++ { | ||
| candidate := fmt.Sprintf("%s%d", base, i) | ||
| if _, taken := used[candidate]; !taken { | ||
| return candidate, nil | ||
| } | ||
| } | ||
| return "", fmt.Errorf("could not generate unique abbreviation for %q", tok) |
Comment on lines
+8
to
+21
| // Compile scans prose for high-frequency multi-syllable tokens (len > 6, freq > 1) | ||
| // and returns a substitution map of original_token → short_abbreviation. | ||
| // The abbreviations are unique within the returned map. | ||
| func Compile(text string) (map[string]string, error) { | ||
| if text == "" { | ||
| return map[string]string{}, nil | ||
| } | ||
|
|
||
| // Count normalized (lowercased) token frequencies. | ||
| freq := make(map[string]int) | ||
| for _, tok := range strings.Fields(text) { | ||
| tok = strings.ToLower(strings.Trim(tok, ".,;:!?\"'()[]{}")) | ||
| if len(tok) > 6 { | ||
| freq[tok]++ |
Comment on lines
+18
to
+22
| for _, tok := range strings.Fields(text) { | ||
| tok = strings.ToLower(strings.Trim(tok, ".,;:!?\"'()[]{}")) | ||
| if len(tok) > 6 { | ||
| freq[tok]++ | ||
| } |
Comment on lines
+42
to
+53
| func TestCompile_AbbreviationsAreUnique(t *testing.T) { | ||
| prompt := strings.Repeat("authentication configuration authorization ", 5) | ||
| subs, err := codebook.Compile(prompt) | ||
| require.NoError(t, err) | ||
| seen := make(map[string]string) | ||
| for orig, abbr := range subs { | ||
| if prev, exists := seen[abbr]; exists { | ||
| t.Errorf("abbreviation %q used for both %q and %q", abbr, prev, orig) | ||
| } | ||
| seen[abbr] = orig | ||
| } | ||
| } |
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
codebook.Compile(text string) (map[string]string, error)that scans prose for high-frequency tokens (length > 6, frequency > 1) and returns unique short abbreviationsTest plan
🤖 Generated with Claude Code