@kensio/pinyinjs 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +93 -24
  2. package/README.md +40 -35
  3. package/dist/cli/arguments.d.ts +45 -0
  4. package/dist/cli/arguments.d.ts.map +1 -1
  5. package/dist/cli/arguments.js +83 -0
  6. package/dist/cli/arguments.js.map +1 -1
  7. package/dist/cli/commands.d.ts.map +1 -1
  8. package/dist/cli/commands.js +24 -1
  9. package/dist/cli/commands.js.map +1 -1
  10. package/dist/cli/help.d.ts.map +1 -1
  11. package/dist/cli/help.js +17 -0
  12. package/dist/cli/help.js.map +1 -1
  13. package/dist/decode/convert.d.ts +10 -0
  14. package/dist/decode/convert.d.ts.map +1 -1
  15. package/dist/decode/convert.js +72 -28
  16. package/dist/decode/convert.js.map +1 -1
  17. package/dist/decode/decode.d.ts +12 -2
  18. package/dist/decode/decode.d.ts.map +1 -1
  19. package/dist/decode/decode.js +64 -7
  20. package/dist/decode/decode.js.map +1 -1
  21. package/dist/decode/reading-rules.d.ts +57 -0
  22. package/dist/decode/reading-rules.d.ts.map +1 -1
  23. package/dist/decode/reading-rules.js +178 -1
  24. package/dist/decode/reading-rules.js.map +1 -1
  25. package/dist/format/slug.d.ts +156 -0
  26. package/dist/format/slug.d.ts.map +1 -0
  27. package/dist/format/slug.js +0 -0
  28. package/dist/format/slug.js.map +1 -0
  29. package/dist/index.d.ts +4 -2
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +3 -2
  32. package/dist/index.js.map +1 -1
  33. package/dist/numerals/cardinal.d.ts +15 -1
  34. package/dist/numerals/cardinal.d.ts.map +1 -1
  35. package/dist/numerals/cardinal.js +8 -1
  36. package/dist/numerals/cardinal.js.map +1 -1
  37. package/dist/numerals/characters.d.ts +9 -0
  38. package/dist/numerals/characters.d.ts.map +1 -1
  39. package/dist/numerals/characters.js +18 -0
  40. package/dist/numerals/characters.js.map +1 -1
  41. package/dist/numerals/numerals.d.ts.map +1 -1
  42. package/dist/numerals/numerals.js +12 -4
  43. package/dist/numerals/numerals.js.map +1 -1
  44. package/dist/numerals/text.d.ts +18 -5
  45. package/dist/numerals/text.d.ts.map +1 -1
  46. package/dist/numerals/text.js +67 -8
  47. package/dist/numerals/text.js.map +1 -1
  48. package/package.json +1 -1
@@ -27,8 +27,14 @@ export declare function decodeSpacing(lattice: Lattice): readonly number[];
27
27
  * Stages 1 to 3 of ALGORITHM.md end to end — build every candidate reading,
28
28
  * lock the positions that cannot vary, score the rest — where the greedy
29
29
  * baseline commits to the longest match at each position and never revisits it.
30
+ *
31
+ * `before` is 汉字 standing in front of the run that the decode should see and
32
+ * not report: the 汉字 a number written in digits would have been written with.
33
+ * A run is decoded on its own without it, and a run reached that way has no idea
34
+ * what preceded it — 个人 alone is the word `gèrén`, while the 两 of 2个人 makes
35
+ * 两个 the word and leaves 人 to itself.
30
36
  */
31
- export declare function decodeRun(dictionary: Dictionary, run: string, rules?: readonly EdgeRule[]): readonly DecodedWord[];
37
+ export declare function decodeRun(dictionary: Dictionary, run: string, rules?: readonly EdgeRule[], before?: string): readonly DecodedWord[];
32
38
  /**
33
39
  * Decode a Han run, keeping what each reading was chosen over.
34
40
  *
@@ -36,6 +42,10 @@ export declare function decodeRun(dictionary: Dictionary, run: string, rules?: r
36
42
  * candidates kept rather than discarded. Separate because the extra sweep is
37
43
  * only worth running for a caller that will use the answer — rendering
38
44
  * uncertain readings differently, or reporting them.
45
+ *
46
+ * `before` is the context {@link decodeRun} takes, and means the same here: it
47
+ * is decoded with the run and reported with neither words nor confidence of its
48
+ * own.
39
49
  */
40
- export declare function decodeRunScored(dictionary: Dictionary, run: string, rules?: readonly EdgeRule[]): readonly ScoredWord[];
50
+ export declare function decodeRunScored(dictionary: Dictionary, run: string, rules?: readonly EdgeRule[], before?: string): readonly ScoredWord[];
41
51
  //# sourceMappingURL=decode.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode/decode.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAE9D,OAAO,EAA2B,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAGL,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAGjB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3D,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEzD;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,OAAO,EAChB,UAAU,EAAE,iBAAiB,GAC5B,SAAS,WAAW,EAAE,CAmBxB;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,MAAM,EAAE,CAIjE;AA2ED;;;;;;GAMG;AACH,wBAAgB,SAAS,CACvB,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,MAAM,EACX,KAAK,GAAE,SAAS,QAAQ,EAAkB,GACzC,SAAS,WAAW,EAAE,CAMxB;AAED;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,MAAM,EACX,KAAK,GAAE,SAAS,QAAQ,EAAkB,GACzC,SAAS,UAAU,EAAE,CAavB"}
1
+ {"version":3,"file":"decode.d.ts","sourceRoot":"","sources":["../../src/decode/decode.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAG9D,OAAO,EAAqC,KAAK,OAAO,EAAE,MAAM,cAAc,CAAC;AAC/E,OAAO,EAGL,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAGjB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAkB,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3D,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAEzD;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,OAAO,EAChB,UAAU,EAAE,iBAAiB,GAC5B,SAAS,WAAW,EAAE,CAmBxB;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,MAAM,EAAE,CAIjE;AAgJD;;;;;;;;;;;;GAYG;AACH,wBAAgB,SAAS,CACvB,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,MAAM,EACX,KAAK,GAAE,SAAS,QAAQ,EAAkB,EAC1C,MAAM,SAAK,GACV,SAAS,WAAW,EAAE,CAOxB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,UAAU,EACtB,GAAG,EAAE,MAAM,EACX,KAAK,GAAE,SAAS,QAAQ,EAAkB,EAC1C,MAAM,SAAK,GACV,SAAS,UAAU,EAAE,CAcvB"}
@@ -1,5 +1,6 @@
1
+ import { toCharacters } from "../script/characters.js";
1
2
  import { scoreReadings } from "./confidence.js";
2
- import { buildLattice, cutPoints } from "./lattice.js";
3
+ import { allEdges, buildLattice, cutPoints } from "./lattice.js";
3
4
  import { isSettled, projectReadings, settledUnits, unitsOf, } from "./locking.js";
4
5
  import { READING_RULES } from "./reading-rules.js";
5
6
  import { applyEdgeRules } from "./rules.js";
@@ -97,17 +98,68 @@ function wordFrom(dictionary, lattice, group) {
97
98
  function ruledLattice(dictionary, run, rules) {
98
99
  return applyEdgeRules(buildLattice(dictionary, run), dictionary, rules);
99
100
  }
101
+ /**
102
+ * Whether any reading in the lattice would hold the two sides of a join
103
+ * together.
104
+ *
105
+ * A reading of one syllable per character can be cut anywhere; anything else —
106
+ * 儿化, a character with no reading — is a single claim about its whole span,
107
+ * so a claim spanning the join cannot be reported for the run alone. 1点儿事
108
+ * is that case: 一点儿 is one `yìdiǎnr` over the 一 the context supplied and
109
+ * the 点儿 the run did.
110
+ */
111
+ function isJoinedAt(lattice, at) {
112
+ return allEdges(lattice).some((edge) => edge.from < at &&
113
+ edge.to > at &&
114
+ edge.reading.length !== edge.to - edge.from);
115
+ }
116
+ /**
117
+ * The lattice a run decodes over, with whatever context stands in front of it.
118
+ *
119
+ * The context is dropped where a reading would hold it to the run, leaving the
120
+ * run decoded on its own, which is what it would have been before there was any
121
+ * context to give.
122
+ */
123
+ function runLattice(dictionary, run, rules, before) {
124
+ const alone = () => ({
125
+ lattice: ruledLattice(dictionary, run, rules),
126
+ at: 0,
127
+ });
128
+ if (before === "") {
129
+ return alone();
130
+ }
131
+ const lattice = ruledLattice(dictionary, before + run, rules);
132
+ const at = toCharacters(before).length;
133
+ return isJoinedAt(lattice, at) ? alone() : { lattice, at };
134
+ }
135
+ /**
136
+ * Cut a decode of the whole lattice down to the run at the end of it.
137
+ *
138
+ * Everything before `at` was context rather than run — 汉字 a caller put in
139
+ * front so that the decode could see them — and is dropped once it has done its
140
+ * work.
141
+ */
142
+ function runGroups({ lattice, at }, units) {
143
+ return groupUnits(units.filter((unit) => unit.from >= at), decodeSpacing(lattice).filter((from) => from >= at));
144
+ }
100
145
  /**
101
146
  * Decode a Han run with the lattice: the recommended path.
102
147
  *
103
148
  * Stages 1 to 3 of ALGORITHM.md end to end — build every candidate reading,
104
149
  * lock the positions that cannot vary, score the rest — where the greedy
105
150
  * baseline commits to the longest match at each position and never revisits it.
151
+ *
152
+ * `before` is 汉字 standing in front of the run that the decode should see and
153
+ * not report: the 汉字 a number written in digits would have been written with.
154
+ * A run is decoded on its own without it, and a run reached that way has no idea
155
+ * what preceded it — 个人 alone is the word `gèrén`, while the 两 of 2个人 makes
156
+ * 两个 the word and leaves 人 to itself.
106
157
  */
107
- export function decodeRun(dictionary, run, rules = READING_RULES) {
108
- const lattice = ruledLattice(dictionary, run, rules);
158
+ export function decodeRun(dictionary, run, rules = READING_RULES, before = "") {
159
+ const held = runLattice(dictionary, run, rules, before);
160
+ const { lattice } = held;
109
161
  const units = decodeReadings(lattice, projectReadings(lattice));
110
- return groupUnits(units, decodeSpacing(lattice)).map((group) => wordFrom(dictionary, lattice, group));
162
+ return runGroups(held, units).map((group) => wordFrom(dictionary, lattice, group));
111
163
  }
112
164
  /**
113
165
  * Decode a Han run, keeping what each reading was chosen over.
@@ -116,11 +168,16 @@ export function decodeRun(dictionary, run, rules = READING_RULES) {
116
168
  * candidates kept rather than discarded. Separate because the extra sweep is
117
169
  * only worth running for a caller that will use the answer — rendering
118
170
  * uncertain readings differently, or reporting them.
171
+ *
172
+ * `before` is the context {@link decodeRun} takes, and means the same here: it
173
+ * is decoded with the run and reported with neither words nor confidence of its
174
+ * own.
119
175
  */
120
- export function decodeRunScored(dictionary, run, rules = READING_RULES) {
121
- const lattice = ruledLattice(dictionary, run, rules);
176
+ export function decodeRunScored(dictionary, run, rules = READING_RULES, before = "") {
177
+ const held = runLattice(dictionary, run, rules, before);
178
+ const { lattice } = held;
122
179
  const units = scoreReadings(lattice, projectReadings(lattice));
123
- return groupUnits(units, decodeSpacing(lattice)).map((group) => ({
180
+ return runGroups(held, units).map((group) => ({
124
181
  word: wordFrom(dictionary, lattice, group),
125
182
  confidence: group.flatMap((unit) => unit.reading.map(() => ({
126
183
  isLocked: unit.isLocked,
@@ -1 +1 @@
1
- {"version":3,"file":"decode.js","sourceRoot":"","sources":["../../src/decode/decode.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAmB,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,SAAS,EAAgB,MAAM,cAAc,CAAC;AACrE,OAAO,EACL,SAAS,EACT,eAAe,EAGf,YAAY,EACZ,OAAO,GACR,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAiB,MAAM,YAAY,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,YAAY,EAA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,QAAQ,CAAC,UAAU,EAAE,OAAO,EAAE,KAAK,CAAC;QAC1C,UAAU,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CACjC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC;YACtB,QAAQ,EAAE,IAAI,CAAC,QAAQ;YACvB,YAAY,EAAE,IAAI,CAAC,YAAY;SAChC,CAAC,CAAC,CACJ;KACF,CAAC,CAAC,CAAC;AACN,CAAC"}
@@ -49,6 +49,63 @@ export declare const MODAL_DE: EdgeRule;
49
49
  * 门铃儿 and a row of Mongol names transliterated with 儿.
50
50
  */
51
51
  export declare const ATTESTED_ERHUA: EdgeRule;
52
+ /**
53
+ * 教 read as `jiāo` where it is teaching rather than a religion.
54
+ *
55
+ * The dictionary stores 教 as `jiào` with `jiāo` as an alternate, and `jiào` is
56
+ * right for the compounds — 教育, 教师, 宗教, 主教 — which are words and reach
57
+ * their reading through the word. What is left is the 教 that stands as a word
58
+ * of its own, and that one is the verb: 他在北京大学教了三年书 came out
59
+ * `jiàole sān nián shū`, and 我教英语, 谁教你法语 and 她教我如何游泳 with it.
60
+ * 教书 is a word and was always `jiāo shū`; nothing carried that across a 了.
61
+ *
62
+ * See {@link isTeachingAt} for what decides it. Forcing the single-character
63
+ * edge is not enough on its own, because a reading spanning two characters
64
+ * carries its own 教 into the position: 王老师教我们汉语 read `jiào` off 师教,
65
+ * and 来教我 off 来教. Those are pairs the dictionary holds with no part of
66
+ * speech and at the cost of a word nothing has ever counted — a reading rather
67
+ * than a word — so a *tagged* word ending in 教 is left alone, which is every
68
+ * one that matters: 任教, 宗教, 主教, 佛教, 传教, 执教, 请教, 家教.
69
+ *
70
+ * Measured over 88,866 lines of Tatoeba and zh.wikipedia, 181 教 decode as a
71
+ * word of their own and every one of them read `jiào`. This moves 158 to
72
+ * `jiāo` and is wrong on three: 统一教创始人, 方法教深思 and 做到了教政分离,
73
+ * where a nominal compound takes an object's shape, the last of them because
74
+ * 做到了 is a word and so the 了 in front is not a particle to look at. Of the
75
+ * 21 it leaves, 8 are right — 诸教中, 教外别传, 董教总, 风教, 教青局 twice, a
76
+ * 教室 reached through a bad split, and a 教 used as a bare noun — and 13 are
77
+ * misses, where jieba tags the object something this does not name.
78
+ */
79
+ export declare const TEACHING_JIAO: EdgeRule;
80
+ /**
81
+ * A 量词 with a number in front of it does not start the next word.
82
+ *
83
+ * 三个人 is three people and came out `sān gèrén`, three *personals*, because
84
+ * 个人 is a common noun and nothing weighed it against the 个 belonging to the
85
+ * 三 in front of it. The same swallow takes 一杯水 into 杯水, 两家俱乐部 into
86
+ * 家俱 and 一天天 into 天天; digits reach it too, since a run decoded after a
87
+ * number sees that number — see `decodeRun` — so 2个人 is the same case.
88
+ *
89
+ * What makes it decidable is that the dictionary tags the 量词: 个, 次, 天, 位,
90
+ * 杯 and 匹 are `q`, while the characters that only look like measure words in
91
+ * this position are not — 分 is `v`, 部 and 成 are `n`, 年 and 点 are `m`. So
92
+ * 五分钟, 三部分, 五成分, 五年级 and 三点钟 are untouched, and those are exactly
93
+ * the words a blanket rule would break. A word that is itself a 量词 is kept as
94
+ * well, since 分钟 and 点钟 are units rather than something being counted.
95
+ *
96
+ * Only the edge *starting at the 量词* is forbidden, so a number and its
97
+ * measure written solid survive whole where the dictionary has them: 一辈子 and
98
+ * 一会儿 are entries covering the number too, and the decode still reaches them.
99
+ *
100
+ * Measured over the same 88,866 lines of Tatoeba and zh.wikipedia the other
101
+ * rules were sized against, it forbids 561 edges across 144 shapes — 个人 203
102
+ * of them, then 杯水, 天一, 口气, 些小 and 家人 — and moves 53 decodes, since
103
+ * most of what it takes away the cost model was not going to choose anyway.
104
+ * Three of the 53 are wrong: 一批评, 这一名词 and 六七股灾, where the 一 and the
105
+ * 六七 count nothing and no tag says so. Exactly one reading changes over the
106
+ * corpus and it is a fix, 下了两天雨 having read 天雨 as `tiān yù`.
107
+ */
108
+ export declare const COUNTED_MEASURE: EdgeRule;
52
109
  /**
53
110
  * The rules the lattice decode applies, in order.
54
111
  */
@@ -1 +1 @@
1
- {"version":3,"file":"reading-rules.d.ts","sourceRoot":"","sources":["../../src/decode/reading-rules.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,KAAK,QAAQ,EAId,MAAM,YAAY,CAAC;AAgCpB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,QAAQ,EAAE,QActB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,cAAc,EAAE,QAY5B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,SAAS,QAAQ,EAA+B,CAAC"}
1
+ {"version":3,"file":"reading-rules.d.ts","sourceRoot":"","sources":["../../src/decode/reading-rules.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,KAAK,QAAQ,EAId,MAAM,YAAY,CAAC;AAgCpB;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,QAAQ,EAAE,QActB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,cAAc,EAAE,QAY5B,CAAC;AAqDF;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,aAAa,EAAE,QAiB3B,CAAC;AAsCF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,eAAO,MAAM,eAAe,EAAE,QAuB7B,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,EAAE,SAAS,QAAQ,EAK5C,CAAC"}
@@ -1,3 +1,5 @@
1
+ import { QUANTITY_CHARACTERS } from "../numerals/characters.js";
2
+ import { toCharacters } from "../script/characters.js";
1
3
  import { tagOf, wordEndingAt, wordStartingAt, } from "./rules.js";
2
4
  /**
3
5
  * Whether a syllable is `děi`, the modal reading of 得.
@@ -100,8 +102,183 @@ export const ATTESTED_ERHUA = {
100
102
  return (reading?.at(-1)?.erhua ?? false) ? "forbid" : "keep";
101
103
  },
102
104
  };
105
+ /**
106
+ * Whether a syllable is `jiāo`, the verbal reading of 教.
107
+ */
108
+ function isJiao(syllable) {
109
+ return (syllable?.initial === "j" && syllable.final === "iao" && syllable.tone === 1);
110
+ }
111
+ /**
112
+ * What a 教 that is teaching governs: the thing or the person taught.
113
+ *
114
+ * A pronoun, a common noun, a name or one of the tags jieba gives a subject —
115
+ * 教我, 教英语, 教历史, 教孩子, 教玛丽. `ns` is deliberately absent, since the
116
+ * only 教 with a place tag after it over the corpus is 教青局, an office.
117
+ */
118
+ const TAUGHT_TAGS = new Set(["r", "n", "nz", "nr", "nt", "ng"]);
119
+ /**
120
+ * What follows a 教 that is teaching where no object does: aspect and the
121
+ * complement marker, which only a verb takes.
122
+ *
123
+ * 他教了三年书, 他教过我英语, 她教书教得很快乐. Matched as characters rather
124
+ * than through their tags, because what the dictionary has starting at a 得 is
125
+ * as likely to be 得很 as the particle itself.
126
+ */
127
+ const ASPECT = new Set(["了", "过", "過", "着", "著", "得"]);
128
+ /**
129
+ * The tag prefix jieba gives a particle, which nothing teaching can follow.
130
+ */
131
+ const PARTICLE_TAG = "u";
132
+ /**
133
+ * Whether the 教 at a position is teaching something to somebody.
134
+ *
135
+ * The object is what says so, which is the same shape the modal 得 rule takes:
136
+ * a 教 with a pronoun, a noun or a name after it is governing it, and a 教 with
137
+ * 了, 过, 着 or 得 after it is a verb whatever follows that. A particle in front
138
+ * of it rules it out, since a verb does not follow one.
139
+ */
140
+ function isTeachingAt(context, at) {
141
+ if (tagOf(context, wordEndingAt(context, at)).startsWith(PARTICLE_TAG)) {
142
+ return false;
143
+ }
144
+ return (ASPECT.has(context.characters[at + 1] ?? "") ||
145
+ TAUGHT_TAGS.has(tagOf(context, wordStartingAt(context, at + 1))));
146
+ }
147
+ /**
148
+ * 教 read as `jiāo` where it is teaching rather than a religion.
149
+ *
150
+ * The dictionary stores 教 as `jiào` with `jiāo` as an alternate, and `jiào` is
151
+ * right for the compounds — 教育, 教师, 宗教, 主教 — which are words and reach
152
+ * their reading through the word. What is left is the 教 that stands as a word
153
+ * of its own, and that one is the verb: 他在北京大学教了三年书 came out
154
+ * `jiàole sān nián shū`, and 我教英语, 谁教你法语 and 她教我如何游泳 with it.
155
+ * 教书 is a word and was always `jiāo shū`; nothing carried that across a 了.
156
+ *
157
+ * See {@link isTeachingAt} for what decides it. Forcing the single-character
158
+ * edge is not enough on its own, because a reading spanning two characters
159
+ * carries its own 教 into the position: 王老师教我们汉语 read `jiào` off 师教,
160
+ * and 来教我 off 来教. Those are pairs the dictionary holds with no part of
161
+ * speech and at the cost of a word nothing has ever counted — a reading rather
162
+ * than a word — so a *tagged* word ending in 教 is left alone, which is every
163
+ * one that matters: 任教, 宗教, 主教, 佛教, 传教, 执教, 请教, 家教.
164
+ *
165
+ * Measured over 88,866 lines of Tatoeba and zh.wikipedia, 181 教 decode as a
166
+ * word of their own and every one of them read `jiào`. This moves 158 to
167
+ * `jiāo` and is wrong on three: 统一教创始人, 方法教深思 and 做到了教政分离,
168
+ * where a nominal compound takes an object's shape, the last of them because
169
+ * 做到了 is a word and so the 了 in front is not a particle to look at. Of the
170
+ * 21 it leaves, 8 are right — 诸教中, 教外别传, 董教总, 风教, 教青局 twice, a
171
+ * 教室 reached through a bad split, and a 教 used as a bare noun — and 13 are
172
+ * misses, where jieba tags the object something this does not name.
173
+ */
174
+ export const TEACHING_JIAO = {
175
+ name: "teaching-jiao",
176
+ verdictFor: (context) => {
177
+ const { edge } = context;
178
+ if (edge.text === "教") {
179
+ return isJiao(edge.reading[0]) && isTeachingAt(context, edge.from)
180
+ ? "force"
181
+ : "keep";
182
+ }
183
+ return edge.partOfSpeech === "" &&
184
+ edge.text.endsWith("教") &&
185
+ edge.reading.length === edge.to - edge.from &&
186
+ !isJiao(edge.reading.at(-1)) &&
187
+ isTeachingAt(context, edge.to - 1)
188
+ ? "forbid"
189
+ : "keep";
190
+ },
191
+ };
192
+ /**
193
+ * The tag the dictionary gives a 量词.
194
+ */
195
+ const MEASURE = "q";
196
+ /**
197
+ * The mark that makes the number after it a position rather than a count.
198
+ *
199
+ * 第三集团军 is the Third Army Group and not three 集团军, so the 集 the
200
+ * dictionary tags `q` is counting nothing and 集团军 is a word like any other.
201
+ */
202
+ const ORDINAL = "第";
203
+ /**
204
+ * Where the number counting the character at a position starts, or undefined
205
+ * where nothing counts it.
206
+ *
207
+ * Two things stop a numeral in front of a 量词 from counting it. An ordinal
208
+ * names a position, so 第十级别 is the tenth 级别 rather than ten 级; and a
209
+ * numeral inside a longer word belongs to that word, so the 一 of 唯一道路 is
210
+ * counting no 道 and the 二 of 十二月 no 月.
211
+ */
212
+ function countedFrom(context, at) {
213
+ const { characters } = context;
214
+ let from = at;
215
+ while (from > 0 && QUANTITY_CHARACTERS.has(characters[from - 1] ?? "")) {
216
+ from--;
217
+ }
218
+ if (from === at || characters[from - 1] === ORDINAL) {
219
+ return undefined;
220
+ }
221
+ const before = wordEndingAt(context, at);
222
+ const held = before === undefined ? at : at - toCharacters(before).length;
223
+ return held < from ? undefined : from;
224
+ }
225
+ /**
226
+ * A 量词 with a number in front of it does not start the next word.
227
+ *
228
+ * 三个人 is three people and came out `sān gèrén`, three *personals*, because
229
+ * 个人 is a common noun and nothing weighed it against the 个 belonging to the
230
+ * 三 in front of it. The same swallow takes 一杯水 into 杯水, 两家俱乐部 into
231
+ * 家俱 and 一天天 into 天天; digits reach it too, since a run decoded after a
232
+ * number sees that number — see `decodeRun` — so 2个人 is the same case.
233
+ *
234
+ * What makes it decidable is that the dictionary tags the 量词: 个, 次, 天, 位,
235
+ * 杯 and 匹 are `q`, while the characters that only look like measure words in
236
+ * this position are not — 分 is `v`, 部 and 成 are `n`, 年 and 点 are `m`. So
237
+ * 五分钟, 三部分, 五成分, 五年级 and 三点钟 are untouched, and those are exactly
238
+ * the words a blanket rule would break. A word that is itself a 量词 is kept as
239
+ * well, since 分钟 and 点钟 are units rather than something being counted.
240
+ *
241
+ * Only the edge *starting at the 量词* is forbidden, so a number and its
242
+ * measure written solid survive whole where the dictionary has them: 一辈子 and
243
+ * 一会儿 are entries covering the number too, and the decode still reaches them.
244
+ *
245
+ * Measured over the same 88,866 lines of Tatoeba and zh.wikipedia the other
246
+ * rules were sized against, it forbids 561 edges across 144 shapes — 个人 203
247
+ * of them, then 杯水, 天一, 口气, 些小 and 家人 — and moves 53 decodes, since
248
+ * most of what it takes away the cost model was not going to choose anyway.
249
+ * Three of the 53 are wrong: 一批评, 这一名词 and 六七股灾, where the 一 and the
250
+ * 六七 count nothing and no tag says so. Exactly one reading changes over the
251
+ * corpus and it is a fix, 下了两天雨 having read 天雨 as `tiān yù`.
252
+ */
253
+ export const COUNTED_MEASURE = {
254
+ name: "counted-measure",
255
+ verdictFor: (context) => {
256
+ const { characters, edge } = context;
257
+ // A reading of a different length from its span says something about how
258
+ // the characters are read rather than only about where they break: 份儿 is
259
+ // `fènr` over two characters, and taking the edge away would take the 儿化
260
+ // with it.
261
+ if (edge.to - edge.from < 2 ||
262
+ edge.reading.length !== edge.to - edge.from) {
263
+ return "keep";
264
+ }
265
+ const counted = countedFrom(context, edge.from);
266
+ if (counted === undefined) {
267
+ return "keep";
268
+ }
269
+ return tagOf(context, characters[edge.from]) === MEASURE &&
270
+ edge.partOfSpeech !== MEASURE
271
+ ? "forbid"
272
+ : "keep";
273
+ },
274
+ };
103
275
  /**
104
276
  * The rules the lattice decode applies, in order.
105
277
  */
106
- export const READING_RULES = [MODAL_DE, ATTESTED_ERHUA];
278
+ export const READING_RULES = [
279
+ MODAL_DE,
280
+ TEACHING_JIAO,
281
+ ATTESTED_ERHUA,
282
+ COUNTED_MEASURE,
283
+ ];
107
284
  //# sourceMappingURL=reading-rules.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"reading-rules.js","sourceRoot":"","sources":["../../src/decode/reading-rules.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,KAAK,EACL,YAAY,EACZ,cAAc,GACf,MAAM,YAAY,CAAC;AAEpB;;;;;GAKG;AACH,SAA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,CAAC,MAAM,CAAC;IACb,CAAC;CACF,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,aAAa,GAAwB;IAChD,QAAQ;IACR,aAAa;IACb,cAAc;IACd,eAAe;CAChB,CAAC"}
@@ -0,0 +1,156 @@
1
+ import { type NumberStyle } from "../decode/convert.js";
2
+ import type { SandhiOptions } from "../decode/sandhi.js";
3
+ import type { Dictionary } from "../dictionary/dictionary.js";
4
+ import type { Locale } from "../script/script.js";
5
+ /**
6
+ * Whether a slug carries its tones.
7
+ *
8
+ * `numbers` writes them as digits — 中文 is `zhong1wen2` — and `none` leaves
9
+ * them off. Tones are on by default because dropping them collides: there are
10
+ * roughly 400 toneless syllables against 1300 toned ones, so a toneless slug
11
+ * runs 树, 书 and 输 together as `shu`. They are not a guarantee, though —
12
+ * 权利 and 权力 are both `quan2li4`, and only {@link SlugOptions.hash} tells
13
+ * those apart.
14
+ */
15
+ export type SlugTones = "numbers" | "none";
16
+ /**
17
+ * Whether the syllables inside one word are separated.
18
+ *
19
+ * `join` writes a word as a word — 中文 is `zhong1wen2` — which is what 正词法
20
+ * asks for and what a reader expects in a URL. `separate` cuts every syllable
21
+ * apart, `zhong1-wen2`, for a caller matching a slug syllable by syllable.
22
+ */
23
+ export type SlugSyllables = "join" | "separate";
24
+ /**
25
+ * How ü is written where the alphabet has no room for it.
26
+ *
27
+ * `v` is the input-method convention and keeps 绿 `lv4` apart from 路 `lu4`;
28
+ * `u` is prettier and merges them.
29
+ */
30
+ export type SlugUmlaut = "v" | "u";
31
+ /**
32
+ * How a slug is built.
33
+ */
34
+ export interface SlugOptions {
35
+ /** Whether tones are written. Defaults to `numbers`. */
36
+ readonly tones?: SlugTones;
37
+ /**
38
+ * What goes between words. Defaults to `-`.
39
+ *
40
+ * Written as given rather than checked, because the separator is the one
41
+ * place a caller decides what kind of identifier this is: `-` for a URL, `_`
42
+ * for a name in code, and the empty string for a key to match against.
43
+ */
44
+ readonly separator?: string;
45
+ /** Whether the syllables within a word are separated. Defaults to `join`. */
46
+ readonly syllables?: SlugSyllables;
47
+ /** How ü is written. Defaults to `v`. */
48
+ readonly umlaut?: SlugUmlaut;
49
+ /**
50
+ * What to do with the digits in a text. Defaults to `keep`.
51
+ *
52
+ * The opposite of what a conversion defaults to, and deliberately: a slug for
53
+ * iPhone 15 评测 should be `iphone-15-ping2ce4` rather than
54
+ * `iphone-shi2wu3-ping2ce4`, because the digits are how anyone would look for
55
+ * it. `read` says them, for a caller that wants the text spoken.
56
+ */
57
+ readonly numbers?: NumberStyle;
58
+ /**
59
+ * A short hash of the source text on the end. Defaults to none.
60
+ *
61
+ * `true` for {@link DEFAULT_HASH_LENGTH} characters, or a length of your own,
62
+ * up to {@link LONGEST_HASH}. See {@link hashOf} for what is hashed and why
63
+ * it is the hanzi rather than the pinyin.
64
+ *
65
+ * Unconditional rather than only where two slugs collide: a function that
66
+ * cannot see the rest of your corpus cannot know that they did. It narrows
67
+ * collisions rather than closing them, and checking the slug you generated
68
+ * against the ones you have stored is still yours to do.
69
+ */
70
+ readonly hash?: number | boolean;
71
+ /**
72
+ * The longest the slug may be, counting any hash. Defaults to no limit.
73
+ *
74
+ * Cut at a word boundary, so a limit lands between words rather than through
75
+ * the middle of one. A single word longer than the limit is cut where the
76
+ * limit falls, since there is nowhere else to cut it, and the hash is never
77
+ * what gets dropped.
78
+ */
79
+ readonly maxLength?: number;
80
+ /**
81
+ * What to write where a text slugs to nothing. Defaults to the empty string.
82
+ *
83
+ * 《》! has no letters in it, and neither does an empty text.
84
+ */
85
+ readonly fallback?: string;
86
+ /** Which reading standard to use. Defaults to `zh-CN`. */
87
+ readonly locale?: Locale;
88
+ /** Which tone sandhi to apply. Defaults to what a conversion applies. */
89
+ readonly sandhi?: SandhiOptions;
90
+ }
91
+ /**
92
+ * How many characters {@link SlugOptions.hash} writes when simply asked for.
93
+ *
94
+ * Four base-36 characters is 1.7 million values, which is far more than it
95
+ * looks: two texts only collide when their slugs *and* their hashes match, so
96
+ * the suffix is only ever telling apart the handful of texts sharing one slug,
97
+ * not the whole corpus.
98
+ */
99
+ export declare const DEFAULT_HASH_LENGTH = 4;
100
+ /**
101
+ * The longest hash there is anything to fill.
102
+ *
103
+ * {@link hashOf} is 32 bits wide, and 36⁷ is the last power of 36 under 2³²:
104
+ * asking for an eighth character would add a place that some values can never
105
+ * reach, which looks like more uniqueness while being none.
106
+ */
107
+ export declare const LONGEST_HASH = 7;
108
+ /**
109
+ * A 32-bit hash of a text, written in the letters and digits a slug allows.
110
+ *
111
+ * **The source text is what is hashed, not the pinyin.** Two texts needing a
112
+ * hash to tell them apart are, by definition, ones the pinyin already ran
113
+ * together: 权利 and 权力 are both `quan2li4`, and hashing that would give them
114
+ * the same suffix and settle nothing. Hashing the hanzi also means the suffix
115
+ * does not move when a later release reads a word differently, so a slug's tail
116
+ * is the stable half of it.
117
+ *
118
+ * FNV-1a with murmur3's finalizer, which is neither cryptographic nor trying to
119
+ * be: nothing here resists an attacker, and all that is being asked of it is
120
+ * that two texts a reader would call different land on different suffixes.
121
+ * Written out rather than reached for because the package has no runtime
122
+ * dependencies and the core imports no Node built-ins, and `crypto.subtle` —
123
+ * the one hash a browser ships — is asynchronous, which this is not.
124
+ *
125
+ * Normalised first, so that text typed as a decomposed sequence hashes as the
126
+ * same text once composed. 简体 and 繁體 are deliberately *not* folded together:
127
+ * a caller who wrote 头发 and 頭髮 separately meant them separately.
128
+ */
129
+ export declare function hashOf(text: string, length: number): string;
130
+ /**
131
+ * Convert hanzi to a URL-safe slug.
132
+ *
133
+ * 我想学中文。 is `wo3-xiang3-xue2-zhong1wen2`, and the reason to ask a
134
+ * dictionary for it rather than a string is everything a slugifier working
135
+ * after the fact cannot recover: 银行 is `yin2hang2` and not `yin2xing2`
136
+ * because the word was decoded rather than the characters looked up one at a
137
+ * time; the hyphens fall between words because 正词法 grouping put them there;
138
+ * and 西安 is `xi-an` rather than `xian` — a different word — because the
139
+ * syllable boundary was still known when the slug was cut.
140
+ *
141
+ * Everything that is not Han is folded into the same alphabet and kept, so
142
+ * iPhone 15 评测 is `iphone-15-ping2ce4`, and everything else — punctuation,
143
+ * emoji, marks of any script — becomes a boundary between words.
144
+ *
145
+ * The options generalise it past URLs: a separator of `_` writes a name in
146
+ * code, and one of the empty string with `tones: "none"` writes a key to match
147
+ * a search box against, where someone typing `zhongwen` should find 中文. See
148
+ * the slug page in `docs/` for those written out.
149
+ *
150
+ * **A slug is not unique and this cannot make it one.** Tones narrow the
151
+ * collisions, {@link SlugOptions.hash} narrows them much further, and neither
152
+ * is a guarantee; a slug that has to be unique still has to be checked against
153
+ * the ones already stored.
154
+ */
155
+ export declare function slug(dictionary: Dictionary, text: string, options?: SlugOptions): string;
156
+ //# sourceMappingURL=slug.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"slug.d.ts","sourceRoot":"","sources":["../../src/format/slug.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,WAAW,EACjB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAE9D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAGlD;;;;;;;;;GASG;AACH,MAAM,MAAM,SAAS,GAAG,SAAS,GAAG,MAAM,CAAC;AAE3C;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,UAAU,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,GAAG,GAAG,GAAG,CAAC;AAEnC;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,wDAAwD;IACxD,QAAQ,CAAC,KAAK,CAAC,EAAE,SAAS,CAAC;IAC3B;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,6EAA6E;IAC7E,QAAQ,CAAC,SAAS,CAAC,EAAE,aAAa,CAAC;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AACzC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -35,6 +35,8 @@ export { decodeReadings, decodeRun, decodeRunScored, decodeSpacing, } from "./de
35
35
  export { convertToHtml, toHtml } from "./format/html.js";
36
36
  export { convertToWadeGiles, toTranscription, type TranscriptionOptions, type WriteWord, } from "./format/transcription.js";
37
37
  export type { HtmlOptions } from "./format/html.js";
38
+ export { slug } from "./format/slug.js";
39
+ export type { SlugOptions, SlugSyllables, SlugTones, SlugUmlaut, } from "./format/slug.js";
38
40
  export { decodeGreedily } from "./decode/greedy.js";
39
41
  export { allEdges, buildLattice, cutPoints, READING_CHARGE, } from "./decode/lattice.js";
40
42
  export type { Lattice, LatticeEdge } from "./decode/lattice.js";
@@ -42,13 +44,13 @@ export { isSettled, projectReadings, settledUnits, unitsOf, } from "./decode/loc
42
44
  export type { ReadingProjection, ReadingUnit } from "./decode/locking.js";
43
45
  export { applyEdgeRules, tagOf, wordEndingAt, wordStartingAt, } from "./decode/rules.js";
44
46
  export type { EdgeContext, EdgeRule, EdgeVerdict } from "./decode/rules.js";
45
- export { ATTESTED_ERHUA, MODAL_DE, READING_RULES, } from "./decode/reading-rules.js";
47
+ export { ATTESTED_ERHUA, COUNTED_MEASURE, MODAL_DE, READING_RULES, TEACHING_JIAO, } from "./decode/reading-rules.js";
46
48
  export { readingCost, shortestPath, spacingCost } from "./decode/viterbi.js";
47
49
  export type { CostOf } from "./decode/viterbi.js";
48
50
  export type { DecodedWord, ScoredWord, WordSeparator } from "./decode/word.js";
49
51
  export { cardinalHanzi, LARGEST_CARDINAL } from "./numerals/cardinal.js";
50
52
  export type { CardinalOptions } from "./numerals/cardinal.js";
51
- export { DIGIT_CHARACTERS, numeralSyllable, UNIT_VALUES, yaoSyllable, } from "./numerals/characters.js";
53
+ export { DIGIT_CHARACTERS, numeralSyllable, QUANTITY_CHARACTERS, UNIT_VALUES, yaoSyllable, } from "./numerals/characters.js";
52
54
  export { fractionHanzi, numeralHanzi, percentHanzi, readNumeral, readNumeralHanzi, } from "./numerals/numerals.js";
53
55
  export type { NumeralOptions, NumeralStyle } from "./numerals/numerals.js";
54
56
  export { isBopomofo, readBopomofo, writeBopomofo, writeBopomofoWord, } from "./transcription/bopomofo.js";