@chaffjs/lang-en 0.34.0 → 0.36.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 (121) hide show
  1. package/dist/citation.d.ts +5 -0
  2. package/dist/citation.d.ts.map +1 -1
  3. package/dist/citation.js +29 -3
  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/names-read-as-verbs.d.ts +21 -0
  25. package/dist/names-read-as-verbs.d.ts.map +1 -0
  26. package/dist/names-read-as-verbs.js +40 -0
  27. package/dist/names-read-as-verbs.js.map +1 -0
  28. package/dist/pos.d.ts.map +1 -1
  29. package/dist/pos.js +30 -1
  30. package/dist/pos.js.map +1 -1
  31. package/dist/structure.d.ts.map +1 -1
  32. package/dist/structure.js +67 -9
  33. package/dist/structure.js.map +1 -1
  34. package/lexicons/abbreviated-label.yaml +2 -0
  35. package/lexicons/ai-tell.yaml +61 -27
  36. package/lexicons/alternative-clause-label.yaml +6 -0
  37. package/lexicons/announcing-opener.yaml +7 -0
  38. package/lexicons/article-sound.yaml +1 -0
  39. package/lexicons/capital-phrase.yaml +3 -1
  40. package/lexicons/case-party-joiner.yaml +7 -0
  41. package/lexicons/citation-name-connector.yaml +28 -0
  42. package/lexicons/citation-series.yaml +5 -1
  43. package/lexicons/clause-conjunction.yaml +7 -0
  44. package/lexicons/closing-cliche.yaml +9 -0
  45. package/lexicons/closing-exhortation.yaml +11 -0
  46. package/lexicons/cold-open.yaml +30 -0
  47. package/lexicons/common-acronym.yaml +21 -1
  48. package/lexicons/comparative-baseline.yaml +27 -0
  49. package/lexicons/condition-heading.yaml +21 -0
  50. package/lexicons/conjunction-lookalike.yaml +13 -0
  51. package/lexicons/connective-opener.yaml +23 -0
  52. package/lexicons/contraction-head.yaml +10 -0
  53. package/lexicons/contrast-amount.yaml +13 -0
  54. package/lexicons/contrast-lead.yaml +23 -0
  55. package/lexicons/contrast-turn.yaml +5 -0
  56. package/lexicons/definition-scope-lead.yaml +11 -0
  57. package/lexicons/determiner-after-article.yaml +14 -0
  58. package/lexicons/drug-stem.yaml +48 -0
  59. package/lexicons/elision-mark.yaml +9 -0
  60. package/lexicons/email-closing.yaml +7 -1
  61. package/lexicons/email-salutation-end.yaml +7 -0
  62. package/lexicons/email-stock-courtesy.yaml +29 -0
  63. package/lexicons/enclosing-unit.yaml +14 -0
  64. package/lexicons/everyday-name.yaml +24 -0
  65. package/lexicons/extra-word.yaml +28 -1
  66. package/lexicons/figure-label-plural.yaml +19 -0
  67. package/lexicons/figure-number-joiner.yaml +20 -0
  68. package/lexicons/headnote-heading.yaml +8 -0
  69. package/lexicons/honorific.yaml +3 -0
  70. package/lexicons/indefinite-article.yaml +8 -0
  71. package/lexicons/initial-lead-word.yaml +7 -0
  72. package/lexicons/intent-phrase.yaml +21 -0
  73. package/lexicons/interviewer-mark.yaml +10 -0
  74. package/lexicons/link-label-ending.yaml +6 -0
  75. package/lexicons/list-conjunction.yaml +7 -0
  76. package/lexicons/list-continues.yaml +9 -0
  77. package/lexicons/name-abbreviation.yaml +21 -0
  78. package/lexicons/name-capitals-prefix.yaml +16 -0
  79. package/lexicons/name-direction-word.yaml +10 -0
  80. package/lexicons/name-title-head.yaml +23 -0
  81. package/lexicons/name-word-ending.yaml +10 -0
  82. package/lexicons/navigation-line.yaml +11 -0
  83. package/lexicons/newsletter-issue.yaml +9 -0
  84. package/lexicons/numbered-division.yaml +4 -1
  85. package/lexicons/open-list-end.yaml +10 -0
  86. package/lexicons/over-general-claim.yaml +62 -0
  87. package/lexicons/padded-intro.yaml +14 -0
  88. package/lexicons/placeholder-division.yaml +7 -0
  89. package/lexicons/prepositive-abbreviation.yaml +10 -0
  90. package/lexicons/product-name.yaml +9 -0
  91. package/lexicons/prohibition-opener.yaml +6 -0
  92. package/lexicons/question-wording-lead.yaml +7 -0
  93. package/lexicons/rejected-wording.yaml +8 -0
  94. package/lexicons/request-deadline-section.yaml +11 -0
  95. package/lexicons/request-prohibition.yaml +8 -0
  96. package/lexicons/sentence-start-word.yaml +64 -0
  97. package/lexicons/session-law.yaml +19 -0
  98. package/lexicons/superlative-goal.yaml +40 -0
  99. package/lexicons/superlative-part.yaml +22 -0
  100. package/lexicons/superlative-phrase.yaml +21 -0
  101. package/lexicons/superlative-purpose.yaml +7 -0
  102. package/lexicons/superlative-referent.yaml +15 -0
  103. package/lexicons/superlative-scope.yaml +5 -1
  104. package/lexicons/telling-cue.yaml +6 -0
  105. package/lexicons/template-heading.yaml +35 -0
  106. package/lexicons/text-message-verb.yaml +11 -0
  107. package/lexicons/uninflected-word.yaml +22 -0
  108. package/lexicons/unprompted-denial.yaml +113 -0
  109. package/lexicons/word-suffix.yaml +34 -1
  110. package/lexicons/wordy-phrase.yaml +2 -2
  111. package/lexicons/year-reference.yaml +12 -0
  112. package/package.json +1 -1
  113. package/src/citation.ts +33 -3
  114. package/src/index.ts +13 -1
  115. package/src/initial-stop.ts +80 -0
  116. package/src/item-style.ts +33 -7
  117. package/src/law-citation.ts +68 -0
  118. package/src/lead-initial.ts +28 -0
  119. package/src/names-read-as-verbs.ts +65 -0
  120. package/src/pos.ts +33 -1
  121. package/src/structure.ts +73 -9
@@ -0,0 +1,65 @@
1
+ // A name the tagger reads as a verb or a pronoun because its lower-case spelling is one: AI as the "ai" of ain't, CA as the
2
+ // "ca" of can't, the US as "us", the IS department as "is". The tagger's vocabulary is keyed by the lower-case word, so it
3
+ // cannot tell the two apart; the sentence can.
4
+
5
+ type TaggedWord = { readonly value: string; readonly pos: string; readonly lemma?: string };
6
+
7
+ const CAPITALS = /^\p{Lu}{2,}$/u;
8
+
9
+ /** Penn tags a name is never given: the verb forms, the modal and the personal pronoun. */
10
+ const VERB_OR_PRONOUN = /^(?:VB[DGNPZ]?|MD|PRP)$/u;
11
+
12
+ /** The second half of a negative contraction, as the tagger splits it off (ca|n't). */
13
+ const NEGATION = "n't";
14
+
15
+ export type NameReadings = {
16
+ /** The first halves of a negative contraction, lower case (ai, ca, wo, sha). */
17
+ readonly contractionHeads: ReadonlySet<string>;
18
+ /** Whether a word opens a noun phrase that a verb or a pronoun cannot follow: an article or a possessive. */
19
+ readonly opensNounPhrase: (word: TaggedWord) => boolean;
20
+ };
21
+
22
+ /** A contraction's first half with no n't after it is not the contraction (AI is changing, lives in CA). */
23
+ const isUnpairedContractionHead = (tagged: readonly TaggedWord[], at: number, heads: ReadonlySet<string>): boolean =>
24
+ heads.has((tagged[at]?.value ?? "").toLowerCase()) && tagged[at + 1]?.value.toLowerCase() !== NEGATION;
25
+
26
+ /**
27
+ * Capitals right after an article or a possessive (the US office, our IT team, the company's IS staff) stand where a noun
28
+ * stands. Capitals elsewhere stay as the tagger read them: "I AM here", "it IS done" and "a hurricane DID form" are emphasis,
29
+ * and "That IS correct" opens with a demonstrative, not an article.
30
+ */
31
+ const isCapitalsAfterOpener = (tagged: readonly TaggedWord[], at: number, opensNounPhrase: NameReadings["opensNounPhrase"]): boolean => {
32
+ const previous = tagged[at - 1];
33
+ return CAPITALS.test(tagged[at]?.value ?? "") && previous !== undefined && opensNounPhrase(previous);
34
+ };
35
+
36
+ /** The positions of words the tagger read as a verb or a pronoun that the sentence shows to be names. */
37
+ export const namesReadAsVerbs = (tagged: readonly TaggedWord[], readings: NameReadings): readonly number[] =>
38
+ tagged.flatMap((word, at) =>
39
+ VERB_OR_PRONOUN.test(word.pos) &&
40
+ (isUnpairedContractionHead(tagged, at, readings.contractionHeads) || isCapitalsAfterOpener(tagged, at, readings.opensNounPhrase))
41
+ ? [at]
42
+ : [],
43
+ );
44
+
45
+ const asName = (word: TaggedWord): TaggedWord => ({ value: word.value, pos: "NNP", lemma: word.value.toLowerCase() });
46
+
47
+ /**
48
+ * The sentence read again with each name at `positions` replaced by `placeholder`, a word the tagger does not know: the names
49
+ * become proper nouns, and their neighbours take the tagger's reading of a sentence with a name there (the US team: team is a
50
+ * noun, not a verb after "us"). When the two readings do not line up word for word, only the names change.
51
+ */
52
+ export const rereadAsNames = (
53
+ entries: readonly TaggedWord[],
54
+ again: readonly TaggedWord[],
55
+ positions: readonly number[],
56
+ placeholder: string,
57
+ ): readonly TaggedWord[] => {
58
+ const names = new Set(positions);
59
+ const aligned = again.length === entries.length && again.every((word, at) => word.value === (names.has(at) ? placeholder : entries[at]?.value));
60
+ const source = aligned ? again : entries;
61
+ return source.map((word, at) => {
62
+ const original = entries[at] ?? word;
63
+ return names.has(at) ? asName(original) : { ...word, value: original.value };
64
+ });
65
+ };
package/src/pos.ts CHANGED
@@ -6,6 +6,7 @@ import { capitalisedGuessedVerbsAsNames, isOrdinaryWord, lowercasedAt, properNou
6
6
  import { isStativeParticiple, stativeVocabulary } from "./stative-participle.ts";
7
7
  import { isCapitalisedOrdinaryWord, isEmphasisedAdverb, isEmphasisedWord, isHyphenated, standsAlone } from "./emphasis.ts";
8
8
  import { gerundFeatures } from "./gerund.ts";
9
+ import { namesReadAsVerbs, rereadAsNames, type NameReadings } from "./names-read-as-verbs.ts";
9
10
  import { winkLexiconWords, winkPosTagger, winkWordNet } from "./wink-modules.ts";
10
11
 
11
12
  /** wink は CommonJS で、辞書を同期に持つ。初期化は 130 ms ほど。 */
@@ -164,6 +165,12 @@ const OBJECT_OR_POSSESSIVE = wordsOf("object-or-possessive");
164
165
 
165
166
  const POSSESSIVE_TAG = new Set(["PRP$", "WP$"]);
166
167
 
168
+ const NAME_READINGS: NameReadings = {
169
+ contractionHeads: wordsOf("contraction-head"),
170
+ opensNounPhrase: (word) =>
171
+ ARTICLES.has(word.value.toLowerCase()) || word.pos === "POS" || (word.pos === "PRP$" && !OBJECT_OR_POSSESSIVE.has(word.value.toLowerCase())),
172
+ };
173
+
167
174
  type Features = { features?: Readonly<Record<string, string>> };
168
175
 
169
176
  /** 冠詞(PronType=Art)と所有の語(Poss=Yes)は、名詞の前に一つしか立たない。二つ並ぶ our the platform は書き損じ。 */
@@ -306,10 +313,35 @@ const withSentenceInitialCase = (tagger: Tagger, text: string, entries: readonly
306
313
  return lowered === undefined ? entries : rereadAt(entries, at, tagged(tagger, lowered));
307
314
  };
308
315
 
316
+ /** A word the tagger's vocabulary does not hold. The tagger reads such capitals as a name, as it reads UN. */
317
+ const UNKNOWN_NAME = "XQZ";
318
+
319
+ /** text with each word at positions replaced by UNKNOWN_NAME, found in order as locate finds them. */
320
+ const withUnknownNamesAt = (text: string, entries: readonly Tagged[], positions: readonly number[]): string => {
321
+ const parts: string[] = [];
322
+ let cursor = 0;
323
+ entries.forEach((entry, at) => {
324
+ const start = text.indexOf(entry.value, cursor);
325
+ if (start === -1) return;
326
+ parts.push(text.slice(cursor, start), positions.includes(at) ? UNKNOWN_NAME : entry.value);
327
+ cursor = start + entry.value.length;
328
+ });
329
+ parts.push(text.slice(cursor));
330
+ return parts.join("");
331
+ };
332
+
333
+ /** Names the tagger read as a verb or a pronoun (AI, the US): the sentence is read again with a name the tagger does not know in their place. */
334
+ const withNamesReread = (tagger: Tagger, text: string, entries: readonly Tagged[]): readonly Tagged[] => {
335
+ const positions = namesReadAsVerbs(entries, NAME_READINGS);
336
+ if (positions.length === 0) return entries;
337
+ return rereadAsNames(entries, tagged(tagger, withUnknownNamesAt(text, entries, positions)), positions, UNKNOWN_NAME);
338
+ };
339
+
309
340
  export const tokenize = (text: string): Token[] | undefined => {
310
341
  const tagger = state.ready;
311
342
  if (tagger === undefined) return undefined;
312
343
  // 語は解析させた字(don't)で返す。語彙表の語と同じ字で比べられる。span は本文を指したまま。
313
344
  const words = straightApostrophes(blankLongRuns(text, RUN_LIMIT));
314
- return locate(words, capitalisedGuessedVerbsAsNames(withSentenceInitialCase(tagger, words, tagged(tagger, words)), state.vocabulary, state.senses));
345
+ const read = withNamesReread(tagger, words, tagged(tagger, words));
346
+ return locate(words, capitalisedGuessedVerbsAsNames(withSentenceInitialCase(tagger, words, read), state.vocabulary, state.senses));
315
347
  };
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";
@@ -28,20 +29,67 @@ const numberOf = (text: string | undefined): string | undefined => {
28
29
  const titleOf = (rest: string): string | undefined => {
29
30
  const trimmed = rest.replace(/^[.:\-–—\s]+/u, "").trim();
30
31
  if (trimmed !== "" && /^\p{Ll}/u.test(trimmed)) return undefined;
32
+ if (citesInSentence(rest)) return undefined;
31
33
  return trimmed;
32
34
  };
33
35
 
36
+ /** "(c) is brief", "(a) of the Act, 50 U.S.C.", "(15 USCA § 46), among other things": subdivisions or a bracketed citation. */
37
+ const LEADING_BRACKETS = /^(?:\s*\([^()]{1,40}\))+/u;
38
+
39
+ /**
40
+ * An opinion's paragraph or footnote that opens with a citation ("§ 331, 7 U.S.C. § 1331.", "Section 9(c) is brief…")
41
+ * cites another Act's section: a comma, or a lowercase word, right after the number and its bracketed parts. A heading's
42
+ * title follows its number without one ("Section 5 (Reserved).", "§ 3(a) Definitions").
43
+ */
44
+ const citesInSentence = (rest: string): boolean => {
45
+ const after = rest.replace(LEADING_BRACKETS, "").trimStart();
46
+ return after.startsWith(",") || (after !== rest.trimStart() && /^\p{Ll}/u.test(after));
47
+ };
48
+
34
49
  const ARTICLE = /^\s{0,3}(?:ARTICLE|Article)\s+(?<n>\d{1,3}|[IVXLC]{1,7})\b(?<rest>.*)$/u;
35
50
  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");
51
+ /**
52
+ * "Sec. 3. (a) For confidential…", '#### "SEC. 3. REQUIREMENT OF DISCLOSURE.': an Act or an Executive order set out in a
53
+ * statute's notes numbers its sections with the abbreviation, the number ending in a full stop; an Act quoted in the notes
54
+ * opens each paragraph with a quotation mark. "Sec. 552 - Public information" (a citation header) has no full stop.
55
+ */
56
+ const ABBREVIATED_SECTION = /^\s{0,3}["“]?(?:SECTION|Section|SEC\.|Sec\.)\s*(?<n>\d{1,3}(?:\.\d{1,3}){0,5})\.(?=\s|$)(?<rest>.*)$/u;
57
+ /**
58
+ * "(a) text", "(ii) text", "(B) text", or the label alone on its line with the text below it. A US statute joins a
59
+ * subsection's label to its first paragraph's ("(c)(1) Whenever…"): the rest then starts with the next label. A
60
+ * repealed item keeps its place in brackets ("[(D) Repealed. …]").
61
+ */
62
+ const LETTERED = new RegExp(
63
+ `^\\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*$`,
64
+ "u",
65
+ );
66
+
67
+ /**
68
+ * "Sec. 4. General Provisions. (a) Nothing in this order…": a title run in before the first item of the section's own list.
69
+ * The rest then starts with that label, which opens the item as a joined label does. A title is a few words spaced as
70
+ * words are, with no sentence in it: a wider gap is a page flattened into one line ("ARTICLE II EFFECT… Section 2.1
71
+ * Effect… (a)"), and "At the Effective Time, … thereof: (a) Each share" is the section's text, not its title.
72
+ */
73
+ const RUN_IN_FIRST_ITEM = /^(?<title>\S+(?: \S+){0,11}?[.:]) (?<item>\([a1]\)\s+\p{Lu}.*)$/u;
74
+ /** A lowercase word ending a sentence or a clause: "thereof: ", "Time. ". "U.S.C. " and "No. " end in capitals. */
75
+ const SENTENCE_END = /\p{Ll}[.:;] /u;
76
+ /** "(a) Prior to… (b) Each…": the next item on the same line is a list flattened into one paragraph, not one item. */
77
+ const NEXT_RUN_IN = /[.;:] \([b2]\) \p{Lu}/u;
78
+
79
+ const titleAndRest = (heading: string): { readonly heading: string; readonly rest: string } => {
80
+ const title = RUN_IN_FIRST_ITEM.exec(heading)?.groups?.["title"];
81
+ const item = title === undefined ? undefined : heading.slice(title.length + 1);
82
+ if (title === undefined || item === undefined || SENTENCE_END.test(title) || NEXT_RUN_IN.test(item)) return { heading, rest: heading };
83
+ return { heading: title, rest: item };
84
+ };
38
85
 
39
86
  const headed = (pattern: RegExp, line: string, numbering: string, label: (n: string) => string): NumberedLine | undefined => {
40
87
  const groups = pattern.exec(line)?.groups;
41
88
  const number = numberOf(groups?.["n"]);
42
- const heading = titleOf(groups?.["rest"] ?? "");
43
- if (groups === undefined || number === undefined || heading === undefined) return undefined;
89
+ const title = titleOf(groups?.["rest"] ?? "");
90
+ if (groups === undefined || number === undefined || title === undefined) return undefined;
44
91
  const parts = number.split(".");
92
+ const { heading, rest } = titleAndRest(title);
45
93
  return {
46
94
  kind: "article",
47
95
  depth: parts.length,
@@ -49,7 +97,7 @@ const headed = (pattern: RegExp, line: string, numbering: string, label: (n: str
49
97
  absolute: true,
50
98
  label: label(groups["n"] ?? ""),
51
99
  heading,
52
- rest: heading,
100
+ rest,
53
101
  ordinal: Number(parts.at(-1)),
54
102
  numbering,
55
103
  };
@@ -59,9 +107,12 @@ const lettered = (line: string, context: NumberingContext): NumberedLine | undef
59
107
  const groups = LETTERED.exec(line)?.groups;
60
108
  const raw = groups?.["n"];
61
109
  if (groups === undefined || raw === undefined) return undefined;
110
+ // "(a)(i) …", "(a)(ii) …": a list that writes its parent's label on every line repeats the open (a), and is its (ii).
111
+ const joined = groups["joined"];
112
+ if (joined !== undefined && context.open.some((open) => open.kind === "item" && open.label === `(${raw})`)) return lettered(joined, context);
62
113
  const style = styleOf(raw, context);
63
114
  const ordinal = style === undefined ? undefined : ordinalOf(raw, style);
64
- const rest = groups["rest"];
115
+ const rest = groups["rest"] ?? groups["joined"];
65
116
  if (style === undefined || (rest === undefined && !continuesOpen(style, ordinal, context))) return undefined;
66
117
  // 番地は書かれたままの "ii" を使う。参照「Section 4.2(a)(ii)」も同じ形で書かれるので、そのまま引ける。
67
118
  return {
@@ -94,6 +145,7 @@ const numbered = (line: string, context: NumberingContext): NumberedLine | undef
94
145
  chapter(CHAPTER, line, CHAPTER_DEPTH, "ch", "Chapter") ??
95
146
  headed(ARTICLE, line, "article", (n) => `Article ${n}`) ??
96
147
  headed(SECTION, line, "section", (n) => `Section ${n}`) ??
148
+ headed(ABBREVIATED_SECTION, line, "section", (n) => `Section ${n}`) ??
97
149
  lettered(line, context);
98
150
 
99
151
  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 +198,8 @@ const glossedDocument = (gloss: Gloss): string | undefined => {
146
198
 
147
199
  const LEXICONS = loadLexicons();
148
200
  const CODES = codeVocabulary(LEXICONS);
201
+ const LAWS = lawVocabulary(LEXICONS);
202
+ const ENCLOSING_UNITS: ReadonlySet<string> = new Set((LEXICONS["enclosing-unit"] ?? []).map((entry) => entry.pattern.toLowerCase()));
149
203
 
150
204
  /** The other document a reference names, or else a bracketed tag that the core checks against the document's list. */
151
205
  const citation = (text: string, start: number, end: number, document: string | undefined): Readonly<Record<string, string>> => {
@@ -154,6 +208,12 @@ const citation = (text: string, start: number, end: number, document: string | u
154
208
  return citedTag === undefined ? {} : { citedTag };
155
209
  };
156
210
 
211
+ /** "section 552 of this title": the unit of a code the reference says it is in, which the core checks against the tree. */
212
+ const enclosing = (text: string, end: number): Readonly<Record<string, string>> => {
213
+ const withinUnit = enclosingUnitAfter(text, end, ENCLOSING_UNITS);
214
+ return withinUnit === undefined ? {} : { withinUnit };
215
+ };
216
+
157
217
  /**
158
218
  * "Section 4.2(a)" → 4.2.a, "Article III" → 3. The same addresses the tree gives.
159
219
  * "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 +225,16 @@ const references = (text: string): Mention[] => {
165
225
  if (main === undefined) return [];
166
226
  const { parts, end } = subdivisions(text, match.index + match[0].length);
167
227
  advance(gloss, text, match.index);
168
- const cited = citedDocumentAfter(text, end) ?? citedDocumentBefore(text, match.index) ?? citedCodeBefore(text, match.index, CODES);
228
+ const cited =
229
+ citedDocumentAfter(text, end) ??
230
+ citedDocumentBefore(text, match.index) ??
231
+ citedCodeBefore(text, match.index, CODES) ??
232
+ citedLawAround(text, match.index, end, LAWS);
169
233
  const document = cited ?? glossedDocument(gloss);
170
234
  gloss.scanned = Math.max(gloss.scanned, end);
171
235
  if (cited !== undefined) gloss.anchors.push({ document: cited, depth: gloss.depth });
172
236
  const numbering = /^[Aa]/u.test(match.groups?.["word"] ?? "") ? "article" : "section";
173
- const shared = { numbering, ...citation(text, match.index, end, document) };
237
+ const shared = { numbering, ...enclosing(text, end), ...citation(text, match.index, end, document) };
174
238
  const first = { start: match.index, end, attrs: { target: [main, ...parts].join("."), label: text.slice(match.index, end), ...shared } };
175
239
  const [plural, roman] = [(match.groups?.["word"] ?? "").endsWith("s"), /^[IVXLC]+$/u.test(match.groups?.["n"] ?? "")];
176
240
  return [first, ...membersAfter(text, end, [main, ...parts], shared, plural, roman)];