@chaffjs/lang-en 0.34.0 → 0.35.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 (70) hide show
  1. package/dist/citation.d.ts +5 -0
  2. package/dist/citation.d.ts.map +1 -1
  3. package/dist/citation.js +21 -1
  4. package/dist/citation.js.map +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +6 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/initial-stop.d.ts +27 -0
  9. package/dist/initial-stop.d.ts.map +1 -0
  10. package/dist/initial-stop.js +47 -0
  11. package/dist/initial-stop.js.map +1 -0
  12. package/dist/item-style.d.ts +5 -2
  13. package/dist/item-style.d.ts.map +1 -1
  14. package/dist/item-style.js +31 -6
  15. package/dist/item-style.js.map +1 -1
  16. package/dist/law-citation.d.ts +14 -0
  17. package/dist/law-citation.d.ts.map +1 -0
  18. package/dist/law-citation.js +58 -0
  19. package/dist/law-citation.js.map +1 -0
  20. package/dist/lead-initial.d.ts +5 -0
  21. package/dist/lead-initial.d.ts.map +1 -0
  22. package/dist/lead-initial.js +24 -0
  23. package/dist/lead-initial.js.map +1 -0
  24. package/dist/structure.d.ts.map +1 -1
  25. package/dist/structure.js +54 -9
  26. package/dist/structure.js.map +1 -1
  27. package/lexicons/abbreviated-label.yaml +2 -0
  28. package/lexicons/alternative-clause-label.yaml +6 -0
  29. package/lexicons/capital-phrase.yaml +3 -1
  30. package/lexicons/case-party-joiner.yaml +7 -0
  31. package/lexicons/citation-name-connector.yaml +28 -0
  32. package/lexicons/citation-series.yaml +4 -1
  33. package/lexicons/common-acronym.yaml +6 -1
  34. package/lexicons/comparative-baseline.yaml +13 -0
  35. package/lexicons/conjunction-lookalike.yaml +13 -0
  36. package/lexicons/definition-scope-lead.yaml +11 -0
  37. package/lexicons/determiner-after-article.yaml +14 -0
  38. package/lexicons/enclosing-unit.yaml +14 -0
  39. package/lexicons/extra-word.yaml +13 -0
  40. package/lexicons/figure-label-plural.yaml +19 -0
  41. package/lexicons/figure-number-joiner.yaml +20 -0
  42. package/lexicons/headnote-heading.yaml +8 -0
  43. package/lexicons/initial-lead-word.yaml +7 -0
  44. package/lexicons/link-label-ending.yaml +6 -0
  45. package/lexicons/list-conjunction.yaml +7 -0
  46. package/lexicons/name-abbreviation.yaml +21 -0
  47. package/lexicons/newsletter-issue.yaml +9 -0
  48. package/lexicons/numbered-division.yaml +4 -1
  49. package/lexicons/over-general-claim.yaml +11 -0
  50. package/lexicons/placeholder-division.yaml +7 -0
  51. package/lexicons/prepositive-abbreviation.yaml +10 -0
  52. package/lexicons/question-wording-lead.yaml +7 -0
  53. package/lexicons/rejected-wording.yaml +8 -0
  54. package/lexicons/request-deadline-section.yaml +11 -0
  55. package/lexicons/request-prohibition.yaml +8 -0
  56. package/lexicons/sentence-start-word.yaml +64 -0
  57. package/lexicons/session-law.yaml +19 -0
  58. package/lexicons/superlative-phrase.yaml +13 -0
  59. package/lexicons/telling-cue.yaml +6 -0
  60. package/lexicons/word-suffix.yaml +5 -0
  61. package/lexicons/wordy-phrase.yaml +2 -2
  62. package/lexicons/year-reference.yaml +12 -0
  63. package/package.json +1 -1
  64. package/src/citation.ts +24 -1
  65. package/src/index.ts +13 -1
  66. package/src/initial-stop.ts +80 -0
  67. package/src/item-style.ts +33 -7
  68. package/src/law-citation.ts +68 -0
  69. package/src/lead-initial.ts +28 -0
  70. package/src/structure.ts +59 -9
@@ -0,0 +1,8 @@
1
+ # Words right after a quoted wording that reject it ("high winds" should not be used, …). The wording is not a
2
+ # definition, so unused-definition does not report it. Matched as written.
3
+ id: rejected-wording
4
+ language: en
5
+ entries:
6
+ - pattern: should not be used
7
+ - pattern: is not used
8
+ - pattern: must not be used
@@ -0,0 +1,11 @@
1
+ # A heading, written alone, over the section where a notice states its deadline once for the whole document
2
+ # (a Federal Register notice's "DATES:"). When that section holds a date with a deadline word, every request in the
3
+ # document has that deadline. Compared with the whole heading, its case and a closing colon aside.
4
+ id: request-deadline-section
5
+ language: en
6
+ entries:
7
+ - pattern: dates
8
+ - pattern: key dates
9
+ - pattern: important dates
10
+ - pattern: deadline
11
+ - pattern: deadlines
@@ -0,0 +1,8 @@
1
+ # A request not to do something ("Please do not submit comments by email.") forbids; there is nothing to finish by a date.
2
+ id: request-prohibition
3
+ language: en
4
+ entries:
5
+ - pattern: do not
6
+ - pattern: don't
7
+ - pattern: never
8
+ - pattern: must not
@@ -0,0 +1,64 @@
1
+ # Function words that start a sentence and are never a surname. After a capital initial and its full stop
2
+ # ("Regulation C. The Guide", "Appendix B. In 2020"), the full stop ends the sentence. Matched as written.
3
+ id: sentence-start-word
4
+ language: en
5
+ entries:
6
+ - pattern: The
7
+ - pattern: This
8
+ - pattern: That
9
+ - pattern: These
10
+ - pattern: Those
11
+ - pattern: A
12
+ - pattern: An
13
+ - pattern: It
14
+ - pattern: Its
15
+ - pattern: In
16
+ - pattern: On
17
+ - pattern: At
18
+ - pattern: For
19
+ - pattern: From
20
+ - pattern: With
21
+ - pattern: By
22
+ - pattern: As
23
+ - pattern: If
24
+ - pattern: When
25
+ - pattern: While
26
+ - pattern: Although
27
+ - pattern: However
28
+ - pattern: Thus
29
+ - pattern: Therefore
30
+ - pattern: We
31
+ - pattern: They
32
+ - pattern: He
33
+ - pattern: She
34
+ - pattern: You
35
+ - pattern: Our
36
+ - pattern: Their
37
+ - pattern: There
38
+ - pattern: Here
39
+ - pattern: Each
40
+ - pattern: Every
41
+ - pattern: All
42
+ - pattern: Some
43
+ - pattern: Many
44
+ - pattern: Most
45
+ - pattern: But
46
+ - pattern: And
47
+ - pattern: So
48
+ - pattern: Yet
49
+ - pattern: Also
50
+ - pattern: Then
51
+ - pattern: After
52
+ - pattern: Before
53
+ - pattern: Under
54
+ - pattern: To
55
+ - pattern: Such
56
+ - pattern: What
57
+ - pattern: Which
58
+ - pattern: How
59
+ - pattern: Why
60
+ - pattern: Additionally
61
+ - pattern: Moreover
62
+ - pattern: Furthermore
63
+ - pattern: Nevertheless
64
+ - pattern: Consequently
@@ -0,0 +1,19 @@
1
+ # How a statute's notes cite a law by its own number and sections: "Pub. L. 98–620, title IV, §402(2)", and the page of
2
+ # the volume that prints it, "§2(a), 60 Stat. 237" or "§3.6, Dec. 29, 2009, 75 F.R. 718". A section cited this way is a
3
+ # section of that law, not of this document. position: before is a name written before the law's number (Pub. L.
4
+ # 98–620); position: after is a volume written after the section, between its volume number and page.
5
+ id: session-law
6
+ language: en
7
+ entries:
8
+ - pattern: Pub. L.
9
+ position: before
10
+ - pattern: Public Law
11
+ position: before
12
+ - pattern: Priv. L.
13
+ position: before
14
+ - pattern: Private Law
15
+ position: before
16
+ - pattern: Stat.
17
+ position: after
18
+ - pattern: F.R.
19
+ position: after
@@ -0,0 +1,13 @@
1
+ # Set phrases that hold a superlative but claim nothing is the best. A superlative inside one is not reported by
2
+ # unqualified-superlative. "Even the best" concedes ("even the best teams slip"); "the best case" is a hypothetical.
3
+ id: superlative-phrase
4
+ language: en
5
+ entries:
6
+ - pattern: even the best
7
+ - pattern: even the most
8
+ - pattern: even on the best
9
+ - pattern: even in the best
10
+ - pattern: even for the best
11
+ - pattern: even with the best
12
+ - pattern: the best case
13
+ - pattern: the best-case
@@ -0,0 +1,6 @@
1
+ # Words that name who said a quotation, or when (in Japanese, 申し込んだら、「…」と言われています). The English phrases weasel-word
2
+ # reads ("many experts say", "studies show") do not report a quotation, so there are none; the list exists so both languages
3
+ # name the same lists.
4
+ id: telling-cue
5
+ language: en
6
+ entries: []
@@ -40,6 +40,11 @@ entries:
40
40
  instead_of: ""
41
41
  - pattern: est
42
42
  instead_of: "e"
43
+ # The adjective of a noun: "crunchy" is "crunch", "possessory" is "possessor", "stony" is "stone".
44
+ - pattern: y
45
+ instead_of: ""
46
+ - pattern: y
47
+ instead_of: "e"
43
48
  - pattern: ly
44
49
  instead_of: ""
45
50
  - pattern: ily
@@ -2,6 +2,8 @@
2
2
  # Only phrases whose short form says the same in every context. "with regard to", "prior to" and "in accordance with"
3
3
  # are left out: contracts and statutes use them on purpose. "a small number of" is left out too: "a few" names a
4
4
  # different count, and minutes use both as separate points of one scale (a few, a small number, some, several, many).
5
+ # "at all times" is left out: a rule states its scope with it (pets must be on a leash at all times), and "always" has to
6
+ # move before the verb and reads as a habit rather than a duty.
5
7
  # Matched as whole words, case aside.
6
8
  id: wordy-phrase
7
9
  language: en
@@ -72,8 +74,6 @@ entries:
72
74
  rewrite: near
73
75
  - pattern: in excess of
74
76
  rewrite: more than
75
- - pattern: at all times
76
- rewrite: always
77
77
  - pattern: has the capacity to
78
78
  rewrite: can
79
79
  - pattern: have the capacity to
@@ -34,6 +34,18 @@ entries:
34
34
  position: after
35
35
  - pattern: of the fiscal year
36
36
  position: after
37
+ - pattern: of each calendar year
38
+ position: after
39
+ - pattern: of every calendar year
40
+ position: after
41
+ - pattern: of each fiscal year
42
+ position: after
43
+ - pattern: of the calendar year
44
+ position: after
45
+ - pattern: of the year in which
46
+ position: after
47
+ - pattern: of the year following
48
+ position: after
37
49
  - pattern: annually
38
50
  position: before
39
51
  - pattern: each year
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaffjs/lang-en",
3
- "version": "0.34.0",
3
+ "version": "0.35.0",
4
4
  "description": "English language adapter for chaff",
5
5
  "license": "MIT",
6
6
  "author": "isamu",
package/src/citation.ts CHANGED
@@ -97,15 +97,25 @@ const titleWords = (rest: string): string[] => {
97
97
  return words;
98
98
  };
99
99
 
100
+ /** Labels the reference did not take into its address: the "(E)" of "section 552(a)(6)(E) of title 5". */
101
+ const UNREAD_LABELS = /^(?:\([A-Za-z0-9]{1,4}\))+/u;
102
+
100
103
  /** What follows the "of" after a reference and its list, or undefined when no "of" follows. */
101
104
  const afterOf = (text: string, end: number): string | undefined => {
102
105
  const rest = text.slice(end, end + 200);
103
- const afterList = listEnd(rest);
106
+ const unread = UNREAD_LABELS.exec(rest)?.[0].length ?? 0;
107
+ const afterList = unread + listEnd(rest.slice(unread));
104
108
  const afterGloss = afterList + (GLOSS.exec(rest.slice(afterList))?.[0].length ?? 0);
105
109
  const of = OF.exec(rest.slice(afterGloss));
106
110
  return of === null ? undefined : rest.slice(afterGloss + of[0].length);
107
111
  };
108
112
 
113
+ /**
114
+ * A code's title cited by its number: "section 552(b)(1) of title 5, United States Code", "section 1029 of title 18".
115
+ * An Act numbers its own titles in Roman ("title IV"), and "title 2 of this Act" says it is this document's.
116
+ */
117
+ const CODE_TITLE = /^[Tt]itle (?<n>\d{1,3})(?![\p{L}\p{N}]|\.\d)(?! of this\b)/u;
118
+
109
119
  /**
110
120
  * The document named right after a reference, or undefined when the reference is into this document.
111
121
  * "of this Agreement" and "of the Agreement" are this document; "of the Master Agreement" is another.
@@ -115,12 +125,25 @@ export const citedDocumentAfter = (text: string, end: number): string | undefine
115
125
  if (named === undefined) return undefined;
116
126
  const tag = TAG_AFTER.exec(named)?.groups?.["tag"];
117
127
  if (tag !== undefined) return tag;
128
+ const title = CODE_TITLE.exec(named)?.groups?.["n"];
129
+ if (title !== undefined) return `title ${title}`;
118
130
  const words = titleWords(named);
119
131
  if (words.length === 0) return undefined;
120
132
  const name = words.join(" ");
121
133
  return words.length === 1 && SELF.has(name) ? undefined : name;
122
134
  };
123
135
 
136
+ const THIS_UNIT = /^this (?<unit>\p{L}+)(?![\p{L}\p{N}])/u;
137
+
138
+ /**
139
+ * The unit the reference says it is in, "title" in "section 552 of this title", when that word is one of `units`
140
+ * (lower case); undefined otherwise.
141
+ */
142
+ export const enclosingUnitAfter = (text: string, end: number, units: ReadonlySet<string>): string | undefined => {
143
+ const unit = THIS_UNIT.exec(afterOf(text, end) ?? "")?.groups?.["unit"]?.toLowerCase();
144
+ return unit !== undefined && units.has(unit) ? unit : undefined;
145
+ };
146
+
124
147
  /**
125
148
  * A tag that names another document only if this document lists it, right after a reference ("Section 4.2.3 of
126
149
  * [HTTP-CACHING]", "Section 4.2.2.17 of [19]") or just before it ("[HTTP-CACHING], Section 4", "[23], Section 2.17").
package/src/index.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import { loadLexicons } from "./lexicons.ts";
2
2
  import { unmarkNumberStops } from "./number-stop.ts";
3
3
  import { labelStops, unmarkLabelStops } from "./label-stop.ts";
4
+ import { initialStops, unmarkInitialStops } from "./initial-stop.ts";
5
+ import { leadInitials, unmarkLeadInitials } from "./lead-initial.ts";
4
6
  import { sentenceSpans } from "./sentence-split.ts";
5
7
  import { splitAtQuotedStops } from "./quoted-stop.ts";
6
8
  import { reattachClosingQuotes } from "./closing-quote.ts";
@@ -15,6 +17,15 @@ import type { AdapterNeeds, EmbeddedLanguage, LanguageAdapter, Segmentation, Sen
15
17
 
16
18
  const LEXICONS = loadLexicons();
17
19
  const LABEL_STOPS = labelStops((LEXICONS["abbreviated-label"] ?? []).map((entry) => entry.pattern));
20
+ const INITIAL_STOPS = initialStops(
21
+ (LEXICONS["sentence-start-word"] ?? []).map((entry) => entry.pattern),
22
+ (LEXICONS["prepositive-abbreviation"] ?? []).map((entry) => entry.pattern),
23
+ (LEXICONS["name-abbreviation"] ?? []).map((entry) => entry.pattern),
24
+ );
25
+ const LEAD_INITIALS = leadInitials(
26
+ (LEXICONS["initial-lead-word"] ?? []).map((entry) => entry.pattern),
27
+ (LEXICONS["sentence-start-word"] ?? []).map((entry) => entry.pattern),
28
+ );
18
29
 
19
30
  const LATIN_LETTER = /[a-z]/giu;
20
31
  const COUNTABLE = /\S/gu;
@@ -40,7 +51,8 @@ const asListItem = (sentence: Sentence): Sentence => ({ ...sentence, listItem: t
40
51
 
41
52
  /** Spans of the splitter, cut again where a line break before a list item ends a sentence. */
42
53
  const sentencesOf = (text: string): Sentence[] => {
43
- const quotedStops = sentenceSpans(unmarkNumberStops(unmarkLabelStops(text, LABEL_STOPS))).flatMap((span) => splitAtQuotedStops(text, span));
54
+ const unmarked = unmarkLeadInitials(unmarkInitialStops(unmarkNumberStops(unmarkLabelStops(text, LABEL_STOPS)), INITIAL_STOPS), LEAD_INITIALS);
55
+ const quotedStops = sentenceSpans(unmarked).flatMap((span) => splitAtQuotedStops(text, span));
44
56
  const listBreaks = listLineBreaks(text);
45
57
  const itemHeads = listItemHeads(text);
46
58
  return reattachClosingQuotes(text, quotedStops)
@@ -0,0 +1,80 @@
1
+ import { escapeRegExp } from "./regexp.ts";
2
+
3
+ /**
4
+ * sentence-splitter reads a capital and a full stop as an initial and never ends a sentence there: "Regulation C. The
5
+ * Guide applies" and "Appendix B. In 2020 …" were one sentence. It also ends a sentence after an abbreviation such as
6
+ * "U.S." before any capital, so "the U.S. Census Bureau" was two. Both are decided by the word after the full stop:
7
+ * - capitals with full stops (C., D.C.) before a sentence-start-word (The, In) end the sentence. Their capitals are
8
+ * replaced with a plain letter, which the splitter does not read as an initial;
9
+ * - a prepositive abbreviation (U.S.) before any other capitalised word does not end it, unless a comma follows that
10
+ * word ("in the U.S. Yesterday, …"). Its last full stop is replaced with a letter;
11
+ * - a name abbreviation (Jas., Wm.) stands before a name, so it does not end it before any word but a
12
+ * sentence-start-word, even before a name and a comma ("Jas. Whittaker, J. Hocknell"). Its full stop is replaced
13
+ * with a letter;
14
+ * - capitals with full stops (J., U.K.) before a word in lower case ("J. meacham", "Hart, K. and J. Reed") do not end
15
+ * it, since no sentence opens in lower case. A word that is lower case only at its head (iPhone) may be a name that
16
+ * opens one, so it is left out. Their last full stop is replaced with a letter.
17
+ * The length does not change, so the spans fit the original text. Initials first on their line are a list's labels
18
+ * (A. The first item) and are left as they are.
19
+ */
20
+
21
+ export type InitialStops = { readonly ends: RegExp | undefined; readonly joins: RegExp | undefined; readonly names: RegExp | undefined };
22
+
23
+ const EDGE_BEFORE = String.raw`(?<![\p{L}\p{N}_.'’])`;
24
+ const WORD_END = String.raw`(?![\p{L}\p{N}_.'’-])`;
25
+ const PLAIN_LETTER = "n";
26
+ const CAPITAL = /\p{Lu}/gu;
27
+ const BEFORE_LOWER_CASE = new RegExp(String.raw`${EDGE_BEFORE}(?:\p{Lu}\.){1,3}(?=[ \t\r\n]+\p{Ll}(?!\p{Lu}))`, "gu");
28
+
29
+ const oneOf = (words: readonly string[]): string => `(?:${words.map(escapeRegExp).join("|")})`;
30
+
31
+ const startOf = (startWords: readonly string[]): string | undefined => (startWords.length === 0 ? undefined : String.raw`\s+${oneOf(startWords)}${WORD_END}`);
32
+
33
+ const sentenceStops = (start: string | undefined, prepositive: readonly string[]): Pick<InitialStops, "ends" | "joins"> => {
34
+ if (start === undefined) return { ends: undefined, joins: undefined };
35
+ const ends = new RegExp(String.raw`${EDGE_BEFORE}(?:\p{Lu}\.){1,3}(?=${start})`, "gu");
36
+ if (prepositive.length === 0) return { ends, joins: undefined };
37
+ return { ends, joins: new RegExp(String.raw`${EDGE_BEFORE}${oneOf(prepositive)}(?=\s+\p{Lu})(?!${start})(?!\s+\p{Lu}\p{L}*,)`, "gu") };
38
+ };
39
+
40
+ // "I" is not a sentence-start word because it is also an initial (John I. Smith); after a name abbreviation the pronoun
41
+ // is told by having no full stop of its own.
42
+ const PRONOUN_I = String.raw`(?!\s+I${WORD_END})`;
43
+
44
+ const notBefore = (start: string | undefined): string => (start === undefined ? "" : `(?!${start})`);
45
+
46
+ /** A name abbreviation before any word but a sentence-start word or the pronoun I. */
47
+ const nameJoins = (start: string | undefined, names: readonly string[]): RegExp | undefined => {
48
+ if (names.length === 0) return undefined;
49
+ return new RegExp(String.raw`${EDGE_BEFORE}${oneOf(names)}(?=\s+\p{L})${PRONOUN_I}${notBefore(start)}`, "gu");
50
+ };
51
+
52
+ /** Built once from the lexicons. */
53
+ export const initialStops = (startWords: readonly string[], prepositive: readonly string[], names: readonly string[] = []): InitialStops => {
54
+ const start = startOf(startWords);
55
+ return { ...sentenceStops(start, prepositive), names: nameJoins(start, names) };
56
+ };
57
+
58
+ /** Only a list's marker, or nothing, before the initials on their line (A. The first item; - B. The second item). */
59
+ const LABEL_HEAD = /^[ \t]*(?:[-*+•][ \t]+)?$/u;
60
+
61
+ const lineStartOf = (text: string, index: number): number => Math.max(text.lastIndexOf("\n", index - 1), text.lastIndexOf("\r", index - 1)) + 1;
62
+
63
+ const isFirstOnLine = (text: string, index: number): boolean => LABEL_HEAD.test(text.slice(lineStartOf(text, index), index));
64
+
65
+ const withoutLastStop = (abbreviation: string): string => `${abbreviation.slice(0, -1)}${PLAIN_LETTER}`;
66
+
67
+ const joined = (text: string, pattern: RegExp | undefined): string => (pattern === undefined ? text : text.replace(pattern, withoutLastStop));
68
+
69
+ /** Initials before a word in lower case, not first on their line (a list's label stays as it is), rewritten. */
70
+ const joinedBeforeLowerCase = (text: string): string =>
71
+ text.replace(BEFORE_LOWER_CASE, (initials: string, index: number) => (isFirstOnLine(text, index) ? initials : withoutLastStop(initials)));
72
+
73
+ /** The text with every initial that ends a sentence, and every prepositive or name abbreviation or initial that does not, rewritten. */
74
+ export const unmarkInitialStops = (text: string, stops: InitialStops): string => {
75
+ const ended =
76
+ stops.ends === undefined
77
+ ? text
78
+ : text.replace(stops.ends, (initials: string, index: number) => (isFirstOnLine(text, index) ? initials : initials.replace(CAPITAL, PLAIN_LETTER)));
79
+ return joinedBeforeLowerCase(joined(joined(ended, stops.joins), stops.names));
80
+ };
package/src/item-style.ts CHANGED
@@ -12,12 +12,19 @@ const AMBIGUOUS = /^[ivx]$/u;
12
12
  const DIGITS = /^\d+$/u;
13
13
  const CAPITALS = /^[A-Z]+$/u;
14
14
  const ONE_CAPITAL = /^[A-Z]$/u;
15
+ /**
16
+ * "(aa)", "(bb)": the US Code numbers the items under a subclause "(I)" with a doubled letter. Elsewhere a doubled letter
17
+ * is a letter: a UK Act inserts "(aa)" after "(a)", and a long list goes on from "(z)" to "(aa)".
18
+ */
19
+ const DOUBLED = /^(?<letter>[a-z])\k<letter>$/u;
15
20
 
16
- export type Style = "letter" | "roman" | "digit" | "capital" | "capital-roman";
21
+ export type Style = "letter" | "roman" | "digit" | "capital" | "capital-roman" | "doubled";
17
22
 
18
23
  /** 大文字で書いた同じ並び。"(B)" は英字の、"(II)" はローマ数字の大文字。 */
19
24
  const CAPITAL_OF: Readonly<Record<"letter" | "roman", Style>> = { letter: "capital", roman: "capital-roman" };
20
25
 
26
+ const isDoubled = (raw: string): boolean => DOUBLED.test(raw) && !MULTI_ROMAN.test(raw);
27
+
21
28
  /** 小文字にした番号を形で読む。一文字の "i" は、開いたときに付けた並びの位置で見分ける。ローマ数字なら 1、英字なら 9。 */
22
29
  const shapeOf = (lower: string, ordinal: number | undefined): "letter" | "roman" => {
23
30
  if (MULTI_ROMAN.test(lower)) return "roman";
@@ -29,15 +36,20 @@ export const styleOfOpen = (open: NumberedLine): Style | undefined => {
29
36
  const inner = /^\((?<n>[A-Za-z0-9]{1,5})\)$/u.exec(open.label)?.groups?.["n"];
30
37
  if (inner === undefined) return undefined;
31
38
  if (DIGITS.test(inner) || IS_INSERTED.test(inner)) return "digit";
39
+ if (isDoubled(inner) && open.ordinal !== undefined) return "doubled";
32
40
  const shape = shapeOf(inner.toLowerCase(), open.ordinal);
33
41
  return CAPITALS.test(inner) ? CAPITAL_OF[shape] : shape;
34
42
  };
35
43
 
36
44
  const LETTER_BEFORE_A = "a".charCodeAt(0) - 1;
37
45
 
38
- /** "(b)" と "(B)" は 2 番目、"(ii)" と "(II)" も 2 番目。二文字以上の英字("(aa)")は並びが決まらないので付けない。 */
46
+ /**
47
+ * "(b)" と "(B)" は 2 番目、"(ii)" と "(II)" も 2 番目、細目の下の重ねた "(bb)" も 2 番目。英字の並びの中の "(aa)" は、
48
+ * 何番目かは決めない。
49
+ */
39
50
  export const ordinalOf = (raw: string, style: Style): number | undefined => {
40
51
  if (style === "digit") return IS_INSERTED.test(raw) ? undefined : Number(raw);
52
+ if (style === "doubled") return raw.charCodeAt(0) - LETTER_BEFORE_A;
41
53
  if (style === "roman" || style === "capital-roman") return parseRoman(raw);
42
54
  return raw.length === 1 ? raw.toLowerCase().charCodeAt(0) - LETTER_BEFORE_A : undefined;
43
55
  };
@@ -49,10 +61,10 @@ const follows = (raw: string, style: "letter" | "capital", context: NumberingCon
49
61
  context.open.some((open, index) => styles(context)[index] === style && open.number.charCodeAt(0) + 1 === raw.charCodeAt(0));
50
62
 
51
63
  /**
52
- * 英字の並びは (a) から始まるので、"(a)" や "(1)" のすぐ下に来た "(i)" は一段深いローマ数字。
53
- * 米国の規則は (a)(1)(i) の順に下る。見出しのすぐ下の "(i)" は、どちらとも決まらないので英字。
64
+ * 英字の並びは (a) から始まるので、"(a)" や "(1)" や "(A)" のすぐ下に来た "(i)" は一段深いローマ数字。
65
+ * 米国の規則は (a)(1)(i)、法典は (a)(1)(A)(i) の順に下る。見出しのすぐ下の "(i)" は、どちらとも決まらないので英字。
54
66
  */
55
- const OPENS_ROMAN: ReadonlySet<Style | undefined> = new Set(["letter", "digit"]);
67
+ const OPENS_ROMAN: ReadonlySet<Style | undefined> = new Set(["letter", "digit", "capital"]);
56
68
 
57
69
  /** "(I)" は米国の法典で (i) の一段下の大文字ローマ数字。"(H)" の次なら英字。 */
58
70
  const capitalRoman = (raw: string, open: readonly (Style | undefined)[], context: NumberingContext): boolean => {
@@ -63,14 +75,27 @@ const capitalRoman = (raw: string, open: readonly (Style | undefined)[], context
63
75
  };
64
76
 
65
77
  /**
66
- * 米国の規則は (a)(1)(i)(A) と下る。大文字の "(A)" は、開いているローマ数字のすぐ下で始まるか、開いている大文字の続きのときだけ項目。
78
+ * 米国の規則(CFR)は (a)(1)(i)(A)、米国の法典(U.S.C.)は (a)(1)(A)(i) と下る。"(A)" はローマ数字のすぐ下か、英字の下の数字の
79
+ * すぐ下で始まる。見出しのすぐ下の "(1)" の下の "(A)" は、どちらの並びとも決まらない。
80
+ */
81
+ const opensCapital = (open: readonly (Style | undefined)[]): boolean =>
82
+ open.at(-1) === "roman" || (open.at(-1) === "digit" && open.slice(0, -1).includes("letter"));
83
+
84
+ /**
85
+ * 大文字の "(A)" は、上の並びのすぐ下で始まるか(opensCapital)、開いている大文字の続きのときだけ項目。
67
86
  * 英国の法令や契約書が一段目に使う "(A)" は、何の下とも決まらないので本文のまま読む。
68
87
  */
69
88
  const capitalStyleOf = (raw: string, context: NumberingContext): Style | undefined => {
70
89
  const open = styles(context);
71
90
  if (capitalRoman(raw, open, context)) return "capital-roman";
72
91
  if (!ONE_CAPITAL.test(raw)) return undefined;
73
- return open.includes("capital") || (raw === "A" && open.at(-1) === "roman") ? "capital" : undefined;
92
+ return open.includes("capital") || (raw === "A" && opensCapital(open)) ? "capital" : undefined;
93
+ };
94
+
95
+ /** A doubled letter right under a subclause "(I)", or beside another doubled letter. */
96
+ const underSubclause = (context: NumberingContext): boolean => {
97
+ const innermost = styles(context).at(-1);
98
+ return innermost === "capital-roman" || innermost === "doubled";
74
99
  };
75
100
 
76
101
  /**
@@ -80,6 +105,7 @@ const capitalStyleOf = (raw: string, context: NumberingContext): Style | undefin
80
105
  export const styleOf = (raw: string, context: NumberingContext): Style | undefined => {
81
106
  if (DIGITS.test(raw) || IS_INSERTED.test(raw)) return "digit";
82
107
  if (CAPITALS.test(raw)) return capitalStyleOf(raw, context);
108
+ if (isDoubled(raw)) return underSubclause(context) ? "doubled" : "letter";
83
109
  if (MULTI_ROMAN.test(raw)) return "roman";
84
110
  if (!AMBIGUOUS.test(raw) || follows(raw, "letter", context)) return "letter";
85
111
  const open = styles(context);
@@ -0,0 +1,68 @@
1
+ import type { Lexicon, LexiconEntry } from "chaffjs/plugin";
2
+ import { escapeRegExp } from "./regexp.ts";
3
+
4
+ // "Pub. L. 98–620, title IV, §402(2)" and "ch. 324, §2(a), 60 Stat. 237": a statute's notes cite a law by its number or by
5
+ // the volume that prints it. The section is that law's, not this document's. Which names are laws is the lexicon's
6
+ // (session-law). Pure.
7
+
8
+ /** Built once from the lexicon; each pattern is undefined when the lexicon names no law of its kind. */
9
+ export type LawVocabulary = { readonly before: RegExp | undefined; readonly after: RegExp | undefined };
10
+
11
+ /** A law's number is its Congress and its number in that Congress: "98–620", "89-487". */
12
+ const LAW_NUMBER = String.raw`\s+\d{1,3}[–-]\d{1,3}(?!\d)`;
13
+ /** "Pub. L. 98–620, " is far from "§402(2)" once "div. A, title IV, §201, title VI, " stands between them. */
14
+ const REACH = 160;
15
+ /**
16
+ * What may stand between a law's number and its section: its divisions, titles, subtitles and parts, other sections of
17
+ * it, and "formerly".
18
+ * The gap is these and nothing else; a word of a sentence ("see", "and the Act") ends the citation.
19
+ */
20
+ const CITATION_PARTS: readonly RegExp[] = [
21
+ /(?:div\.|subtitle|part|subchapter|ch\.)\s+[A-Z0-9]{1,4}/u,
22
+ /title\s+[IVXLC]{1,7}/u,
23
+ /formerly|§/u,
24
+ /\d{1,5}[A-Za-z]{0,2}/u,
25
+ /\([A-Za-z0-9]{1,4}\)/u,
26
+ /[\s,–-]/u,
27
+ ];
28
+ const CITATION_PART = new RegExp(CITATION_PARTS.map((part) => part.source).join("|"), "gu");
29
+ /** "§3.6, Dec. 29, 2009, 75 F.R. 718": a date may stand between a section and the volume's page. */
30
+ const DATE = String.raw`(?:,\s*[A-Z][a-z]{2,8}\.?\s+\d{1,2},\s+\d{4})?`;
31
+ /** "§2 (less (a)), 60 Stat. 237": a derivation table says in brackets which part of the section it takes. */
32
+ const PART_TAKEN = String.raw`(?:\s\((?:[^()]|\([^()]{1,10}\)){1,40}\))?`;
33
+
34
+ const namesOf = (entries: readonly LexiconEntry[]): string | undefined => {
35
+ const names = entries.map((entry) => entry.pattern).toSorted((left, right) => right.length - left.length);
36
+ return names.length === 0 ? undefined : `(?:${names.map(escapeRegExp).join("|")})`;
37
+ };
38
+
39
+ export const lawVocabulary = (lexicons: Readonly<Record<string, Lexicon>>): LawVocabulary => {
40
+ const entries = lexicons["session-law"] ?? [];
41
+ const named = namesOf(entries.filter((entry) => entry.position === "before"));
42
+ const volumes = namesOf(entries.filter((entry) => entry.position === "after"));
43
+ return {
44
+ before: named === undefined ? undefined : new RegExp(String.raw`(?<![\p{L}\p{N}])${named}${LAW_NUMBER}`, "gu"),
45
+ after: volumes === undefined ? undefined : new RegExp(String.raw`^${PART_TAKEN}${DATE},\s*\d{1,3}\s+${volumes}\s+\d`, "u"),
46
+ };
47
+ };
48
+
49
+ /** The last law named before `start` whose citation runs on, part by part, up to the reference. */
50
+ const lawBefore = (text: string, start: number, pattern: RegExp): string | undefined => {
51
+ const window = text.slice(Math.max(0, start - REACH), start);
52
+ const last = [...window.matchAll(pattern)].at(-1);
53
+ if (last === undefined) return undefined;
54
+ const gap = window.slice(last.index + last[0].length);
55
+ return gap.replaceAll(CITATION_PART, "") === "" ? last[0] : undefined;
56
+ };
57
+
58
+ /**
59
+ * The law a reference from `start` to `end` is a section of, or undefined when it is not cited as a law's section.
60
+ * Named before it ("Pub. L. 98–620, §402(2)"), or by the volume after it ("§2(a), 60 Stat. 237"), which names the law
61
+ * only by its page, so the volume stands for it.
62
+ */
63
+ export const citedLawAround = (text: string, start: number, end: number, vocabulary: LawVocabulary): string | undefined => {
64
+ const named = vocabulary.before === undefined ? undefined : lawBefore(text, start, vocabulary.before);
65
+ if (named !== undefined) return named;
66
+ const volume = vocabulary.after?.exec(text.slice(end, end + REACH))?.[0];
67
+ return volume === undefined ? undefined : volume.replace(/^[\s,]+/u, "");
68
+ };
@@ -0,0 +1,28 @@
1
+ import { escapeRegExp } from "./regexp.ts";
2
+
3
+ /**
4
+ * sentence-splitter joins an initial to the next word only when a capitalised word stands before it, so "written by
5
+ * J. Smith", "Smith and C.W. Landsea" and "by F. W. Evans" were each cut after the initials. After a word that leads a
6
+ * name (by, and), initials before a capitalised word are a name's, unless that word is a sentence-start-word
7
+ * ("items B and D. The rest") or the initials are the pronoun I ("you and I. Then"). Their full stops are replaced with
8
+ * a plain letter; the length does not change, so the spans fit the original text.
9
+ */
10
+
11
+ const EDGE_BEFORE = String.raw`(?<![\p{L}\p{N}_.'’])`;
12
+ const WORD_END = String.raw`(?![\p{L}\p{N}_.'’-])`;
13
+ const PLAIN_LETTER = "n";
14
+ const FULL_STOP = /\./gu;
15
+ const PRONOUN_I = /^I\.$/u;
16
+
17
+ const oneOf = (words: readonly string[]): string => `(?:${words.map(escapeRegExp).join("|")})`;
18
+
19
+ /** Built once from the lexicons: initials after a lead word, before a capitalised word that is not a sentence-start-word. */
20
+ export const leadInitials = (leadWords: readonly string[], startWords: readonly string[]): RegExp | undefined => {
21
+ if (leadWords.length === 0) return undefined;
22
+ const notStart = startWords.length === 0 ? "" : String.raw`(?!\s+${oneOf(startWords)}${WORD_END})`;
23
+ return new RegExp(String.raw`(?<=${EDGE_BEFORE}${oneOf(leadWords)}[ \t]+)(?:\p{Lu}\.[ \t]?){0,2}\p{Lu}\.(?=\s+\p{Lu})${notStart}`, "gu");
24
+ };
25
+
26
+ /** The text with the full stops of every name's initials after a lead word rewritten. */
27
+ export const unmarkLeadInitials = (text: string, pattern: RegExp | undefined): string =>
28
+ pattern === undefined ? text : text.replace(pattern, (initials: string) => (PRONOUN_I.test(initials) ? initials : initials.replace(FULL_STOP, PLAIN_LETTER)));