@kensio/pinyinjs 1.2.0 → 1.2.2
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.
- package/CHANGELOG.md +111 -16
- package/README.md +35 -35
- package/data/core.entries +89 -89
- package/data/full.entries +257 -257
- package/data/manifest.json +6 -6
- package/data/standard.entries +102 -102
- package/dist/decode/convert.d.ts.map +1 -1
- package/dist/decode/convert.js +91 -40
- package/dist/decode/convert.js.map +1 -1
- package/dist/decode/decode.d.ts +12 -2
- package/dist/decode/decode.d.ts.map +1 -1
- package/dist/decode/decode.js +64 -7
- package/dist/decode/decode.js.map +1 -1
- package/dist/decode/reading-rules.d.ts +57 -0
- package/dist/decode/reading-rules.d.ts.map +1 -1
- package/dist/decode/reading-rules.js +178 -1
- package/dist/decode/reading-rules.js.map +1 -1
- package/dist/dictionary/assertions.d.ts.map +1 -1
- package/dist/dictionary/assertions.js +20 -0
- package/dist/dictionary/assertions.js.map +1 -1
- package/dist/dictionary/locale.d.ts +55 -0
- package/dist/dictionary/locale.d.ts.map +1 -0
- package/dist/dictionary/locale.js +323 -0
- package/dist/dictionary/locale.js.map +1 -0
- package/dist/dictionary/merge.d.ts +3 -1
- package/dist/dictionary/merge.d.ts.map +1 -1
- package/dist/dictionary/merge.js +54 -10
- package/dist/dictionary/merge.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/numerals/cardinal.d.ts +15 -1
- package/dist/numerals/cardinal.d.ts.map +1 -1
- package/dist/numerals/cardinal.js +8 -1
- package/dist/numerals/cardinal.js.map +1 -1
- package/dist/numerals/characters.d.ts +9 -0
- package/dist/numerals/characters.d.ts.map +1 -1
- package/dist/numerals/characters.js +18 -0
- package/dist/numerals/characters.js.map +1 -1
- package/dist/numerals/numerals.d.ts.map +1 -1
- package/dist/numerals/numerals.js +12 -4
- package/dist/numerals/numerals.js.map +1 -1
- package/dist/numerals/text.d.ts +23 -9
- package/dist/numerals/text.d.ts.map +1 -1
- package/dist/numerals/text.js +93 -9
- package/dist/numerals/text.js.map +1 -1
- package/dist/orthography/capitals.d.ts.map +1 -1
- package/dist/orthography/capitals.js +25 -8
- package/dist/orthography/capitals.js.map +1 -1
- package/package.json +1 -1
package/dist/numerals/text.js
CHANGED
|
@@ -57,6 +57,46 @@ const SPOKEN_ZERO_BELOW = 10;
|
|
|
57
57
|
* 12:00 is 十二点 with the 二 inside a larger number.
|
|
58
58
|
*/
|
|
59
59
|
const LIANG_HOUR = 2;
|
|
60
|
+
/**
|
|
61
|
+
* The characters a lone 2 in front of them labels rather than counts.
|
|
62
|
+
*
|
|
63
|
+
* A 2 standing immediately in front of what it counts is 两 — 两个, 两人, 两岁,
|
|
64
|
+
* 两次, 两天 — and the measure words it can count with are an open list, so 两
|
|
65
|
+
* is the default and this is the exception. What is left is the positions a
|
|
66
|
+
* digit *names*: 2月 is February, 2日 and 2号 are the second of the month, 2楼
|
|
67
|
+
* is the second floor, 2路 is the number 2 bus, 2班 is the second class and 2期
|
|
68
|
+
* is the second phase. A named position is 二.
|
|
69
|
+
*
|
|
70
|
+
* The traditional forms are listed beside the simplified ones, since the text
|
|
71
|
+
* being converted may be in either script.
|
|
72
|
+
*/
|
|
73
|
+
const LABELLED = new Set([
|
|
74
|
+
"月",
|
|
75
|
+
"日",
|
|
76
|
+
"号",
|
|
77
|
+
"號",
|
|
78
|
+
"楼",
|
|
79
|
+
"樓",
|
|
80
|
+
"路",
|
|
81
|
+
"班",
|
|
82
|
+
"期",
|
|
83
|
+
]);
|
|
84
|
+
/**
|
|
85
|
+
* The units a lone 2 in front of them keeps 二 for, as the number itself does.
|
|
86
|
+
*
|
|
87
|
+
* 20 是二十 and 200 是二百, so 2十 and 2百 are written the same way: whether the
|
|
88
|
+
* unit is a digit or a character does not change how the number is said. 千, 万
|
|
89
|
+
* and 亿 are left to `CardinalOptions.liang` for exactly the same reason, which
|
|
90
|
+
* is why 2万 is 两万 as 20,000 already is.
|
|
91
|
+
*/
|
|
92
|
+
const SMALL_UNITS = new Set(["十", "百"]);
|
|
93
|
+
/**
|
|
94
|
+
* The mark that makes the number after it an ordinal: 第2次 is 第二次.
|
|
95
|
+
*
|
|
96
|
+
* An ordinal names a position however ordinary the measure word after it is,
|
|
97
|
+
* so it takes 二 where the same 2次 on its own would be 两次.
|
|
98
|
+
*/
|
|
99
|
+
const ORDINAL = "第";
|
|
60
100
|
/**
|
|
61
101
|
* How many digits a year is written with, when it is a year and not a count.
|
|
62
102
|
*
|
|
@@ -80,14 +120,46 @@ function styleFor(digits, following) {
|
|
|
80
120
|
? "digits"
|
|
81
121
|
: "cardinal";
|
|
82
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* Whether a number is standing in front of something it counts.
|
|
125
|
+
*
|
|
126
|
+
* Only ever true of a number the following Han actually touches: in 2、3个 the
|
|
127
|
+
* 个 counts the 3 and there is a 、 between it and the 2, and in 2%的 the sign
|
|
128
|
+
* is what the digits belong to. `before` and `after` are the characters the
|
|
129
|
+
* digit run sits between, and a digit run with anything but Han after it is not
|
|
130
|
+
* in front of a 量词 at all.
|
|
131
|
+
*/
|
|
132
|
+
function isInFrontOfCount(before, after, following) {
|
|
133
|
+
return (after === "" &&
|
|
134
|
+
following !== "" &&
|
|
135
|
+
!LABELLED.has(following) &&
|
|
136
|
+
!SMALL_UNITS.has(following) &&
|
|
137
|
+
before !== ORDINAL);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Where a decimal breaks into words, or undefined where it is not one.
|
|
141
|
+
*
|
|
142
|
+
* The counted part keeps its grouping — 75.5 is `qīshíwǔ diǎn wǔ`, one word for
|
|
143
|
+
* the quantity, which is the same word 75 on its own is — and everything from
|
|
144
|
+
* the 点 onwards is read digit by digit, so the point and each digit after it
|
|
145
|
+
* stand alone: 3.14 is `sān diǎn yī sì`.
|
|
146
|
+
*/
|
|
147
|
+
function decimalWords(hanzi) {
|
|
148
|
+
const characters = toCharacters(hanzi);
|
|
149
|
+
const point = characters.indexOf("点");
|
|
150
|
+
if (point === -1) {
|
|
151
|
+
return undefined;
|
|
152
|
+
}
|
|
153
|
+
return [point, ...characters.slice(point).map(() => 1)];
|
|
154
|
+
}
|
|
83
155
|
/**
|
|
84
156
|
* Read one number, with the sign that may follow it.
|
|
85
157
|
*/
|
|
86
|
-
function readNumber(digits, isPercent, style, options) {
|
|
158
|
+
function readNumber(digits, isPercent, style, isCounting, options) {
|
|
87
159
|
// A writer's thousands separators are not part of the number; nothing is
|
|
88
160
|
// said for them.
|
|
89
161
|
const value = digits.replaceAll(",", "");
|
|
90
|
-
const settings = { ...options, style };
|
|
162
|
+
const settings = { ...options, style, counts: isCounting };
|
|
91
163
|
const hanzi = isPercent
|
|
92
164
|
? percentHanzi(value, settings)
|
|
93
165
|
: numeralHanzi(value, settings);
|
|
@@ -95,7 +167,16 @@ function readNumber(digits, isPercent, style, options) {
|
|
|
95
167
|
if (hanzi === undefined || reading === undefined) {
|
|
96
168
|
return undefined;
|
|
97
169
|
}
|
|
98
|
-
|
|
170
|
+
// Digits read out one at a time are not a word at all, so there is nothing
|
|
171
|
+
// for a point in them to break up.
|
|
172
|
+
const words = style === "digits" ? undefined : decimalWords(hanzi);
|
|
173
|
+
return {
|
|
174
|
+
text: digits,
|
|
175
|
+
reading,
|
|
176
|
+
hanzi,
|
|
177
|
+
style,
|
|
178
|
+
...(words !== undefined && { words }),
|
|
179
|
+
};
|
|
99
180
|
}
|
|
100
181
|
/**
|
|
101
182
|
* A stretch that stays exactly as it was written.
|
|
@@ -177,15 +258,16 @@ function readTime(text, from, options) {
|
|
|
177
258
|
/**
|
|
178
259
|
* Split a stretch of non-Han text into what is read and what is not.
|
|
179
260
|
*
|
|
180
|
-
* `
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
261
|
+
* `context` is the Han either side of this stretch, which is all a number has
|
|
262
|
+
* to go on: 年 makes 1998 a year, 个 makes 3 a count and 个 also makes 2 两.
|
|
263
|
+
* Where there is nothing around it, or nothing that decides, the number is
|
|
264
|
+
* counted.
|
|
184
265
|
*
|
|
185
266
|
* Text that is not a number is returned unread rather than dropped, so the
|
|
186
267
|
* segments always concatenate back to the input.
|
|
187
268
|
*/
|
|
188
|
-
export function readNumbersIn(text,
|
|
269
|
+
export function readNumbersIn(text, context, options = {}) {
|
|
270
|
+
const { following, preceding } = context;
|
|
189
271
|
const segments = [];
|
|
190
272
|
let at = 0;
|
|
191
273
|
for (const match of text.matchAll(NUMBER)) {
|
|
@@ -212,7 +294,9 @@ export function readNumbersIn(text, following, options = {}) {
|
|
|
212
294
|
// What decides the style is whatever comes next: the sign if there is one,
|
|
213
295
|
// otherwise the first character of the following Han.
|
|
214
296
|
const style = styleFor(digits, isPercent ? "" : following);
|
|
215
|
-
const
|
|
297
|
+
const before = from === 0 ? preceding : (text[from - 1] ?? "");
|
|
298
|
+
const isCounting = isInFrontOfCount(before, after, following);
|
|
299
|
+
const read = readNumber(digits, isPercent, style, isCounting, options);
|
|
216
300
|
if (read === undefined) {
|
|
217
301
|
continue;
|
|
218
302
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../src/numerals/text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAsB,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAEvD,OAAO,EACL,YAAY,EAGZ,YAAY,EACZ,gBAAgB,GACjB,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"text.js","sourceRoot":"","sources":["../../src/numerals/text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAsB,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAEvD,OAAO,EACL,YAAY,EAGZ,YAAY,EACZ,gBAAgB,GACjB,MAAM,eAAe,CAAC;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OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC;AAClE,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capitals.d.ts","sourceRoot":"","sources":["../../src/orthography/capitals.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"capitals.d.ts","sourceRoot":"","sources":["../../src/orthography/capitals.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,QAAQ,GAAG,MAAM,CAAC;AAmCtD;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAKhD;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAGxD;AAED;;;;;GAKG;AACH,wBAAgB,uBAAuB,CACrC,KAAK,EAAE,SAAS,MAAM,EAAE,GACvB,SAAS,MAAM,EAAE,CAEnB"}
|
|
@@ -1,18 +1,35 @@
|
|
|
1
|
+
import { toCharacters } from "../script/characters.js";
|
|
1
2
|
import { rewriteParts } from "./parts.js";
|
|
2
3
|
/**
|
|
3
4
|
* Punctuation a sentence ends with, in either script.
|
|
4
5
|
*/
|
|
5
6
|
const SENTENCE_ENDINGS = new Set([".", "!", "?", "。", "!", "?"]);
|
|
7
|
+
/**
|
|
8
|
+
* A digit either side of a full stop, which is what makes it a decimal point.
|
|
9
|
+
*/
|
|
10
|
+
const DIGIT = /\d/u;
|
|
11
|
+
/**
|
|
12
|
+
* Whether a character ends a sentence where it stands.
|
|
13
|
+
*
|
|
14
|
+
* A full stop with a digit either side of it is the point of a decimal rather
|
|
15
|
+
* than the end of anything: 75.5元 is a price and not two sentences, so the
|
|
16
|
+
* stop in it neither makes the text a sentence nor starts a new one. Only the
|
|
17
|
+
* half-width stop can be a decimal point; 。 is never one.
|
|
18
|
+
*/
|
|
19
|
+
function isEnding(character, characters, at) {
|
|
20
|
+
if (!SENTENCE_ENDINGS.has(character)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return !(character === "." &&
|
|
24
|
+
DIGIT.test(characters[at - 1] ?? "") &&
|
|
25
|
+
DIGIT.test(characters[at + 1] ?? ""));
|
|
26
|
+
}
|
|
6
27
|
/**
|
|
7
28
|
* Whether text is punctuated as a sentence rather than quoted as a word.
|
|
8
29
|
*/
|
|
9
30
|
export function isSentence(text) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return false;
|
|
31
|
+
const characters = toCharacters(text);
|
|
32
|
+
return characters.some((character, at) => isEnding(character, characters, at));
|
|
16
33
|
}
|
|
17
34
|
/**
|
|
18
35
|
* Capitalise the first letter of a word.
|
|
@@ -52,12 +69,12 @@ export function capitaliseSentenceParts(parts) {
|
|
|
52
69
|
*/
|
|
53
70
|
const capitaliseCharacters = (characters) => {
|
|
54
71
|
let isPending = true;
|
|
55
|
-
return characters.map((character) => {
|
|
72
|
+
return characters.map((character, at) => {
|
|
56
73
|
if (isPending && character.toLowerCase() !== character.toUpperCase()) {
|
|
57
74
|
isPending = false;
|
|
58
75
|
return character.toUpperCase();
|
|
59
76
|
}
|
|
60
|
-
if (
|
|
77
|
+
if (isEnding(character, characters, at)) {
|
|
61
78
|
isPending = true;
|
|
62
79
|
}
|
|
63
80
|
return character;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capitals.js","sourceRoot":"","sources":["../../src/orthography/capitals.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,YAAY,EAAE,MAAM,YAAY,CAAC;AAYlE;;GAEG;AACH,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CA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