-
Notifications
You must be signed in to change notification settings - Fork 1
fix: length-aware pattern selection and syllable-boundary trimming #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bukinoshita
merged 11 commits into
main
from
fix/length-aware-pattern-selection-syllable-trim-2c81
Jul 10, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3fd4561
fix: trim overlong words at syllable boundaries instead of blind slicing
cursoragent a07dcbc
fix: select patterns by expected-length fit to the requested range
cursoragent fe5a3d7
refactor: share isVowel and trailingConsonantRun helpers across utils
cursoragent 336687b
test: assert legal endings against canonical FINAL_CLUSTERS
cursoragent f7b3d0b
refactor: deep-freeze pattern length stats
cursoragent a1769f9
perf: memoize pattern candidates per length range
cursoragent b64e364
fix: bound the pattern candidate cache with FIFO eviction
cursoragent 125ef6a
fix: isVowel rejects empty and multi-character strings
cursoragent e3f48fa
fix: rebuild from padding instead of keeping an illegal slice when no…
cursoragent 4ff102b
fix: bound padding scratch allocation to a fixed chunk size
cursoragent 86b261f
test: detect trailing qu in the illegal-ending regression helper
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { isVowel } from './is-vowel'; | ||
|
|
||
| describe('is-vowel', () => { | ||
| describe('isVowel', () => { | ||
| it('returns true for every vowel', () => { | ||
| expect(['a', 'e', 'i', 'o', 'u'].filter(isVowel)).toEqual(['a', 'e', 'i', 'o', 'u']); | ||
| }); | ||
|
|
||
| it('returns false for consonants', () => { | ||
| expect(['b', 'q', 'w', 'y', 'z'].filter(isVowel)).toEqual([]); | ||
| }); | ||
|
|
||
| it('returns false for the empty string', () => { | ||
| expect(isVowel('')).toBe(false); | ||
| }); | ||
|
|
||
| it('returns false for multi-character strings', () => { | ||
| expect(isVowel('ae')).toBe(false); | ||
| expect(isVowel('eio')).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const isVowel = (char: string): boolean => char.length === 1 && 'aeiou'.includes(char); |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { PATTERNS } from './pattern'; | ||
| import { PATTERN_LENGTH_STATS } from './pattern-length-stats'; | ||
|
|
||
| describe('pattern-length-stats', () => { | ||
| describe('PATTERN_LENGTH_STATS', () => { | ||
| it('has stats for every pattern', () => { | ||
| expect(Object.keys(PATTERN_LENGTH_STATS).toSorted()).toEqual([...PATTERNS].toSorted()); | ||
| }); | ||
|
|
||
| it('has positive means for every pattern', () => { | ||
| const offenders = PATTERNS.filter((pattern) => PATTERN_LENGTH_STATS[pattern].mean <= 0); | ||
| expect(offenders).toEqual([]); | ||
| }); | ||
|
|
||
| it('orders means by pattern complexity', () => { | ||
| expect(PATTERN_LENGTH_STATS.CV.mean).toBeLessThan(PATTERN_LENGTH_STATS.CVCV.mean); | ||
| expect(PATTERN_LENGTH_STATS.CVCV.mean).toBeLessThan(PATTERN_LENGTH_STATS.CVCVCV.mean); | ||
| }); | ||
|
|
||
| it('has non-decreasing cumulative distributions from 0 to 1', () => { | ||
| const offenders = PATTERNS.filter((pattern) => { | ||
| const { cumulative } = PATTERN_LENGTH_STATS[pattern]; | ||
| const monotone = cumulative.every( | ||
| (value, index) => index === 0 || value >= cumulative[index - 1], | ||
| ); | ||
| return !monotone || cumulative[0] !== 0 || cumulative[cumulative.length - 1] !== 1; | ||
| }); | ||
|
|
||
| expect(offenders).toEqual([]); | ||
| }); | ||
|
|
||
| it('reports short patterns as fitting under small bounds', () => { | ||
| expect(PATTERN_LENGTH_STATS.CV.cumulative[8]).toBe(1); | ||
| expect(PATTERN_LENGTH_STATS.CVCVCV.cumulative[8]).toBeLessThan(0.5); | ||
| }); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { buildPattern, PATTERNS, type Pattern } from './pattern'; | ||
| import { createRng } from './rng'; | ||
|
|
||
| const SAMPLE_COUNT = 512; | ||
| const MAX_TRACKED_LENGTH = 40; | ||
|
|
||
| interface LengthStats { | ||
| readonly mean: number; | ||
| readonly cumulative: readonly number[]; | ||
| } | ||
|
|
||
| const sampleLengths = (pattern: Pattern): readonly number[] => | ||
| Array.from({ length: SAMPLE_COUNT }, (_, index) => { | ||
| const [word] = buildPattern(pattern, createRng(index + 1)); | ||
| return word.length; | ||
| }); | ||
|
|
||
| const statsFor = (pattern: Pattern): LengthStats => { | ||
| const lengths = sampleLengths(pattern); | ||
| return Object.freeze({ | ||
| mean: lengths.reduce((sum, length) => sum + length, 0) / lengths.length, | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| cumulative: Object.freeze( | ||
| Array.from( | ||
| { length: MAX_TRACKED_LENGTH + 1 }, | ||
| (_, bound) => lengths.filter((length) => length <= bound).length / lengths.length, | ||
| ), | ||
| ), | ||
| }); | ||
| }; | ||
|
|
||
| export const PATTERN_LENGTH_STATS: Readonly<Record<Pattern, LengthStats>> = Object.freeze( | ||
| Object.fromEntries(PATTERNS.map((pattern) => [pattern, statsFor(pattern)])), | ||
| ) as Readonly<Record<Pattern, LengthStats>>; | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.