@chaffjs/lang-en 0.33.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 (77) 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/abbreviation-mention.yaml +14 -0
  29. package/lexicons/alternative-clause-label.yaml +6 -0
  30. package/lexicons/attached-document-cue.yaml +33 -0
  31. package/lexicons/capital-phrase.yaml +3 -1
  32. package/lexicons/case-party-joiner.yaml +7 -0
  33. package/lexicons/citation-name-connector.yaml +28 -0
  34. package/lexicons/citation-series.yaml +4 -1
  35. package/lexicons/citation-signal.yaml +8 -0
  36. package/lexicons/common-acronym.yaml +8 -1
  37. package/lexicons/comparative-baseline.yaml +13 -0
  38. package/lexicons/concrete-deadline.yaml +7 -0
  39. package/lexicons/conjunction-lookalike.yaml +13 -0
  40. package/lexicons/definition-scope-lead.yaml +11 -0
  41. package/lexicons/determiner-after-article.yaml +14 -0
  42. package/lexicons/email-greeting.yaml +1 -0
  43. package/lexicons/enclosing-unit.yaml +14 -0
  44. package/lexicons/extra-word.yaml +30 -0
  45. package/lexicons/figure-label-plural.yaml +19 -0
  46. package/lexicons/figure-number-joiner.yaml +20 -0
  47. package/lexicons/headnote-heading.yaml +8 -0
  48. package/lexicons/initial-lead-word.yaml +7 -0
  49. package/lexicons/link-label-ending.yaml +6 -0
  50. package/lexicons/list-conjunction.yaml +7 -0
  51. package/lexicons/name-abbreviation.yaml +21 -0
  52. package/lexicons/newsletter-issue.yaml +9 -0
  53. package/lexicons/numbered-division.yaml +4 -1
  54. package/lexicons/over-general-claim.yaml +11 -0
  55. package/lexicons/placeholder-division.yaml +7 -0
  56. package/lexicons/prepositive-abbreviation.yaml +10 -0
  57. package/lexicons/question-wording-lead.yaml +7 -0
  58. package/lexicons/rejected-wording.yaml +8 -0
  59. package/lexicons/request-deadline-section.yaml +11 -0
  60. package/lexicons/request-prohibition.yaml +8 -0
  61. package/lexicons/requirement-open-end.yaml +3 -0
  62. package/lexicons/sentence-start-word.yaml +64 -0
  63. package/lexicons/session-law.yaml +19 -0
  64. package/lexicons/superlative-phrase.yaml +13 -0
  65. package/lexicons/telling-cue.yaml +6 -0
  66. package/lexicons/word-suffix.yaml +5 -0
  67. package/lexicons/wordy-phrase.yaml +2 -2
  68. package/lexicons/year-reference.yaml +12 -0
  69. package/lexicons/year-span-joiner.yaml +5 -1
  70. package/package.json +1 -1
  71. package/src/citation.ts +24 -1
  72. package/src/index.ts +13 -1
  73. package/src/initial-stop.ts +80 -0
  74. package/src/item-style.ts +33 -7
  75. package/src/law-citation.ts +68 -0
  76. package/src/lead-initial.ts +28 -0
  77. package/src/structure.ts +59 -9
@@ -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)));
package/src/structure.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Mention, NumberedLine, NumberingContext, StructurePatterns } from "chaffjs/plugin";
2
- import { citedDocumentAfter, citedDocumentBefore, listedTagAround } from "./citation.ts";
2
+ import { citedDocumentAfter, citedDocumentBefore, enclosingUnitAfter, listedTagAround } from "./citation.ts";
3
3
  import { citedCodeBefore, codeVocabulary, titledCodeAt } from "./code-citation.ts";
4
+ import { citedLawAround, lawVocabulary } from "./law-citation.ts";
4
5
  import { loadLexicons } from "./lexicons.ts";
5
6
  import { membersAfter } from "./reference-list.ts";
6
7
  import { parseRoman } from "./roman.ts";
@@ -33,15 +34,48 @@ const titleOf = (rest: string): string | undefined => {
33
34
 
34
35
  const ARTICLE = /^\s{0,3}(?:ARTICLE|Article)\s+(?<n>\d{1,3}|[IVXLC]{1,7})\b(?<rest>.*)$/u;
35
36
  const SECTION = /^\s{0,3}(?:SECTION|Section|§)\s*(?<n>\d{1,3}(?:\.\d{1,3}){0,5})\b(?<rest>.*)$/u;
36
- /** "(a) text", "(ii) text", "(B) text", or the label alone on its line with the text below it. */
37
- const LETTERED = new RegExp(`^\\s{0,6}\\((?<n>[a-z]{1,4}|[A-Z]{1,4}|\\d{1,3}|${INSERTED})\\)(?:\\s+(?<rest>\\S.*))?\\s*$`, "u");
37
+ /**
38
+ * "Sec. 3. (a) For confidential…", '#### "SEC. 3. REQUIREMENT OF DISCLOSURE.': an Act or an Executive order set out in a
39
+ * statute's notes numbers its sections with the abbreviation, the number ending in a full stop; an Act quoted in the notes
40
+ * opens each paragraph with a quotation mark. "Sec. 552 - Public information" (a citation header) has no full stop.
41
+ */
42
+ const ABBREVIATED_SECTION = /^\s{0,3}["“]?(?:SECTION|Section|SEC\.|Sec\.)\s*(?<n>\d{1,3}(?:\.\d{1,3}){0,5})\.(?=\s|$)(?<rest>.*)$/u;
43
+ /**
44
+ * "(a) text", "(ii) text", "(B) text", or the label alone on its line with the text below it. A US statute joins a
45
+ * subsection's label to its first paragraph's ("(c)(1) Whenever…"): the rest then starts with the next label. A
46
+ * repealed item keeps its place in brackets ("[(D) Repealed. …]").
47
+ */
48
+ const LETTERED = new RegExp(
49
+ `^\\s{0,6}\\[?\\((?<n>[a-z]{1,4}|[A-Z]{1,4}|\\d{1,3}|${INSERTED})\\)(?:\\s+(?<rest>\\S.*)|(?<joined>\\([^()\\s]{1,5}\\)\\s*\\S.*))?\\s*$`,
50
+ "u",
51
+ );
52
+
53
+ /**
54
+ * "Sec. 4. General Provisions. (a) Nothing in this order…": a title run in before the first item of the section's own list.
55
+ * The rest then starts with that label, which opens the item as a joined label does. A title is a few words spaced as
56
+ * words are, with no sentence in it: a wider gap is a page flattened into one line ("ARTICLE II EFFECT… Section 2.1
57
+ * Effect… (a)"), and "At the Effective Time, … thereof: (a) Each share" is the section's text, not its title.
58
+ */
59
+ const RUN_IN_FIRST_ITEM = /^(?<title>\S+(?: \S+){0,11}?[.:]) (?<item>\([a1]\)\s+\p{Lu}.*)$/u;
60
+ /** A lowercase word ending a sentence or a clause: "thereof: ", "Time. ". "U.S.C. " and "No. " end in capitals. */
61
+ const SENTENCE_END = /\p{Ll}[.:;] /u;
62
+ /** "(a) Prior to… (b) Each…": the next item on the same line is a list flattened into one paragraph, not one item. */
63
+ const NEXT_RUN_IN = /[.;:] \([b2]\) \p{Lu}/u;
64
+
65
+ const titleAndRest = (heading: string): { readonly heading: string; readonly rest: string } => {
66
+ const title = RUN_IN_FIRST_ITEM.exec(heading)?.groups?.["title"];
67
+ const item = title === undefined ? undefined : heading.slice(title.length + 1);
68
+ if (title === undefined || item === undefined || SENTENCE_END.test(title) || NEXT_RUN_IN.test(item)) return { heading, rest: heading };
69
+ return { heading: title, rest: item };
70
+ };
38
71
 
39
72
  const headed = (pattern: RegExp, line: string, numbering: string, label: (n: string) => string): NumberedLine | undefined => {
40
73
  const groups = pattern.exec(line)?.groups;
41
74
  const number = numberOf(groups?.["n"]);
42
- const heading = titleOf(groups?.["rest"] ?? "");
43
- if (groups === undefined || number === undefined || heading === undefined) return undefined;
75
+ const title = titleOf(groups?.["rest"] ?? "");
76
+ if (groups === undefined || number === undefined || title === undefined) return undefined;
44
77
  const parts = number.split(".");
78
+ const { heading, rest } = titleAndRest(title);
45
79
  return {
46
80
  kind: "article",
47
81
  depth: parts.length,
@@ -49,7 +83,7 @@ const headed = (pattern: RegExp, line: string, numbering: string, label: (n: str
49
83
  absolute: true,
50
84
  label: label(groups["n"] ?? ""),
51
85
  heading,
52
- rest: heading,
86
+ rest,
53
87
  ordinal: Number(parts.at(-1)),
54
88
  numbering,
55
89
  };
@@ -59,9 +93,12 @@ const lettered = (line: string, context: NumberingContext): NumberedLine | undef
59
93
  const groups = LETTERED.exec(line)?.groups;
60
94
  const raw = groups?.["n"];
61
95
  if (groups === undefined || raw === undefined) return undefined;
96
+ // "(a)(i) …", "(a)(ii) …": a list that writes its parent's label on every line repeats the open (a), and is its (ii).
97
+ const joined = groups["joined"];
98
+ if (joined !== undefined && context.open.some((open) => open.kind === "item" && open.label === `(${raw})`)) return lettered(joined, context);
62
99
  const style = styleOf(raw, context);
63
100
  const ordinal = style === undefined ? undefined : ordinalOf(raw, style);
64
- const rest = groups["rest"];
101
+ const rest = groups["rest"] ?? groups["joined"];
65
102
  if (style === undefined || (rest === undefined && !continuesOpen(style, ordinal, context))) return undefined;
66
103
  // 番地は書かれたままの "ii" を使う。参照「Section 4.2(a)(ii)」も同じ形で書かれるので、そのまま引ける。
67
104
  return {
@@ -94,6 +131,7 @@ const numbered = (line: string, context: NumberingContext): NumberedLine | undef
94
131
  chapter(CHAPTER, line, CHAPTER_DEPTH, "ch", "Chapter") ??
95
132
  headed(ARTICLE, line, "article", (n) => `Article ${n}`) ??
96
133
  headed(SECTION, line, "section", (n) => `Section ${n}`) ??
134
+ headed(ABBREVIATED_SECTION, line, "section", (n) => `Section ${n}`) ??
97
135
  lettered(line, context);
98
136
 
99
137
  const REFERENCE = /(?<word>\b[Ss]ections?|\b[Aa]rticles?|§) ?(?<n>\d{1,3}(?:\.\d{1,3}){0,5}|[IVXLC]{1,7})\b/gu;
@@ -146,6 +184,8 @@ const glossedDocument = (gloss: Gloss): string | undefined => {
146
184
 
147
185
  const LEXICONS = loadLexicons();
148
186
  const CODES = codeVocabulary(LEXICONS);
187
+ const LAWS = lawVocabulary(LEXICONS);
188
+ const ENCLOSING_UNITS: ReadonlySet<string> = new Set((LEXICONS["enclosing-unit"] ?? []).map((entry) => entry.pattern.toLowerCase()));
149
189
 
150
190
  /** The other document a reference names, or else a bracketed tag that the core checks against the document's list. */
151
191
  const citation = (text: string, start: number, end: number, document: string | undefined): Readonly<Record<string, string>> => {
@@ -154,6 +194,12 @@ const citation = (text: string, start: number, end: number, document: string | u
154
194
  return citedTag === undefined ? {} : { citedTag };
155
195
  };
156
196
 
197
+ /** "section 552 of this title": the unit of a code the reference says it is in, which the core checks against the tree. */
198
+ const enclosing = (text: string, end: number): Readonly<Record<string, string>> => {
199
+ const withinUnit = enclosingUnitAfter(text, end, ENCLOSING_UNITS);
200
+ return withinUnit === undefined ? {} : { withinUnit };
201
+ };
202
+
157
203
  /**
158
204
  * "Section 4.2(a)" → 4.2.a, "Article III" → 3. The same addresses the tree gives.
159
205
  * "Section 9 of the Master Agreement" and "35 CFR §122" carry the other document's name, and are not looked up in this tree.
@@ -165,12 +211,16 @@ const references = (text: string): Mention[] => {
165
211
  if (main === undefined) return [];
166
212
  const { parts, end } = subdivisions(text, match.index + match[0].length);
167
213
  advance(gloss, text, match.index);
168
- const cited = citedDocumentAfter(text, end) ?? citedDocumentBefore(text, match.index) ?? citedCodeBefore(text, match.index, CODES);
214
+ const cited =
215
+ citedDocumentAfter(text, end) ??
216
+ citedDocumentBefore(text, match.index) ??
217
+ citedCodeBefore(text, match.index, CODES) ??
218
+ citedLawAround(text, match.index, end, LAWS);
169
219
  const document = cited ?? glossedDocument(gloss);
170
220
  gloss.scanned = Math.max(gloss.scanned, end);
171
221
  if (cited !== undefined) gloss.anchors.push({ document: cited, depth: gloss.depth });
172
222
  const numbering = /^[Aa]/u.test(match.groups?.["word"] ?? "") ? "article" : "section";
173
- const shared = { numbering, ...citation(text, match.index, end, document) };
223
+ const shared = { numbering, ...enclosing(text, end), ...citation(text, match.index, end, document) };
174
224
  const first = { start: match.index, end, attrs: { target: [main, ...parts].join("."), label: text.slice(match.index, end), ...shared } };
175
225
  const [plural, roman] = [(match.groups?.["word"] ?? "").endsWith("s"), /^[IVXLC]+$/u.test(match.groups?.["n"] ?? "")];
176
226
  return [first, ...membersAfter(text, end, [main, ...parts], shared, plural, roman)];