@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,20 @@
1
+ # What joins the numbers after a plural figure-label word: group list names each number ("Tables 14, 15 and 17"),
2
+ # group range every number from the first to the last ("Tables 15 through 19", "Figures 2–4"). A hyphen is not one:
3
+ # "Table 2-1" is a sub-number.
4
+ id: figure-number-joiner
5
+ language: en
6
+ entries:
7
+ - pattern: ","
8
+ group: list
9
+ - pattern: and
10
+ group: list
11
+ - pattern: "&"
12
+ group: list
13
+ - pattern: through
14
+ group: range
15
+ - pattern: thru
16
+ group: range
17
+ - pattern: to
18
+ group: range
19
+ - pattern: –
20
+ group: range
@@ -0,0 +1,8 @@
1
+ # Headings of the summary placed above a judgment. What sits under one restates the body in short, so a term there is
2
+ # not its first use (use-before-definition). Read together with summary-heading; the whole heading must match.
3
+ id: headnote-heading
4
+ language: en
5
+ entries:
6
+ - pattern: Syllabus
7
+ - pattern: Headnote
8
+ - pattern: Headnotes
@@ -0,0 +1,7 @@
1
+ # Words that lead a person's name ("written by J. Smith", "Smith and C.W. Landsea"). After one, initials before a
2
+ # capitalised word that is not a sentence-start-word do not end the sentence. Matched as written.
3
+ id: initial-lead-word
4
+ language: en
5
+ entries:
6
+ - pattern: by
7
+ - pattern: and
@@ -0,0 +1,6 @@
1
+ # List items that end as the words of a link ("Apply online: click here"). The item points somewhere and is neither a
2
+ # sentence nor a name, even when the link itself was lost in a copy of the page. list-item-form-mix does not judge its form.
3
+ id: link-label-ending
4
+ language: en
5
+ entries:
6
+ - pattern: click here
@@ -0,0 +1,7 @@
1
+ # Words that only join the items of a list (defined-name-repeated). In "Registered Mail or Courier Delivery (together, the
2
+ # "Notice Methods")" the bracket names the whole list, not its last item.
3
+ id: list-conjunction
4
+ language: en
5
+ entries:
6
+ - pattern: and
7
+ - pattern: or
@@ -0,0 +1,21 @@
1
+ # Old written short forms of given names ("Jas. Whittaker", "Wm. Smith", "Joel Wm. Parker"). They stand only before a
2
+ # surname or another name, so their full stop never ends a sentence, even before a name and a comma ("Jas.
3
+ # Whittaker, J. Hocknell"). Forms that are also a whole name (Fred., Alex.) are left out: "I met Fred. Alex came."
4
+ # ends there. Matched as written.
5
+ id: name-abbreviation
6
+ language: en
7
+ entries:
8
+ - pattern: Benj.
9
+ - pattern: Chas.
10
+ - pattern: Danl.
11
+ - pattern: Edw.
12
+ - pattern: Geo.
13
+ - pattern: Jas.
14
+ - pattern: Jno.
15
+ - pattern: Jos.
16
+ - pattern: Nathl.
17
+ - pattern: Richd.
18
+ - pattern: Robt.
19
+ - pattern: Saml.
20
+ - pattern: Thos.
21
+ - pattern: Wm.
@@ -0,0 +1,9 @@
1
+ # The issue number of a newsletter sent on a schedule (Vol. 12), with … where the number stands. An issue number in the
2
+ # title (a heading before the first sentence) makes the document an issue sent to every subscriber, not a letter
3
+ # to one reader, and it takes no greeting and no closing.
4
+ id: newsletter-issue
5
+ language: en
6
+ entries:
7
+ - pattern: Vol. …
8
+ - pattern: Vol.…
9
+ - pattern: Volume …
@@ -1,11 +1,13 @@
1
1
  # Names of the divisions of a document that take a number after them (Part II, Section VIII, Title IV). A Roman
2
- # numeral right after one of these is the number, not an acronym. Matched as written, or in capitals (PART II).
2
+ # numeral right after one of these is the number, not an acronym. Matched as written, or in capitals (PART II). A US
3
+ # statute cites a Public Law's titles in lower case ("Pub. L. 101–73, title IX, §962").
3
4
  id: numbered-division
4
5
  language: en
5
6
  entries:
6
7
  - pattern: Part
7
8
  - pattern: Section
8
9
  - pattern: Title
10
+ - pattern: title
9
11
  - pattern: Chapter
10
12
  - pattern: Article
11
13
  - pattern: Appendix
@@ -18,6 +20,7 @@ entries:
18
20
  - pattern: Division
19
21
  - pattern: Subpart
20
22
  - pattern: Subtitle
23
+ - pattern: subtitle
21
24
  - pattern: Subchapter
22
25
  - pattern: Phase
23
26
  - pattern: Table
@@ -0,0 +1,11 @@
1
+ # Sweeping claims about people in general: what most people do, what nobody does. Read by over-general-claim, which
2
+ # counts them per length of the document; one is ordinary. "Most people agree" and "everyone knows" are weasel-word's.
3
+ id: over-general-claim
4
+ language: en
5
+ entries:
6
+ - pattern: most people
7
+ - pattern: for most of us
8
+ - pattern: for most households
9
+ - pattern: nobody
10
+ - pattern: everyone
11
+ - pattern: anyone who
@@ -0,0 +1,7 @@
1
+ # A division whose number is left blank in a template (numbering-gap): "Section [●]". The tree does not read it as a
2
+ # division, so the numbering of paragraphs starts again at the line it begins.
3
+ id: placeholder-division
4
+ language: en
5
+ entries:
6
+ - pattern: Section [●]
7
+ - pattern: Article [●]
@@ -0,0 +1,10 @@
1
+ # Abbreviations in capitals with full stops that stand before a name as an adjective ("U.S. Census Bureau", "U.K.
2
+ # Parliament"). Before a capitalised word that is not a sentence-start-word, their full stop does not end the sentence.
3
+ # Matched as written.
4
+ id: prepositive-abbreviation
5
+ language: en
6
+ entries:
7
+ - pattern: U.S.
8
+ - pattern: U.K.
9
+ - pattern: U.N.
10
+ - pattern: E.U.
@@ -0,0 +1,7 @@
1
+ # Words right before a quoted term that ask about it ("a definition of what “done” means", "how “ready” is decided").
2
+ # The sentence asks what the term means and defines nothing, so unused-definition does not report the term.
3
+ id: question-wording-lead
4
+ language: en
5
+ entries:
6
+ - pattern: what
7
+ - pattern: how
@@ -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
@@ -6,4 +6,7 @@ entries:
6
6
  - pattern: etc.
7
7
  - pattern: and so on
8
8
  - pattern: and the like
9
+ # In a contract "including, but not limited to" is a term of art: it keeps a court from narrowing the defined class to
10
+ # the examples listed, so the class named before it decides the scope. Specifications and documentation only.
9
11
  - pattern: but not limited to
12
+ genres: [technical, docs]
@@ -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
@@ -1,5 +1,6 @@
1
1
  # Words that join the start and the end of a period. date-without-year does not report the start of a period whose end
2
- # gives its year in words ("April 1 to March 31 of the following year"): like a fiscal year, it holds for any year.
2
+ # gives its year in words ("April 1 to March 31 of the following year"): like a fiscal year, it holds for any year. The
3
+ # start may name its year relative to the end ("6 April in one year to 5 April in the next"); those words join too.
3
4
  id: year-span-joiner
4
5
  language: en
5
6
  entries:
@@ -7,6 +8,9 @@ entries:
7
8
  - pattern: through
8
9
  - pattern: until
9
10
  - pattern: till
11
+ - pattern: in one year to
12
+ - pattern: of one year to
13
+ - pattern: one year to
10
14
  - pattern: "-"
11
15
  - pattern: –
12
16
  - pattern: —
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaffjs/lang-en",
3
- "version": "0.33.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);