File size: 5,328 Bytes
494c9e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | /**
* findSplitPoint ๅๅ
ๆต่ฏ
* ่ฟ่ก: cd client/src && npx tsx ts/utils/findSplitPoint.test.ts
*
* SEPARATOR_GROUPS๏ผๅ
้จๅธธ้๏ผๆต่ฏไพ่ตๅ
ถ้ป่ฎคๅผ๏ผ:
* Group 0๏ผๅฅๅญ็บง๏ผ: ใ๏ผ๏ผ.!?
* Group 1๏ผๅญๅฅ็บง๏ผ: ๏ผ;๏ผ, ๏ผ็ฉบๆ ผ๏ผ
*/
import { findSplitPoint } from "./semanticUtils";
let passed = 0;
let failed = 0;
function assert(desc: string, actual: number, expected: number) {
if (actual === expected) {
console.log(` โ ${desc}`);
passed++;
} else {
console.error(` โ ${desc} โ expected ${expected}, got ${actual}`);
failed++;
}
}
// โโ 1. ๆ ไปปไฝๅ้็ฌฆ โ ๅ้ๅฐ maxEnd โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log("1. ๆ ๅ้็ฌฆ๏ผๅ้ maxEnd");
assert("็บฏๅญๆฏๆ ๅ้็ฌฆ", findSplitPoint("abcdef", 0, 6), 6);
assert("start=2๏ผไปๆ ๅ้็ฌฆ", findSplitPoint("abcdef", 2, 5), 5);
assert("CJK ๆ ๅ้็ฌฆ", findSplitPoint("ไฝ ๅฅฝไธ็", 0, 4), 4);
// โโ 2. ไผๅ
็บง๏ผๅฅๅญ็บงไผๅ
ไบๅญๅฅ็บง โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log("2. ๅฅๅญ็บง vs ๅญๅฅ็บงไผๅ
็บง");
// "ไฝ ๅฅฝ๏ผไธ็ใๅ่ง" โ ๅฅๅทๅจๅ๏ผ้ๅทๅจๅ๏ผๅบ้ๅฅๅท
assert("ๅฅๅทไผๅ
ไบ้ๅท",
findSplitPoint("ไฝ ๅฅฝ๏ผไธ็ใๅ่ง", 0, 7), 6); // "ไธ็ใ" ็ปๆไฝ็ฝฎ = idx 6
// "Hello, world! foo" โ ๆๅนๅทไผๅ
ไบ้ๅท
assert("ๆๅนๅทไผๅ
ไบ้ๅท",
findSplitPoint("Hello, world! foo", 0, 14), 13); // "world!" ็ปๆ idx=13
// โโ 3. ๅ็ปๅ
ๅๆ้ ๅณ็ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log("3. ๅ็ปๅ
ๆ้ ๅณ");
// "A.B?C" โ .ๅจidx1๏ผ?ๅจidx3๏ผๅๅฑๅฅๅญ็บง๏ผๅ ? ็ปๅฐพ(idx=4)
assert("ๅ็ปๅๆ้ ๅณ(. vs ?)",
findSplitPoint("A.B?C", 0, 5), 4);
// "x๏ผyใz๏ผw"๏ผๅ
จ JS ๅๅญ็ฌฆ๏ผโ ใๅจ idx=3๏ผ่พน็=4๏ผๅ้ข็ ๏ผๅจ idx=5๏ผgroup1๏ผ๏ผgroup0 ไผๅ
โ ่ฟๅ 4
assert("ๅฅๅญ็บง ใ ไผๅ
ไบๅ้ข็ ๏ผ",
findSplitPoint("x๏ผyใz๏ผw", 0, 8), 4);
// โโ 4. start ๅ็งป โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log("4. start ๅ็งป");
// "abcde.fg" start=4 โ window="e.fg"๏ผ. ๅจ window[1] โ ่ฟๅ start+2=6
assert("start ๅ็งปๅๆพๅฐๅฅ็น",
findSplitPoint("abcde.fg", 4, 8), 6);
// start=0 maxEnd=4 โ window="abcd"๏ผๆ ๅ้็ฌฆ โ 4
assert("ๅ็งปๅ window ๆ ๅ้็ฌฆๅ้",
findSplitPoint("abcde.fg", 0, 4), 4);
// โโ 5. ๅ้็ฌฆๆฐๅจ maxEnd ่พน็ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log("5. ๅ้็ฌฆ็ดง่ดด maxEnd");
// "abc." maxEnd=4 โ window ๅ
ๅซ . โ ่ฟๅ 4
assert("ๅฅ็นๆฐๅจ maxEnd ๅค",
findSplitPoint("abc.", 0, 4), 4);
// "abc." maxEnd=3 โ window="abc"๏ผๆ ๅ้็ฌฆ โ 3
assert("ๅฅ็นๅๅฅฝๅจ maxEnd ไนๅค",
findSplitPoint("abc.", 0, 3), 3);
// โโ 6. ็ฉบๆ ผ๏ผๅญๅฅ็บงๆไฝไผๅ
๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log("6. ็ฉบๆ ผ๏ผๆไฝไผๅ
็บง๏ผ");
assert("ๅชๆ็ฉบๆ ผๆถ้็ฉบๆ ผ",
findSplitPoint("hello world", 0, 11), 6); // "hello " ็ปๆ idx=6
// ็ฉบๆ ผ + ้ๅท โ ้ๅทไผๅ
๏ผๅๅฑ group1๏ผๅๆ้ ๅณ็๏ผ
// "hello, world test" โ "๏ผ" ไธๅญๅจ๏ผ"," ๅจ idx=5๏ผ็ฉบๆ ผๅจ idx=12๏ผๅ็ปๅ้ ๅณ โ 13๏ผ็ฉบๆ ผๅ๏ผ
assert("็ฉบๆ ผไธ้ๅทๅ็ป๏ผๅๆ้ ๅณ",
findSplitPoint("hello, world test", 0, 17), 13);
// โโ 7. ่ฟ็ปญๅ้็ฌฆ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log("7. ่ฟ็ปญๅ้็ฌฆ");
// "a..b" โ ไธคไธช . ้ฝๅจ group0๏ผlastIndexOf ๆพๅฐ้ ๅณ็้ฃไธช(idx=2) โ ่ฟๅ 3
assert("่ฟ็ปญๅฅ็นๅ้ ๅณ",
findSplitPoint("a..b", 0, 4), 3);
// โโ 8. ไธญๆๆ ็น โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log("8. ไธญๆๆ ็น");
// "่ฟๆฏๅฅๅญใไธไธๅฅ" โ ใๅจidx4 โ ่ฟๅ 5
assert("ไธญๆๅฅๅท",
findSplitPoint("่ฟๆฏๅฅๅญใไธไธๅฅ", 0, 8), 5);
// "่ฟๆฏ๏ผๅญๅฅ๏ผๅฆไธ" โ ๏ผๅจgroup0?ไธๅฏน๏ผ๏ผๅจgroup1๏ผ๏ผไนๅจgroup1๏ผ๏ผๅจidx4๏ผ๏ผๅจidx2๏ผๅ้ ๅณ โ idx=5
assert("ไธญๆๅๅทไผๅ
ไบ้ๅท๏ผๅ็ป้ ๅณ๏ผ",
findSplitPoint("่ฟๆฏ๏ผๅญๅฅ๏ผๅฆไธ", 0, 8), 6);
// โโ ็ปๆๆฑๆป โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
console.log(`\n็ปๆ: ${passed} ้่ฟ / ${failed} ๅคฑ่ดฅ`);
if (failed > 0) process.exit(1);
|