@chaffjs/lang-ja 0.30.0 → 0.32.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.
- package/dist/currency-amounts.d.ts +21 -0
- package/dist/currency-amounts.d.ts.map +1 -0
- package/dist/currency-amounts.js +73 -0
- package/dist/currency-amounts.js.map +1 -0
- package/dist/era-year.d.ts +18 -0
- package/dist/era-year.d.ts.map +1 -0
- package/dist/era-year.js +47 -0
- package/dist/era-year.js.map +1 -0
- package/dist/lexicons.d.ts +3 -0
- package/dist/lexicons.d.ts.map +1 -1
- package/dist/lexicons.js +12 -0
- package/dist/lexicons.js.map +1 -1
- package/dist/numbers.d.ts +1 -1
- package/dist/numbers.d.ts.map +1 -1
- package/dist/numbers.js +7 -4
- package/dist/numbers.js.map +1 -1
- package/dist/quantities.d.ts +2 -0
- package/dist/quantities.d.ts.map +1 -1
- package/dist/quantities.js +56 -27
- package/dist/quantities.js.map +1 -1
- package/dist/unit-case.d.ts +4 -0
- package/dist/unit-case.d.ts.map +1 -0
- package/dist/unit-case.js +11 -0
- package/dist/unit-case.js.map +1 -0
- package/lexicons/age-reference-label.yaml +12 -0
- package/lexicons/age-time-mark.yaml +9 -0
- package/lexicons/annual-holidays-label.yaml +10 -0
- package/lexicons/annual-holidays-word.yaml +23 -0
- package/lexicons/annual-pay-label.yaml +22 -0
- package/lexicons/annual-pay-word.yaml +44 -0
- package/lexicons/any-year-marker.yaml +18 -0
- package/lexicons/balance-column.yaml +43 -0
- package/lexicons/benefit-limit-word.yaml +9 -0
- package/lexicons/birth-date-label.yaml +6 -0
- package/lexicons/bmi-term.yaml +10 -0
- package/lexicons/bmi-unit.yaml +7 -0
- package/lexicons/calendar-era.yaml +6 -0
- package/lexicons/change-column.yaml +28 -0
- package/lexicons/change-target.yaml +4 -1
- package/lexicons/closing-balance-label.yaml +48 -0
- package/lexicons/company-form-foreign.yaml +45 -0
- package/lexicons/company-name-kana.yaml +91 -0
- package/lexicons/cumulative-table.yaml +35 -0
- package/lexicons/currency-column.yaml +13 -0
- package/lexicons/date-deadline-mark.yaml +10 -0
- package/lexicons/dated-column-label.yaml +14 -0
- package/lexicons/dated-pair-label.yaml +217 -0
- package/lexicons/dated-span-label.yaml +12 -0
- package/lexicons/deductible-basis.yaml +20 -0
- package/lexicons/deductible-word.yaml +8 -0
- package/lexicons/discount-rate-label.yaml +30 -0
- package/lexicons/due-date-label.yaml +4 -0
- package/lexicons/email-greeting.yaml +4 -0
- package/lexicons/email-sentence-end.yaml +10 -0
- package/lexicons/event-bill-word.yaml +5 -0
- package/lexicons/event-due-word.yaml +6 -0
- package/lexicons/event-payment-deadline-word.yaml +18 -0
- package/lexicons/event-payment-word.yaml +8 -0
- package/lexicons/event-start-label.yaml +9 -0
- package/lexicons/event-ticket-word.yaml +8 -0
- package/lexicons/event-trip-word.yaml +12 -0
- package/lexicons/fact-age-joiner.yaml +10 -0
- package/lexicons/fact-age-limit.yaml +7 -0
- package/lexicons/fact-age-mark.yaml +7 -0
- package/lexicons/fact-amount-condition.yaml +15 -0
- package/lexicons/fact-count-column.yaml +8 -0
- package/lexicons/fact-count-counter.yaml +7 -0
- package/lexicons/fact-count-not-whole.yaml +22 -0
- package/lexicons/fact-count-restating.yaml +9 -0
- package/lexicons/fact-count-separate-rows.yaml +6 -0
- package/lexicons/fact-count-whole.yaml +9 -0
- package/lexicons/fact-document-term.yaml +32 -0
- package/lexicons/fact-label-count-link.yaml +9 -0
- package/lexicons/fact-label-counter.yaml +25 -0
- package/lexicons/fact-label-drop.yaml +7 -0
- package/lexicons/fact-label-qualifier.yaml +8 -0
- package/lexicons/fact-rate-base.yaml +12 -0
- package/lexicons/fact-rate-kind.yaml +13 -0
- package/lexicons/fact-rate-period.yaml +16 -0
- package/lexicons/fact-separator.yaml +2 -0
- package/lexicons/fact-term-self.yaml +9 -0
- package/lexicons/fact-term-start.yaml +7 -0
- package/lexicons/formula-amount-word.yaml +12 -0
- package/lexicons/gpa-label.yaml +18 -0
- package/lexicons/grade-point.yaml +35 -0
- package/lexicons/heading-number-label.yaml +1 -2
- package/lexicons/installment-label.yaml +49 -0
- package/lexicons/installment-word.yaml +37 -0
- package/lexicons/interest-label.yaml +28 -0
- package/lexicons/item-name-column.yaml +9 -0
- package/lexicons/lab-result-column.yaml +23 -0
- package/lexicons/label-product-word.yaml +21 -0
- package/lexicons/line-amount-column.yaml +3 -0
- package/lexicons/meter-event.yaml +24 -0
- package/lexicons/meter-reading-label.yaml +29 -0
- package/lexicons/meter-unit.yaml +10 -0
- package/lexicons/name-char-reading.yaml +18 -0
- package/lexicons/name-lookalike-char.yaml +26 -0
- package/lexicons/net-amount-label.yaml +62 -0
- package/lexicons/net-amount-period.yaml +14 -0
- package/lexicons/net-gross-weight-column.yaml +20 -0
- package/lexicons/order-quantity-column.yaml +26 -0
- package/lexicons/organization-unit.yaml +25 -0
- package/lexicons/orthographic-variant-distinct.yaml +6 -0
- package/lexicons/orthographic-variant-whole-word.yaml +27 -0
- package/lexicons/overnight-work-mark.yaml +13 -0
- package/lexicons/pack-count-column.yaml +10 -0
- package/lexicons/pack-price-column.yaml +12 -0
- package/lexicons/per-unit-mark.yaml +1 -0
- package/lexicons/period-label.yaml +9 -0
- package/lexicons/period-multiple-label.yaml +12 -0
- package/lexicons/period-multiple-word.yaml +30 -0
- package/lexicons/period-part-after.yaml +8 -0
- package/lexicons/period-part-inline-joiner.yaml +5 -0
- package/lexicons/period-part-marker.yaml +9 -0
- package/lexicons/period-part-range.yaml +10 -0
- package/lexicons/period-part-word.yaml +5 -0
- package/lexicons/person-label.yaml +19 -0
- package/lexicons/person-title.yaml +22 -0
- package/lexicons/phone-label.yaml +9 -0
- package/lexicons/points-earned-column.yaml +20 -0
- package/lexicons/points-rate-joiner.yaml +9 -0
- package/lexicons/points-unit.yaml +7 -0
- package/lexicons/product-form.yaml +23 -0
- package/lexicons/quantity-column.yaml +22 -0
- package/lexicons/range-start-end.yaml +30 -0
- package/lexicons/rate-unit.yaml +8 -0
- package/lexicons/ratio-label.yaml +15 -0
- package/lexicons/ratio-term.yaml +13 -0
- package/lexicons/reference-bound-mark.yaml +14 -0
- package/lexicons/reference-column-heading.yaml +7 -0
- package/lexicons/reference-column.yaml +16 -0
- package/lexicons/reference-flag.yaml +23 -0
- package/lexicons/reference-label.yaml +123 -0
- package/lexicons/reference-unit-column.yaml +7 -0
- package/lexicons/reference-unit.yaml +80 -0
- package/lexicons/reporting-comparison.yaml +26 -0
- package/lexicons/reporting-current-not.yaml +10 -0
- package/lexicons/reporting-current.yaml +15 -0
- package/lexicons/reporting-forecast.yaml +18 -0
- package/lexicons/reporting-period-frame.yaml +19 -0
- package/lexicons/reporting-period-line.yaml +12 -0
- package/lexicons/reporting-period-not.yaml +23 -0
- package/lexicons/reporting-results-word.yaml +10 -0
- package/lexicons/reporting-year-frame.yaml +7 -0
- package/lexicons/request-offer.yaml +15 -0
- package/lexicons/retention-unit.yaml +2 -2
- package/lexicons/revision-column.yaml +15 -0
- package/lexicons/revision-direction.yaml +21 -0
- package/lexicons/serial-label.yaml +9 -0
- package/lexicons/service-charge-label.yaml +6 -0
- package/lexicons/share-amount-label.yaml +20 -0
- package/lexicons/share-column-rate-header.yaml +41 -0
- package/lexicons/shift-column.yaml +25 -0
- package/lexicons/table-scale-unit.yaml +19 -0
- package/lexicons/table-unit-caption.yaml +6 -0
- package/lexicons/tax-price-column.yaml +14 -0
- package/lexicons/tier-label.yaml +25 -0
- package/lexicons/tier-unit.yaml +9 -0
- package/lexicons/timed-pair-label.yaml +71 -0
- package/lexicons/total-label.yaml +12 -1
- package/lexicons/total-noun-listed.yaml +8 -0
- package/lexicons/total-noun-qualifier.yaml +8 -0
- package/lexicons/total-noun-unsummed.yaml +10 -0
- package/lexicons/total-period-qualifier.yaml +10 -0
- package/lexicons/transcript-column.yaml +17 -0
- package/lexicons/unit-area.yaml +6 -0
- package/lexicons/unit-concentration.yaml +71 -0
- package/lexicons/unit-price-column.yaml +21 -0
- package/lexicons/unit-volume.yaml +4 -0
- package/lexicons/use-amount-word.yaml +9 -0
- package/lexicons/variance-column.yaml +17 -0
- package/lexicons/vehicle-plate.yaml +7 -0
- package/lexicons/weekly-days-off.yaml +35 -0
- package/lexicons/year-span-joiner.yaml +13 -0
- package/package.json +2 -2
- package/src/currency-amounts.ts +96 -0
- package/src/era-year.ts +66 -0
- package/src/lexicons.ts +18 -0
- package/src/numbers.ts +7 -3
- package/src/quantities.ts +61 -29
- package/src/unit-case.ts +14 -0
package/src/quantities.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import type { Mention } from "chaffjs/plugin";
|
|
2
2
|
import { parseJapaneseNumber, toHalfWidth } from "./numbers.ts";
|
|
3
3
|
import { isReady, morphemes, type Morph } from "./pos.ts";
|
|
4
|
+
import { currencyAmounts, type AmountVocabulary } from "./currency-amounts.ts";
|
|
5
|
+
import { glossedYearReader } from "./era-year.ts";
|
|
6
|
+
import { loadCalendarEras, loadLexicons } from "./lexicons.ts";
|
|
7
|
+
import { escapeRegExp } from "./regexp.ts";
|
|
4
8
|
|
|
5
9
|
// 数量と日付。形態素が使えれば品詞で読み、使えなければ単位の表で読む。
|
|
6
10
|
|
|
7
|
-
|
|
11
|
+
/** 元号と元年の西暦の年は語彙表 calendar-era が言う。 */
|
|
12
|
+
const ERAS = loadCalendarEras();
|
|
13
|
+
|
|
14
|
+
/** glossYear: 括弧で言い換えた年(令和6年(2024年))の、括弧の中の年を西暦にしたもの。 */
|
|
15
|
+
type Counted = { readonly start: number; readonly end: number; readonly value: number; readonly unit: string; readonly glossYear?: number };
|
|
8
16
|
|
|
9
17
|
const PERCENT = new Set(["%", "%"]);
|
|
10
18
|
/** 形態素が無いときの単位の表。長いものから当てる。形態素があれば、助数詞はすべて読める。 */
|
|
@@ -50,10 +58,12 @@ const isJoiner = (morphs: readonly Morph[], index: number): boolean => {
|
|
|
50
58
|
|
|
51
59
|
/** 桁の語。「26.7 万行」「1.2 万円」のように、数と空白 1 つを挟んで書かれても同じ数の一部。兆は数として読めないので入れない。 */
|
|
52
60
|
const MULTIPLIERS = new Set(["万", "億"]);
|
|
61
|
+
/** 算用数字の後ろの桁の語(「2,400 百万円」「1,320 千円」)の頭。空白を挟んでも数の続き。 */
|
|
62
|
+
const SCALE_HEADS = new Set(["十", "百", "千", ...MULTIPLIERS]);
|
|
53
63
|
|
|
54
64
|
/** 数と桁の語のあいだの空白 1 つ。 */
|
|
55
65
|
const isSpacedMultiplier = (morphs: readonly Morph[], index: number): boolean =>
|
|
56
|
-
isSpace(morphs[index]) && isNumeral(morphs[index - 1]) &&
|
|
66
|
+
isSpace(morphs[index]) && isNumeral(morphs[index - 1]) && SCALE_HEADS.has(morphs[index + 1]?.surface ?? "");
|
|
57
67
|
|
|
58
68
|
/** index から始まる数の並びの終わり(含まない)。 */
|
|
59
69
|
const runEnd = (morphs: readonly Morph[], index: number): number => {
|
|
@@ -152,8 +162,8 @@ const countedByMorphemes = (text: string, morphs: readonly Morph[]): Counted[] =
|
|
|
152
162
|
return found;
|
|
153
163
|
};
|
|
154
164
|
|
|
155
|
-
/** 算用数字は、すぐ後ろか空白 1 つを挟んだ桁の語(1.2万、1.2
|
|
156
|
-
const NUMBER_RUN = /[0-90-9][0-90-9.,]{0,15}(?:[ \t\u3000]?[
|
|
165
|
+
/** 算用数字は、すぐ後ろか空白 1 つを挟んだ桁の語(1.2万、1.2 万、2,400百万)まで一つの数。形態素の経路と同じ読み方。 */
|
|
166
|
+
const NUMBER_RUN = /[0-90-9][0-90-9.,]{0,15}(?:[ \t\u3000]?[十百千]?[万億千])?|[〇一二三四五六七八九十百千万億]{1,12}/gu;
|
|
157
167
|
|
|
158
168
|
export const countedByTable = (text: string): Counted[] =>
|
|
159
169
|
[...text.matchAll(NUMBER_RUN)].flatMap((match) => {
|
|
@@ -171,24 +181,26 @@ const last: { text: string | undefined; ready: boolean; found: readonly Counted[
|
|
|
171
181
|
/** これより小さい「年」は期間(3年)で、暦の年ではない。 */
|
|
172
182
|
const YEAR_FLOOR = 1000;
|
|
173
183
|
|
|
174
|
-
/**
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
184
|
+
/**
|
|
185
|
+
* 元号の年と西暦の年を括弧で並べた年(令和6年(2024年)、2024年(令和8年)、令和6(2024)年)は、一つの暦の年。
|
|
186
|
+
* 括弧の外に書いた年を読み、中の年は数量でも別の日付でもない。年の後ろに月が続けば、一つの日付として読める。
|
|
187
|
+
*/
|
|
188
|
+
const GLOSSED_YEARS = glossedYearReader(ERAS);
|
|
189
|
+
|
|
190
|
+
const withGlossedYears = (text: string, found: readonly Counted[]): Counted[] => {
|
|
191
|
+
const glossed = GLOSSED_YEARS(text);
|
|
192
|
+
if (glossed.length === 0) return [...found];
|
|
193
|
+
const years = glossed.map((item): Counted => ({ start: item.start, end: item.end, value: item.year, unit: "年", glossYear: item.glossYear }));
|
|
194
|
+
const outside = found.filter((item) => !years.some((year) => item.start < year.end && year.start < item.end));
|
|
195
|
+
return [...outside, ...years].toSorted((left, right) => left.start - right.start);
|
|
196
|
+
};
|
|
185
197
|
|
|
186
198
|
const counted = (text: string): readonly Counted[] => {
|
|
187
199
|
if (last.text === text && last.ready === isReady()) return last.found;
|
|
188
200
|
// IPADIC は全角の「4月」を一語の名詞と読む。全角数字は 1 文字ずつ半角にしてから読むので、位置は変わらない。
|
|
189
201
|
const half = toHalfWidth(text);
|
|
190
202
|
const morphs = morphemes(half);
|
|
191
|
-
const found =
|
|
203
|
+
const found = withGlossedYears(half, morphs === undefined ? countedByTable(text) : countedByMorphemes(half, morphs));
|
|
192
204
|
Object.assign(last, { text, ready: isReady(), found });
|
|
193
205
|
return found;
|
|
194
206
|
};
|
|
@@ -201,10 +213,12 @@ type DateMatch = { readonly date: Mention; readonly used: number };
|
|
|
201
213
|
const follows = (left: Counted | undefined, right: Counted | undefined, unit: string): right is Counted =>
|
|
202
214
|
left !== undefined && right?.unit === unit && right.start === left.end;
|
|
203
215
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
}
|
|
216
|
+
/** 括弧で言い換えた年で始まる日付は、括弧の中の年も持つ(attrs.glossYear)。外の年と違えば、どちらかの書き違い。 */
|
|
217
|
+
const dateOf = (parts: readonly Counted[], value: string): DateMatch => {
|
|
218
|
+
const glossYear = parts[0]?.glossYear;
|
|
219
|
+
const attrs = glossYear === undefined ? { value } : { value, glossYear };
|
|
220
|
+
return { date: { start: parts[0]?.start ?? 0, end: parts.at(-1)?.end ?? 0, attrs }, used: parts.length };
|
|
221
|
+
};
|
|
208
222
|
|
|
209
223
|
/** 「2024年4月1日」「2024年4月」。 */
|
|
210
224
|
const yearMonth = (year: Counted | undefined, month: Counted | undefined, day: Counted | undefined): DateMatch | undefined => {
|
|
@@ -260,10 +274,25 @@ type Span = { readonly start: number; readonly end: number };
|
|
|
260
274
|
/** ISO の日付の中の数(「02」)を、数量や年月日の日付として二重に読まない。 */
|
|
261
275
|
const overlaps = (item: Span, spans: readonly Span[]): boolean => spans.some((span) => item.start < span.end && span.start < item.end);
|
|
262
276
|
|
|
277
|
+
const LEXICONS = loadLexicons();
|
|
278
|
+
const notation = (position: "before" | "after"): string[] =>
|
|
279
|
+
(LEXICONS["currency-notation"] ?? []).filter((entry) => entry.position === position).map((entry) => entry.pattern);
|
|
280
|
+
|
|
281
|
+
const AMOUNT_VOCABULARY: AmountVocabulary = {
|
|
282
|
+
before: notation("before"),
|
|
283
|
+
after: [...notation("after"), ...(LEXICONS["percent-unit"] ?? []).map((entry) => entry.pattern)],
|
|
284
|
+
multipliers: (LEXICONS["amount-multiplier"] ?? []).flatMap((entry) => (entry.weight === undefined ? [] : [{ pattern: entry.pattern, weight: entry.weight }])),
|
|
285
|
+
};
|
|
286
|
+
|
|
263
287
|
export const quantities = (text: string): Mention[] => {
|
|
264
288
|
const iso = isoDates(text);
|
|
265
|
-
|
|
266
|
-
|
|
289
|
+
const { dates: written, rest } = toDates(counted(text));
|
|
290
|
+
const read = rest.filter((item) => !overlaps(item, iso));
|
|
291
|
+
// 助数詞として読めない通貨の書き方(¥1,320、1,320米ドル)は、ほかの読みと重ならないものだけ足す。
|
|
292
|
+
const taken = [...iso, ...written, ...read];
|
|
293
|
+
const added = currencyAmounts(text, AMOUNT_VOCABULARY).filter((item) => !overlaps(item, taken));
|
|
294
|
+
return [...read, ...added]
|
|
295
|
+
.toSorted((left, right) => left.start - right.start)
|
|
267
296
|
.map((item) => ({ start: item.start, end: item.end, attrs: { value: item.value, unit: item.unit } }));
|
|
268
297
|
};
|
|
269
298
|
|
|
@@ -278,19 +307,22 @@ const weekdayAfter = (text: string, end: number): number | undefined => {
|
|
|
278
307
|
};
|
|
279
308
|
|
|
280
309
|
/** 元号の最初の年の前年(令和1年 = 2019 年)。元号で書いた日付を西暦の日付にする。 */
|
|
281
|
-
const ERA_BASE:
|
|
282
|
-
const
|
|
310
|
+
const ERA_BASE: ReadonlyMap<string, number> = new Map(ERAS.map((era) => [era.name, era.firstYear - 1]));
|
|
311
|
+
const ERA_NAMES = ERAS.map((era) => escapeRegExp(era.name)).join("|");
|
|
312
|
+
const LONGEST_ERA = Math.max(0, ...ERAS.map((era) => era.name.length));
|
|
313
|
+
const ERA_BEFORE = new RegExp(`(?<era>${ERA_NAMES})$`, "u");
|
|
283
314
|
/** 月の付いた日付の年を直す。年だけ(「昭和二十二年法律」)は 1000 に届かないので、はじめから日付でなく期間の数量。 */
|
|
284
315
|
const ERA_DATE = /^(?<year>\d{1,2})-(?<rest>.+)$/u;
|
|
285
316
|
|
|
286
317
|
/** 「令和元年10月1日」: 元年は数として読めないので、年の無い 10-01 になっている。その前の「元号 + 元年」を 1 年として足す。 */
|
|
287
|
-
const
|
|
318
|
+
const FIRST_YEAR = "元年";
|
|
319
|
+
const FIRST_YEAR_BEFORE = new RegExp(`(?<era>${ERA_NAMES})${FIRST_YEAR}$`, "u");
|
|
288
320
|
const MONTH_DAY = /^\d{2}-\d{2}$/u;
|
|
289
321
|
|
|
290
322
|
const inFirstEraYear = (text: string, date: Mention): Mention | undefined => {
|
|
291
323
|
if (!MONTH_DAY.test(String(date.attrs["value"]))) return undefined;
|
|
292
|
-
const found = FIRST_YEAR_BEFORE.exec(text.slice(Math.max(0, date.start -
|
|
293
|
-
const base = found?.groups?.["era"] === undefined ? undefined : ERA_BASE
|
|
324
|
+
const found = FIRST_YEAR_BEFORE.exec(text.slice(Math.max(0, date.start - LONGEST_ERA - FIRST_YEAR.length), date.start));
|
|
325
|
+
const base = found?.groups?.["era"] === undefined ? undefined : ERA_BASE.get(found.groups["era"]);
|
|
294
326
|
if (found === null || base === undefined) return undefined;
|
|
295
327
|
return { ...date, start: date.start - found[0].length, attrs: { ...date.attrs, value: `${String(base + 1)}-${String(date.attrs["value"])}` } };
|
|
296
328
|
};
|
|
@@ -299,8 +331,8 @@ const inWesternYear = (text: string, date: Mention): Mention => {
|
|
|
299
331
|
const first = inFirstEraYear(text, date);
|
|
300
332
|
if (first !== undefined) return first;
|
|
301
333
|
const parts = ERA_DATE.exec(String(date.attrs["value"]))?.groups;
|
|
302
|
-
const era = ERA_BEFORE.exec(text.slice(Math.max(0, date.start -
|
|
303
|
-
const base = era === undefined ? undefined : ERA_BASE
|
|
334
|
+
const era = ERA_BEFORE.exec(text.slice(Math.max(0, date.start - LONGEST_ERA), date.start))?.groups?.["era"];
|
|
335
|
+
const base = era === undefined ? undefined : ERA_BASE.get(era);
|
|
304
336
|
if (parts === undefined || era === undefined || base === undefined) return date;
|
|
305
337
|
const value = `${String(base + Number(parts["year"]))}-${parts["rest"] ?? ""}`;
|
|
306
338
|
return { ...date, start: date.start - era.length, attrs: { ...date.attrs, value } };
|
package/src/unit-case.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// 語彙の単位が語(大文字小文字を問わずに読む)か、記号(書かれたとおりに読む)か。
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A word is three or more lowercase letters, possibly in several words ("percent", "per cent", "million", "yen"). Anything
|
|
5
|
+
* else is a symbol whose case carries meaning: a capital the lexicon wrote (USD, kDa, mL vs ML), or a lowercase symbol of
|
|
6
|
+
* one or two letters (m vs M, g vs G, mg vs Mg).
|
|
7
|
+
*/
|
|
8
|
+
const UNIT_WORD = /^\p{Ll}{3,}(?: \p{Ll}+)*$/u;
|
|
9
|
+
|
|
10
|
+
export const isUnitWord = (unit: string): boolean => UNIT_WORD.test(unit);
|
|
11
|
+
|
|
12
|
+
/** "8.5 Percent", "198 Million Yen": a unit word starts at `at` in any case; a symbol only as the lexicon wrote it. */
|
|
13
|
+
export const startsWithUnit = (text: string, at: number, unit: string): boolean =>
|
|
14
|
+
isUnitWord(unit) ? text.slice(at, at + unit.length).toLowerCase() === unit : text.startsWith(unit, at);
|