@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.
Files changed (182) hide show
  1. package/dist/currency-amounts.d.ts +21 -0
  2. package/dist/currency-amounts.d.ts.map +1 -0
  3. package/dist/currency-amounts.js +73 -0
  4. package/dist/currency-amounts.js.map +1 -0
  5. package/dist/era-year.d.ts +18 -0
  6. package/dist/era-year.d.ts.map +1 -0
  7. package/dist/era-year.js +47 -0
  8. package/dist/era-year.js.map +1 -0
  9. package/dist/lexicons.d.ts +3 -0
  10. package/dist/lexicons.d.ts.map +1 -1
  11. package/dist/lexicons.js +12 -0
  12. package/dist/lexicons.js.map +1 -1
  13. package/dist/numbers.d.ts +1 -1
  14. package/dist/numbers.d.ts.map +1 -1
  15. package/dist/numbers.js +7 -4
  16. package/dist/numbers.js.map +1 -1
  17. package/dist/quantities.d.ts +2 -0
  18. package/dist/quantities.d.ts.map +1 -1
  19. package/dist/quantities.js +56 -27
  20. package/dist/quantities.js.map +1 -1
  21. package/dist/unit-case.d.ts +4 -0
  22. package/dist/unit-case.d.ts.map +1 -0
  23. package/dist/unit-case.js +11 -0
  24. package/dist/unit-case.js.map +1 -0
  25. package/lexicons/age-reference-label.yaml +12 -0
  26. package/lexicons/age-time-mark.yaml +9 -0
  27. package/lexicons/annual-holidays-label.yaml +10 -0
  28. package/lexicons/annual-holidays-word.yaml +23 -0
  29. package/lexicons/annual-pay-label.yaml +22 -0
  30. package/lexicons/annual-pay-word.yaml +44 -0
  31. package/lexicons/any-year-marker.yaml +18 -0
  32. package/lexicons/balance-column.yaml +43 -0
  33. package/lexicons/benefit-limit-word.yaml +9 -0
  34. package/lexicons/birth-date-label.yaml +6 -0
  35. package/lexicons/bmi-term.yaml +10 -0
  36. package/lexicons/bmi-unit.yaml +7 -0
  37. package/lexicons/calendar-era.yaml +6 -0
  38. package/lexicons/change-column.yaml +28 -0
  39. package/lexicons/change-target.yaml +4 -1
  40. package/lexicons/closing-balance-label.yaml +48 -0
  41. package/lexicons/company-form-foreign.yaml +45 -0
  42. package/lexicons/company-name-kana.yaml +91 -0
  43. package/lexicons/cumulative-table.yaml +35 -0
  44. package/lexicons/currency-column.yaml +13 -0
  45. package/lexicons/date-deadline-mark.yaml +10 -0
  46. package/lexicons/dated-column-label.yaml +14 -0
  47. package/lexicons/dated-pair-label.yaml +217 -0
  48. package/lexicons/dated-span-label.yaml +12 -0
  49. package/lexicons/deductible-basis.yaml +20 -0
  50. package/lexicons/deductible-word.yaml +8 -0
  51. package/lexicons/discount-rate-label.yaml +30 -0
  52. package/lexicons/due-date-label.yaml +4 -0
  53. package/lexicons/email-greeting.yaml +4 -0
  54. package/lexicons/email-sentence-end.yaml +10 -0
  55. package/lexicons/event-bill-word.yaml +5 -0
  56. package/lexicons/event-due-word.yaml +6 -0
  57. package/lexicons/event-payment-deadline-word.yaml +18 -0
  58. package/lexicons/event-payment-word.yaml +8 -0
  59. package/lexicons/event-start-label.yaml +9 -0
  60. package/lexicons/event-ticket-word.yaml +8 -0
  61. package/lexicons/event-trip-word.yaml +12 -0
  62. package/lexicons/fact-age-joiner.yaml +10 -0
  63. package/lexicons/fact-age-limit.yaml +7 -0
  64. package/lexicons/fact-age-mark.yaml +7 -0
  65. package/lexicons/fact-amount-condition.yaml +15 -0
  66. package/lexicons/fact-count-column.yaml +8 -0
  67. package/lexicons/fact-count-counter.yaml +7 -0
  68. package/lexicons/fact-count-not-whole.yaml +22 -0
  69. package/lexicons/fact-count-restating.yaml +9 -0
  70. package/lexicons/fact-count-separate-rows.yaml +6 -0
  71. package/lexicons/fact-count-whole.yaml +9 -0
  72. package/lexicons/fact-document-term.yaml +32 -0
  73. package/lexicons/fact-label-count-link.yaml +9 -0
  74. package/lexicons/fact-label-counter.yaml +25 -0
  75. package/lexicons/fact-label-drop.yaml +7 -0
  76. package/lexicons/fact-label-qualifier.yaml +8 -0
  77. package/lexicons/fact-rate-base.yaml +12 -0
  78. package/lexicons/fact-rate-kind.yaml +13 -0
  79. package/lexicons/fact-rate-period.yaml +16 -0
  80. package/lexicons/fact-separator.yaml +2 -0
  81. package/lexicons/fact-term-self.yaml +9 -0
  82. package/lexicons/fact-term-start.yaml +7 -0
  83. package/lexicons/formula-amount-word.yaml +12 -0
  84. package/lexicons/gpa-label.yaml +18 -0
  85. package/lexicons/grade-point.yaml +35 -0
  86. package/lexicons/heading-number-label.yaml +1 -2
  87. package/lexicons/installment-label.yaml +49 -0
  88. package/lexicons/installment-word.yaml +37 -0
  89. package/lexicons/interest-label.yaml +28 -0
  90. package/lexicons/item-name-column.yaml +9 -0
  91. package/lexicons/lab-result-column.yaml +23 -0
  92. package/lexicons/label-product-word.yaml +21 -0
  93. package/lexicons/line-amount-column.yaml +3 -0
  94. package/lexicons/meter-event.yaml +24 -0
  95. package/lexicons/meter-reading-label.yaml +29 -0
  96. package/lexicons/meter-unit.yaml +10 -0
  97. package/lexicons/name-char-reading.yaml +18 -0
  98. package/lexicons/name-lookalike-char.yaml +26 -0
  99. package/lexicons/net-amount-label.yaml +62 -0
  100. package/lexicons/net-amount-period.yaml +14 -0
  101. package/lexicons/net-gross-weight-column.yaml +20 -0
  102. package/lexicons/order-quantity-column.yaml +26 -0
  103. package/lexicons/organization-unit.yaml +25 -0
  104. package/lexicons/orthographic-variant-distinct.yaml +6 -0
  105. package/lexicons/orthographic-variant-whole-word.yaml +27 -0
  106. package/lexicons/overnight-work-mark.yaml +13 -0
  107. package/lexicons/pack-count-column.yaml +10 -0
  108. package/lexicons/pack-price-column.yaml +12 -0
  109. package/lexicons/per-unit-mark.yaml +1 -0
  110. package/lexicons/period-label.yaml +9 -0
  111. package/lexicons/period-multiple-label.yaml +12 -0
  112. package/lexicons/period-multiple-word.yaml +30 -0
  113. package/lexicons/period-part-after.yaml +8 -0
  114. package/lexicons/period-part-inline-joiner.yaml +5 -0
  115. package/lexicons/period-part-marker.yaml +9 -0
  116. package/lexicons/period-part-range.yaml +10 -0
  117. package/lexicons/period-part-word.yaml +5 -0
  118. package/lexicons/person-label.yaml +19 -0
  119. package/lexicons/person-title.yaml +22 -0
  120. package/lexicons/phone-label.yaml +9 -0
  121. package/lexicons/points-earned-column.yaml +20 -0
  122. package/lexicons/points-rate-joiner.yaml +9 -0
  123. package/lexicons/points-unit.yaml +7 -0
  124. package/lexicons/product-form.yaml +23 -0
  125. package/lexicons/quantity-column.yaml +22 -0
  126. package/lexicons/range-start-end.yaml +30 -0
  127. package/lexicons/rate-unit.yaml +8 -0
  128. package/lexicons/ratio-label.yaml +15 -0
  129. package/lexicons/ratio-term.yaml +13 -0
  130. package/lexicons/reference-bound-mark.yaml +14 -0
  131. package/lexicons/reference-column-heading.yaml +7 -0
  132. package/lexicons/reference-column.yaml +16 -0
  133. package/lexicons/reference-flag.yaml +23 -0
  134. package/lexicons/reference-label.yaml +123 -0
  135. package/lexicons/reference-unit-column.yaml +7 -0
  136. package/lexicons/reference-unit.yaml +80 -0
  137. package/lexicons/reporting-comparison.yaml +26 -0
  138. package/lexicons/reporting-current-not.yaml +10 -0
  139. package/lexicons/reporting-current.yaml +15 -0
  140. package/lexicons/reporting-forecast.yaml +18 -0
  141. package/lexicons/reporting-period-frame.yaml +19 -0
  142. package/lexicons/reporting-period-line.yaml +12 -0
  143. package/lexicons/reporting-period-not.yaml +23 -0
  144. package/lexicons/reporting-results-word.yaml +10 -0
  145. package/lexicons/reporting-year-frame.yaml +7 -0
  146. package/lexicons/request-offer.yaml +15 -0
  147. package/lexicons/retention-unit.yaml +2 -2
  148. package/lexicons/revision-column.yaml +15 -0
  149. package/lexicons/revision-direction.yaml +21 -0
  150. package/lexicons/serial-label.yaml +9 -0
  151. package/lexicons/service-charge-label.yaml +6 -0
  152. package/lexicons/share-amount-label.yaml +20 -0
  153. package/lexicons/share-column-rate-header.yaml +41 -0
  154. package/lexicons/shift-column.yaml +25 -0
  155. package/lexicons/table-scale-unit.yaml +19 -0
  156. package/lexicons/table-unit-caption.yaml +6 -0
  157. package/lexicons/tax-price-column.yaml +14 -0
  158. package/lexicons/tier-label.yaml +25 -0
  159. package/lexicons/tier-unit.yaml +9 -0
  160. package/lexicons/timed-pair-label.yaml +71 -0
  161. package/lexicons/total-label.yaml +12 -1
  162. package/lexicons/total-noun-listed.yaml +8 -0
  163. package/lexicons/total-noun-qualifier.yaml +8 -0
  164. package/lexicons/total-noun-unsummed.yaml +10 -0
  165. package/lexicons/total-period-qualifier.yaml +10 -0
  166. package/lexicons/transcript-column.yaml +17 -0
  167. package/lexicons/unit-area.yaml +6 -0
  168. package/lexicons/unit-concentration.yaml +71 -0
  169. package/lexicons/unit-price-column.yaml +21 -0
  170. package/lexicons/unit-volume.yaml +4 -0
  171. package/lexicons/use-amount-word.yaml +9 -0
  172. package/lexicons/variance-column.yaml +17 -0
  173. package/lexicons/vehicle-plate.yaml +7 -0
  174. package/lexicons/weekly-days-off.yaml +35 -0
  175. package/lexicons/year-span-joiner.yaml +13 -0
  176. package/package.json +2 -2
  177. package/src/currency-amounts.ts +96 -0
  178. package/src/era-year.ts +66 -0
  179. package/src/lexicons.ts +18 -0
  180. package/src/numbers.ts +7 -3
  181. package/src/quantities.ts +61 -29
  182. 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
- type Counted = { readonly start: number; readonly end: number; readonly value: number; readonly unit: string };
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]) && MULTIPLIERS.has(morphs[index + 1]?.surface ?? "");
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]?[万億])?|[〇一二三四五六七八九十百千万億]{1,12}/gu;
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
- /** 「2026年(令和8年)9月4日」: 西暦の年のすぐ後ろの括弧に書いた元号の年は、同じ年の言い換えで、数量でも別の日付でもない。 */
175
- const ERA_GLOSS = /^[((](?:令和|平成|昭和|大正|明治)(?:[0-9〇一二三四五六七八九十]{1,3}|元)年[))]/u;
176
-
177
- /** 括弧の言い換えを西暦の年に含め、括弧の中の年を落とす。年の後ろに月が続けば、一つの日付として読める。 */
178
- const withEraGlosses = (text: string, found: readonly Counted[]): Counted[] =>
179
- found.reduce<Counted[]>((kept, item) => {
180
- const previous = kept.at(-1);
181
- if (previous !== undefined && item.end <= previous.end) return kept;
182
- const gloss = item.unit === "年" && item.value >= YEAR_FLOOR ? ERA_GLOSS.exec(text.slice(item.end)) : null;
183
- return [...kept, gloss === null ? item : { ...item, end: item.end + gloss[0].length }];
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 = withEraGlosses(half, morphs === undefined ? countedByTable(text) : countedByMorphemes(half, morphs));
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
- const dateOf = (parts: readonly Counted[], value: string): DateMatch => ({
205
- date: { start: parts[0]?.start ?? 0, end: parts.at(-1)?.end ?? 0, attrs: { value } },
206
- used: parts.length,
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
- return toDates(counted(text))
266
- .rest.filter((item) => !overlaps(item, iso))
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: Readonly<Record<string, number>> = { 令和: 2018, 平成: 1988, 昭和: 1925, 大正: 1911, 明治: 1867 };
282
- const ERA_BEFORE = /(?<era>令和|平成|昭和|大正|明治)$/u;
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 FIRST_YEAR_BEFORE = /(?<era>令和|平成|昭和|大正|明治)元年$/u;
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 - 4), date.start));
293
- const base = found?.groups?.["era"] === undefined ? undefined : ERA_BASE[found.groups["era"]];
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 - 2), date.start))?.groups?.["era"];
303
- const base = era === undefined ? undefined : ERA_BASE[era];
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 } };
@@ -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);