Skip to content

Commit bc2773d

Browse files
fix(check): a Korean number and its counter count as one word in line reading time (#7343)
* fix(check): count a Korean number and its counter as one word in line reading time * fix(check): keep segmenting Thai, Lao, Khmer and Burmese when counting reading words
1 parent b37db11 commit bc2773d

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎packages/cli/src/utils/craftAudit.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ describe("readingWords", () => {
8585
expect(readingWords("今天天气很好")).toBeGreaterThan(1);
8686
});
8787

88+
it("counts Korean by its spaces and a number with its counter as one unit", () => {
89+
expect(readingWords("실패 12회")).toBe(2);
90+
expect(readingWords("오류 3건 발생")).toBe(3);
91+
expect(readingWords("테스트 실패 12/40")).toBe(4);
92+
expect(readingWords("커피12잔")).toBe(1);
93+
});
94+
95+
it("still segments scripts written without spaces", () => {
96+
expect(readingWords("ฉันรักภาษาไทยมาก")).toBeGreaterThan(1);
97+
expect(readingWords("ខ្ញុំស្រលាញ់ភាសាខ្មែរ")).toBeGreaterThan(1);
98+
expect(readingWords("ຂ້ອຍຮັກພາສາລາວຫຼາຍ")).toBeGreaterThan(1);
99+
expect(readingWords("ကျွန်တော်မြန်မာစကားကိုချစ်တယ်")).toBeGreaterThan(1);
100+
});
101+
102+
it("keeps counting a Japanese number and its counter as two", () => {
103+
expect(readingWords("12回")).toBe(2);
104+
expect(readingWords("実行12回失敗")).toBe(4);
105+
});
106+
88107
it("counts a contraction as one word", () => {
89108
expect(readingWords("a tooth can't get in")).toBe(5);
90109
expect(readingWords("a tooth can’t get in")).toBe(5);

‎packages/cli/src/utils/craftAudit.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ const ZERO_BBOX: CheckBbox = { x: 0, y: 0, width: 0, height: 0 };
3232
const WORD_RUNS = /[\p{L}\p{M}\p{N}%$]+(?:['’][\p{L}\p{M}\p{N}]+)*/gu;
3333
// Chinese and Japanese have no spaces; a dictionary segmenter counts their words like an English line's.
3434
const WORD_SEGMENTER = new Intl.Segmenter("ja", { granularity: "word" });
35+
const UNSPACED_SCRIPT =
36+
/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar}]/u;
3537

3638
/** Words a viewer reads: letter/number runs, with CJK runs split into dictionary words. */
3739
export function readingWords(text: string): number {
3840
const runs = text.toLowerCase().normalize("NFC").match(WORD_RUNS) ?? [];
3941
return runs.reduce(
4042
(count, run) =>
41-
count + Math.max(1, [...WORD_SEGMENTER.segment(run)].filter((s) => s.isWordLike).length),
43+
count +
44+
(UNSPACED_SCRIPT.test(run)
45+
? Math.max(1, [...WORD_SEGMENTER.segment(run)].filter((s) => s.isWordLike).length)
46+
: 1),
4247
0,
4348
);
4449
}

0 commit comments

Comments
 (0)