@mailwoman/record 9.4.0 → 10.1.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.
- package/README.md +14 -14
- package/lib/address.ts +41 -49
- package/lib/name.ts +77 -15
- package/lib/organization.ts +42 -68
- package/out/address.d.ts +29 -47
- package/out/address.d.ts.map +1 -1
- package/out/address.js +6 -16
- package/out/address.js.map +1 -1
- package/out/name.d.ts +29 -7
- package/out/name.d.ts.map +1 -1
- package/out/name.js +57 -35
- package/out/name.js.map +1 -1
- package/out/organization.d.ts +23 -47
- package/out/organization.d.ts.map +1 -1
- package/out/organization.js +26 -59
- package/out/organization.js.map +1 -1
- package/package.json +16 -38
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# @mailwoman/record
|
|
2
2
|
|
|
3
|
-
**
|
|
4
|
-
resolution matcher.
|
|
5
|
-
|
|
6
|
-
geocode. Organization and contact records build on the same canonical record.
|
|
3
|
+
This package provides the **record schema and per-field normalizers** for the
|
|
4
|
+
geocode-first entity resolution matcher. The design starts from the address. The
|
|
5
|
+
canonical `PostalAddress` record combines parsed address components, the
|
|
6
|
+
formatter's match key, and a resolved geocode. Organization and contact records build on the same canonical record.
|
|
7
7
|
|
|
8
8
|
```ts
|
|
9
9
|
import { PostalAddress, parsePersonName, canonicalizeOrganizationName } from "@mailwoman/record";
|
|
@@ -50,24 +50,24 @@ import { canonicalizeOrganizationName, type CanonicalizeOrgOpts } from "@mailwom
|
|
|
50
50
|
|
|
51
51
|
## Design
|
|
52
52
|
|
|
53
|
-
- **Plain data
|
|
53
|
+
- **Plain data without classes.** Records are plain TypeScript objects with
|
|
54
54
|
branded types where needed.
|
|
55
|
-
- **Address-first.**
|
|
56
|
-
matcher resolves places
|
|
55
|
+
- **Address-first.** `PostalAddress` is the canonical record, because the
|
|
56
|
+
geocode-first matcher resolves places and not strings.
|
|
57
57
|
- **Domain-scoped.** Organization canonicalization supports jurisdiction and
|
|
58
58
|
domain context (e.g., `{jurisdiction: "ID"}` for Indonesian legal designations,
|
|
59
|
-
`{domain: "healthcare"}` to
|
|
59
|
+
`{domain: "healthcare"}` to keep PT/SCA from colliding with medical
|
|
60
60
|
abbreviations).
|
|
61
|
-
- **Lean dependencies.**
|
|
62
|
-
canonical key.
|
|
61
|
+
- **Lean dependencies.** The package depends only on `@mailwoman/formatter`, for
|
|
62
|
+
the canonical key.
|
|
63
63
|
|
|
64
64
|
## Related
|
|
65
65
|
|
|
66
|
-
- [`@mailwoman/match`](../match)
|
|
67
|
-
- [`@mailwoman/formatter`](../formatter)
|
|
68
|
-
- [`@mailwoman/registry`](../registry)
|
|
66
|
+
- [`@mailwoman/match`](../match): the fuzzy matcher that consumes these records.
|
|
67
|
+
- [`@mailwoman/formatter`](../formatter): provides the `canonicalKey` that `PostalAddress` uses.
|
|
68
|
+
- [`@mailwoman/registry`](../registry): the high-level `resolveEntities` that uses records.
|
|
69
69
|
- [Geocode-First Record Matching](https://mailwoman.ai/articles/concepts/geocode-first-record-matching/)
|
|
70
70
|
|
|
71
71
|
## License
|
|
72
72
|
|
|
73
|
-
[AGPL-3.0-only](https://www.gnu.org/licenses/
|
|
73
|
+
[AGPL-3.0-only](https://www.gnu.org/licenses/AGPL-3.0.html)
|
package/lib/address.ts
CHANGED
|
@@ -2,38 +2,12 @@
|
|
|
2
2
|
* @copyright Sister Software
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* The canonical address record — the matcher's unit of address identity, and the canonical record
|
|
7
|
-
* the organization and contact records build on.
|
|
8
|
-
*
|
|
9
|
-
* It is plain data: parser components + the formatter's match key + an optional resolved geocode,
|
|
10
|
-
* composed into one object. No ORM, no decorators, no schema-generation implementation — if we need a
|
|
11
|
-
* database we reach for Kysely at the call site, not a model layer here.
|
|
12
|
-
*
|
|
13
|
-
* The geocode fields mirror mailwoman's `GeocodeResult` (tier + calibrated uncertainty + hierarchy)
|
|
14
|
-
* on purpose: that is the location signal the Fellegi-Sunter scorer weights its distance evidence
|
|
15
|
-
* by — two records sharing a `address_point` coordinate is strong agreement; sharing an
|
|
16
|
-
* `interpolated` centroid is weak; a PO-box / multi-unit coordinate is barely location agreement
|
|
17
|
-
* at all (the NAACCR precedent, see the geocode-first record-matching concept doc).
|
|
18
5
|
*/
|
|
19
6
|
|
|
20
|
-
import
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*/
|
|
25
|
-
export interface GeoCoordinate {
|
|
26
|
-
latitude: number
|
|
27
|
-
longitude: number
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* The resolution tier that produced a coordinate, mirroring mailwoman's geocoder (`address_point` > `interpolated` >
|
|
32
|
-
* `street` > `admin`). Kept as a local plain union so this package stays decoupled from the heavy geocoder runtime; a
|
|
33
|
-
* `GeocodeResult.resolution_tier` maps in directly. (`street` = a street centroid for a street-only query, #1042 —
|
|
34
|
-
* coarser than a house-number estimate, finer than an admin centroid.)
|
|
35
|
-
*/
|
|
36
|
-
export type ResolutionTier = "address_point" | "interpolated" | "street" | "admin" | "venue" | "plus_code"
|
|
7
|
+
import type { ResolutionTier } from "@mailwoman/annotations/geo"
|
|
8
|
+
import { type ComponentDict, type FormatAddressOptions, formatAddress } from "@mailwoman/codex/address/format"
|
|
9
|
+
import { canonicalKey } from "@mailwoman/codex/address/key"
|
|
10
|
+
import type { GeoCoordinate } from "@mailwoman/spatial"
|
|
37
11
|
|
|
38
12
|
/**
|
|
39
13
|
* One resolved admin-hierarchy ancestor (most specific first), for spelling-invariant blocking.
|
|
@@ -50,47 +24,58 @@ export interface HierarchyNode {
|
|
|
50
24
|
export interface AddressGeocode {
|
|
51
25
|
coordinate: GeoCoordinate
|
|
52
26
|
tier: ResolutionTier
|
|
27
|
+
|
|
53
28
|
/**
|
|
54
|
-
*
|
|
29
|
+
* Gives the uncertainty radius in meters.
|
|
30
|
+
*
|
|
31
|
+
* It is `null` on the admin tier and whenever the tier reports none.
|
|
55
32
|
*/
|
|
56
33
|
uncertaintyMeters: number | null
|
|
34
|
+
|
|
57
35
|
/**
|
|
58
|
-
*
|
|
36
|
+
* Lists the resolved admin hierarchy from the locality up to the country.
|
|
59
37
|
*/
|
|
60
38
|
hierarchy?: HierarchyNode[]
|
|
39
|
+
|
|
61
40
|
/**
|
|
62
|
-
*
|
|
41
|
+
* Marks a delivery point, such as a PO box, rather than a building,
|
|
42
|
+
* so the coordinate does not locate the addressee.
|
|
63
43
|
*/
|
|
64
44
|
poBox?: boolean
|
|
45
|
+
|
|
65
46
|
/**
|
|
66
|
-
*
|
|
47
|
+
* Marks a multi-unit building whose records share one coordinate,
|
|
48
|
+
* so the coordinate cannot distinguish units.
|
|
67
49
|
*/
|
|
68
50
|
multiUnit?: boolean
|
|
69
51
|
}
|
|
70
52
|
|
|
71
53
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
54
|
+
* Describes the canonical address record with parsed components plus a match key.
|
|
55
|
+
*
|
|
56
|
+
* It may also include formatted text, raw input or a resolved geocode.
|
|
74
57
|
*/
|
|
75
58
|
export interface PostalAddress {
|
|
76
59
|
/**
|
|
77
|
-
*
|
|
60
|
+
* Holds the parsed address components keyed by component tag.
|
|
78
61
|
*/
|
|
79
62
|
components: ComponentDict
|
|
63
|
+
|
|
80
64
|
/**
|
|
81
|
-
*
|
|
65
|
+
* Holds the normalized, deterministic match key used for blocking, built by
|
|
66
|
+
* `canonicalKey` from `@mailwoman/codex/address/key`.
|
|
82
67
|
*/
|
|
83
68
|
canonicalKey: string
|
|
69
|
+
|
|
84
70
|
/**
|
|
85
|
-
*
|
|
71
|
+
* Gives a human-readable single-line form for display.
|
|
86
72
|
*/
|
|
87
73
|
formatted?: string
|
|
88
|
-
|
|
89
|
-
* Resolved location, when geocoded.
|
|
90
|
-
*/
|
|
74
|
+
|
|
91
75
|
geocode?: AddressGeocode
|
|
76
|
+
|
|
92
77
|
/**
|
|
93
|
-
*
|
|
78
|
+
* Keeps the original free-text input for provenance.
|
|
94
79
|
*/
|
|
95
80
|
raw?: string
|
|
96
81
|
}
|
|
@@ -100,26 +85,33 @@ export interface PostalAddress {
|
|
|
100
85
|
*/
|
|
101
86
|
export interface ToPostalAddressOptions {
|
|
102
87
|
/**
|
|
103
|
-
*
|
|
88
|
+
* Supplies the country for formatting as an ISO-2 code or a name.
|
|
89
|
+
* It defaults to the `country` component.
|
|
104
90
|
*/
|
|
105
91
|
country?: string
|
|
92
|
+
|
|
106
93
|
/**
|
|
107
|
-
*
|
|
94
|
+
* Supplies the original free-text input to keep as provenance.
|
|
108
95
|
*/
|
|
109
96
|
raw?: string
|
|
97
|
+
|
|
110
98
|
/**
|
|
111
|
-
*
|
|
99
|
+
* Indicates whether to compute the `formatted` string.
|
|
100
|
+
* It defaults to `true`.
|
|
112
101
|
*/
|
|
113
102
|
format?: boolean
|
|
103
|
+
|
|
114
104
|
/**
|
|
115
|
-
*
|
|
105
|
+
* Passes options to the formatter, defaulting to a single line joined with `", "`.
|
|
116
106
|
*/
|
|
117
107
|
formatOptions?: FormatAddressOptions
|
|
118
108
|
}
|
|
119
109
|
|
|
120
110
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
111
|
+
* Builds a {@linkcode PostalAddress} from parsed components, always filling the match key
|
|
112
|
+
* and, unless `format` is `false`, the formatted address.
|
|
113
|
+
*
|
|
114
|
+
* Attach a geocode separately with {@linkcode withGeocode}.
|
|
123
115
|
*/
|
|
124
116
|
export function toPostalAddress(components: ComponentDict, opts: ToPostalAddressOptions = {}): PostalAddress {
|
|
125
117
|
const country = opts.country ?? components.country ?? ""
|
package/lib/name.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { TextSpliterator } from "spliterator"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* @copyright Sister Software
|
|
3
5
|
* @license AGPL-3.0
|
|
@@ -14,19 +16,21 @@
|
|
|
14
16
|
*
|
|
15
17
|
* Scope + honesty (per the name-canonicalization research pass):
|
|
16
18
|
*
|
|
17
|
-
* - Western / romanized names only. Cultural given-family
|
|
19
|
+
* - Western / romanized names only. Cultural given-family order variation (East-Asian family-first)
|
|
18
20
|
* and transliteration are not handled here — a documented follow-up.
|
|
19
|
-
* - Nickname → canonical-root mapping is intentionally
|
|
21
|
+
* - Nickname → canonical-root mapping is intentionally not done at parse time: it is lossy and
|
|
20
22
|
* gendered (Bobbie → Robert _or_ Roberta), so equivalence belongs in the matcher as a fuzzy
|
|
21
|
-
* agreement level
|
|
23
|
+
* agreement level rather than a destructive rewrite. We only _extract_ a parenthetical/quoted
|
|
22
24
|
* nickname.
|
|
23
|
-
* - A CRF parser (probablepeople) is the gold-standard reference but too heavy to port
|
|
25
|
+
* - A CRF parser (probablepeople) is the gold-standard reference but too heavy to port. this
|
|
24
26
|
* positional parser covers the documented hard cases (inversion, particles, generational +
|
|
25
27
|
* professional suffixes) without a model.
|
|
26
28
|
*/
|
|
27
29
|
|
|
28
30
|
/**
|
|
29
|
-
* A parsed person name.
|
|
31
|
+
* A parsed person name.
|
|
32
|
+
*
|
|
33
|
+
* All fields optional — the parser fills what it can identify.
|
|
30
34
|
*/
|
|
31
35
|
export interface PersonName {
|
|
32
36
|
/**
|
|
@@ -60,7 +64,9 @@ export interface PersonName {
|
|
|
60
64
|
}
|
|
61
65
|
|
|
62
66
|
/**
|
|
63
|
-
* Titles / salutations that lead a name.
|
|
67
|
+
* Titles / salutations that lead a name.
|
|
68
|
+
*
|
|
69
|
+
* Matched case-insensitively, trailing `.` ignored.
|
|
64
70
|
*/
|
|
65
71
|
const TITLES = new Set([
|
|
66
72
|
"airman",
|
|
@@ -161,8 +167,10 @@ const SUFFIXES = new Set([
|
|
|
161
167
|
])
|
|
162
168
|
|
|
163
169
|
/**
|
|
164
|
-
* Surname particles.
|
|
165
|
-
*
|
|
170
|
+
* Surname particles.
|
|
171
|
+
*
|
|
172
|
+
* Consecutive particles fold together (`de` + `la` → `de la`), and the next
|
|
173
|
+
* non-particle token begins the bare surname.
|
|
166
174
|
*/
|
|
167
175
|
const PARTICLES = new Set([
|
|
168
176
|
"al",
|
|
@@ -204,8 +212,10 @@ const norm = (token: string): string => token.replace(/\.$/, "").toLowerCase()
|
|
|
204
212
|
const countChar = (s: string, c: string): number => s.split(c).length - 1
|
|
205
213
|
|
|
206
214
|
/**
|
|
207
|
-
* Parse a full name into components.
|
|
208
|
-
*
|
|
215
|
+
* Parse a full name into components.
|
|
216
|
+
*
|
|
217
|
+
* @returns `null` for empty input.
|
|
218
|
+
* Best-effort and non-throwing — ambiguous input degrades gracefully rather than erroring.
|
|
209
219
|
*/
|
|
210
220
|
export function parsePersonName(input: string | null | undefined): PersonName | null {
|
|
211
221
|
if (!isPresent(input)) return null
|
|
@@ -234,7 +244,7 @@ export function parsePersonName(input: string | null | undefined): PersonName |
|
|
|
234
244
|
// 2. Resolve a single comma: "Last, First" inversion, unless the tail is a known suffix
|
|
235
245
|
// ("John Smith, Jr."), in which case keep order and treat the tail as a suffix.
|
|
236
246
|
if (countChar(working, ",") === 1) {
|
|
237
|
-
const [head, tail] =
|
|
247
|
+
const [head, tail] = TextSpliterator.from(working, { delimiter: "," }).toArray()
|
|
238
248
|
|
|
239
249
|
if (tail && tail.split(/\s+/).every((t) => SUFFIXES.has(norm(t)))) {
|
|
240
250
|
result.suffix = tail
|
|
@@ -244,11 +254,11 @@ export function parsePersonName(input: string | null | undefined): PersonName |
|
|
|
244
254
|
}
|
|
245
255
|
}
|
|
246
256
|
|
|
247
|
-
const tokens = working.split(/\s+/).filter((value) => value.length
|
|
257
|
+
const tokens = working.split(/\s+/).filter((value) => value.length)
|
|
248
258
|
|
|
249
259
|
if (!tokens.length) return Object.keys(result).length ? result : null
|
|
250
260
|
|
|
251
|
-
// 3.
|
|
261
|
+
// 3. Titles at the start become the prefix.
|
|
252
262
|
const prefixParts: string[] = []
|
|
253
263
|
|
|
254
264
|
while (tokens.length > 1 && TITLES.has(norm(tokens[0]!))) {
|
|
@@ -259,7 +269,7 @@ export function parsePersonName(input: string | null | undefined): PersonName |
|
|
|
259
269
|
result.prefix = prefixParts.join(" ")
|
|
260
270
|
}
|
|
261
271
|
|
|
262
|
-
// 4.
|
|
272
|
+
// 4. Suffixes at the end become the suffix, while one name token must remain.
|
|
263
273
|
const suffixParts: string[] = []
|
|
264
274
|
|
|
265
275
|
while (tokens.length > 1 && SUFFIXES.has(norm(tokens.at(-1)!))) {
|
|
@@ -272,7 +282,8 @@ export function parsePersonName(input: string | null | undefined): PersonName |
|
|
|
272
282
|
|
|
273
283
|
if (!tokens.length) return result
|
|
274
284
|
|
|
275
|
-
// 5. Locate the surname particle run
|
|
285
|
+
// 5. Locate the surname particle run.
|
|
286
|
+
// Everything from it onward is the (particled) surname.
|
|
276
287
|
let particleStart = -1
|
|
277
288
|
|
|
278
289
|
for (let i = 0; i < tokens.length; i++) {
|
|
@@ -325,3 +336,54 @@ export function parsePersonName(input: string | null | undefined): PersonName |
|
|
|
325
336
|
|
|
326
337
|
return result
|
|
327
338
|
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* The order a person name prints in.
|
|
342
|
+
*
|
|
343
|
+
* Western / romanized only, the same scope {@linkcode parsePersonName} declares.
|
|
344
|
+
* A family-first system does not use this order in reverse.
|
|
345
|
+
* An invented order here would be worse than having none.
|
|
346
|
+
*/
|
|
347
|
+
const NAME_ORDER: readonly (keyof PersonName)[] = ["prefix", "given", "middle", "familyParticle", "family", "suffix"]
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The parts {@linkcode formatPersonName} prints for the short style.
|
|
351
|
+
*
|
|
352
|
+
* What a person is addressed by, without the title, the nickname or the letters after.
|
|
353
|
+
*/
|
|
354
|
+
const SHORT_NAME_ORDER: readonly (keyof PersonName)[] = ["given", "familyParticle", "family"]
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* How much of the name to print.
|
|
358
|
+
*/
|
|
359
|
+
export type PersonNameStyle = "full" | "short"
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Render a {@linkcode PersonName} back to a string — the inverse of {@linkcode parsePersonName}.
|
|
363
|
+
*
|
|
364
|
+
* `"full"` prints every part the parser identified except the nickname.
|
|
365
|
+
* A nickname is an alternative to the given name rather than an addition to it.
|
|
366
|
+
*
|
|
367
|
+
* `"short"` prints what a person is addressed by.
|
|
368
|
+
* Display labels and match keys use this form.
|
|
369
|
+
*
|
|
370
|
+
* The particle travels with the surname in both styles.
|
|
371
|
+
* The parser stores it separately so the matcher can compare `Vega` independently of `de la`;
|
|
372
|
+
* printing them apart would produce a form absent from the input name.
|
|
373
|
+
*/
|
|
374
|
+
export function formatPersonName(name: PersonName | null | undefined, style: PersonNameStyle = "full"): string {
|
|
375
|
+
if (!name) return ""
|
|
376
|
+
|
|
377
|
+
const order = style === "short" ? SHORT_NAME_ORDER : NAME_ORDER
|
|
378
|
+
const parts: string[] = []
|
|
379
|
+
|
|
380
|
+
for (const part of order) {
|
|
381
|
+
const value = name[part]?.trim()
|
|
382
|
+
|
|
383
|
+
if (value) {
|
|
384
|
+
parts.push(value)
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return parts.join(" ")
|
|
389
|
+
}
|
package/lib/organization.ts
CHANGED
|
@@ -3,42 +3,12 @@
|
|
|
3
3
|
* @license AGPL-3.0
|
|
4
4
|
* @author Teffen Ellis, et al.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* `Corporation`, `Limited`, `LLC`) are normalized away before matching, so `Acme Corp` and `Acme
|
|
10
|
-
* Corporation, LLC` collapse to the same key. We also split off a `doing business as` clause and
|
|
11
|
-
* normalize connectives (`&` → `and`), punctuation, accents, and a leading `The`.
|
|
12
|
-
*
|
|
13
|
-
* **The collision problem (#668).** A legal-form token in one jurisdiction is a meaningful word in
|
|
14
|
-
* another domain. `PT` is Indonesia's `Perseroan Terbatas` (its LLC) — and US-healthcare
|
|
15
|
-
* shorthand for _Physical Therapy_. `SCA` / `SCS` are French/Belgian/Luxembourg commandite forms
|
|
16
|
-
* — and, in a clinic's name, _Sudden Cardiac Arrest_ / _Spinal Cord Stimulator_. A single
|
|
17
|
-
* universal strip-list can't be right for both: strip `PT` and you corrupt `Lakeside PT`; keep it
|
|
18
|
-
* and you leave the legal form on an Indonesian company. So the strip-set is computed on **two
|
|
19
|
-
* axes**:
|
|
20
|
-
*
|
|
21
|
-
* - **jurisdiction** (ISO 3166-1 alpha-2, e.g. from the resolved address country) — _adds_ the legal
|
|
22
|
-
* forms valid in that country. Collision-prone forms (`pt`, `sca`, `scs`) live here, admitted only
|
|
23
|
-
* behind a known jurisdiction, NOT in the universal base.
|
|
24
|
-
* - **domain** (an ingest-config tag, e.g. `healthcare`) — _protects_ domain-meaningful tokens from
|
|
25
|
-
* ever being stripped, even when a jurisdiction pack would add them. Domain protection wins.
|
|
26
|
-
*
|
|
27
|
-
* `effective = (base ∪ jurisdiction-pack) − domain-protect-pack`. With no options the set is the
|
|
28
|
-
* universal base and behavior is byte-for-byte unchanged — the new axes are strictly opt-in.
|
|
29
|
-
*
|
|
30
|
-
* Evidence honesty (per the name-canonicalization research pass): the PERSON-name side is well
|
|
31
|
-
* sourced; the ORGANIZATION side is a known evidence gap. This is a solid _canonicalization_
|
|
32
|
-
* baseline (the strip-designations principle is Winkler-grounded; the designation list draws on
|
|
33
|
-
* the ISO 20275 Entity Legal Forms register and `cleanco`). The jurisdiction/domain packs below
|
|
34
|
-
* are grounded seeds, not exhaustive — extend them per ISO 20275 as locales are added. The harder
|
|
35
|
-
* org-_matching_ problems — acronym ↔ expansion (`IBM` ↔ `International Business Machines`),
|
|
36
|
-
* DBA/alias resolution beyond the simple clause, subsidiary/parent, and TF-IDF n-gram token
|
|
37
|
-
* matching — are deferred to a follow-up (a dedicated org-matching research pass + the matcher
|
|
38
|
-
* epic).
|
|
6
|
+
* Canonicalizes organization names by removing legal designations. It normalizes DBA clauses plus punctuation and accents.
|
|
7
|
+
* It also folds connectives and leading articles. Jurisdiction adds local legal forms. Domain rules preserve
|
|
8
|
+
* ambiguous terms that could otherwise be stripped.
|
|
39
9
|
*/
|
|
40
10
|
|
|
41
|
-
import { foldForKey } from "@mailwoman/
|
|
11
|
+
import { foldForKey } from "@mailwoman/codex/address/key"
|
|
42
12
|
|
|
43
13
|
/**
|
|
44
14
|
* A canonicalized organization name.
|
|
@@ -63,36 +33,42 @@ export interface OrganizationName {
|
|
|
63
33
|
}
|
|
64
34
|
|
|
65
35
|
/**
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
36
|
+
* Names a domain pack.
|
|
37
|
+
*
|
|
38
|
+
* Each pack leaves domain-specific abbreviations in the organization name
|
|
39
|
+
* instead of stripping them as legal forms.
|
|
40
|
+
* The `general` pack adds no domain-specific abbreviations.
|
|
69
41
|
*/
|
|
70
42
|
export type DesignationDomain = "general" | "healthcare"
|
|
71
43
|
|
|
72
44
|
/**
|
|
73
|
-
* Context for {@link canonicalizeOrganizationName}
|
|
45
|
+
* Context for {@link canonicalizeOrganizationName}; omit both fields for the universal base behavior.
|
|
74
46
|
*/
|
|
75
47
|
export interface CanonicalizeOptions {
|
|
76
48
|
/**
|
|
77
|
-
* ISO 3166-1 alpha-2 country code
|
|
78
|
-
*
|
|
79
|
-
*
|
|
49
|
+
* ISO 3166-1 alpha-2 country code for the organization's jurisdiction.
|
|
50
|
+
*
|
|
51
|
+
* Its country pack adds that jurisdiction's legal forms to the strip set.
|
|
52
|
+
* The comparison ignores case.
|
|
53
|
+
* Unknown codes add no pack.
|
|
80
54
|
*/
|
|
81
55
|
jurisdiction?: string
|
|
82
56
|
/**
|
|
83
|
-
* Ingest domain
|
|
84
|
-
*
|
|
57
|
+
* Ingest domain that keeps domain-specific abbreviations in organization names.
|
|
58
|
+
*
|
|
59
|
+
* The `healthcare` domain leaves `pt` in the name.
|
|
60
|
+
* It also leaves `sca` and `scs`.
|
|
61
|
+
* These rules take precedence over jurisdiction packs.
|
|
85
62
|
*/
|
|
86
63
|
domain?: DesignationDomain
|
|
87
64
|
}
|
|
88
65
|
|
|
89
66
|
/**
|
|
90
|
-
* Universal legal-entity designations
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
* jurisdiction.
|
|
67
|
+
* Universal legal-entity designations, normalized to lowercase without punctuation
|
|
68
|
+
* and stripped as whole tokens.
|
|
69
|
+
*
|
|
70
|
+
* Name-meaningful words and the collision-prone forms that live in
|
|
71
|
+
* {@link JURISDICTION_DESIGNATIONS} are deliberately absent.
|
|
96
72
|
*/
|
|
97
73
|
const BASE_DESIGNATIONS = new Set([
|
|
98
74
|
"inc",
|
|
@@ -144,16 +120,14 @@ const BASE_DESIGNATIONS = new Set([
|
|
|
144
120
|
"doo",
|
|
145
121
|
"ood",
|
|
146
122
|
"ead",
|
|
147
|
-
// Belgian forms
|
|
123
|
+
// Belgian forms, safe in the base because they collide with no domain.
|
|
148
124
|
"bvba",
|
|
149
125
|
"sprl",
|
|
150
126
|
])
|
|
151
127
|
|
|
152
128
|
/**
|
|
153
|
-
* Jurisdiction-conditional legal forms (ISO 3166-1 alpha-2 → forms)
|
|
154
|
-
*
|
|
155
|
-
* Stripping these is correct ONLY when we know the org's country — never in the universal base. Grounded seeds, not
|
|
156
|
-
* exhaustive; extend per ISO 20275.
|
|
129
|
+
* Jurisdiction-conditional legal forms (ISO 3166-1 alpha-2 → forms) added only when the jurisdiction
|
|
130
|
+
* is known, holding the collision-prone tokens (`pt`, `sca`, `scs`) that the base must not strip.
|
|
157
131
|
*/
|
|
158
132
|
const JURISDICTION_DESIGNATIONS: Record<string, readonly string[]> = {
|
|
159
133
|
ID: ["pt", "tbk", "ud"], // Perseroan Terbatas / Terbuka (listed) / Usaha Dagang
|
|
@@ -165,10 +139,8 @@ const JURISDICTION_DESIGNATIONS: Record<string, readonly string[]> = {
|
|
|
165
139
|
}
|
|
166
140
|
|
|
167
141
|
/**
|
|
168
|
-
* Domain
|
|
169
|
-
*
|
|
170
|
-
* collide with jurisdiction-conditional legal forms — `pt` (Physical Therapy), `sca` (Sudden Cardiac Arrest), `scs`
|
|
171
|
-
* (Spinal Cord Stimulator) — plus a couple of always-clinical ones for future-proofing.
|
|
142
|
+
* Domain guard-sets (domain → tokens never stripped) that override any jurisdiction pack,
|
|
143
|
+
* e.g. `healthcare` keeps `pt`, `sca`, and `scs` from being stripped as legal forms.
|
|
172
144
|
*/
|
|
173
145
|
const DOMAIN_PROTECTED: Record<DesignationDomain, readonly string[]> = {
|
|
174
146
|
general: [],
|
|
@@ -176,9 +148,10 @@ const DOMAIN_PROTECTED: Record<DesignationDomain, readonly string[]> = {
|
|
|
176
148
|
}
|
|
177
149
|
|
|
178
150
|
/**
|
|
179
|
-
* Compute the effective
|
|
180
|
-
*
|
|
181
|
-
* the
|
|
151
|
+
* Compute the effective strip-set `(base ∪ jurisdiction-pack) − domain-guard-pack`.
|
|
152
|
+
*
|
|
153
|
+
* @returns the shared base set unchanged when no context is given (the byte-stable default),
|
|
154
|
+
* so the common path allocates no set.
|
|
182
155
|
*/
|
|
183
156
|
function resolveDesignations(options?: CanonicalizeOptions): ReadonlySet<string> {
|
|
184
157
|
const jurisdiction = options?.jurisdiction?.trim().toUpperCase()
|
|
@@ -210,8 +183,10 @@ function resolveDesignations(options?: CanonicalizeOptions): ReadonlySet<string>
|
|
|
210
183
|
const DBA_PATTERN = /\s+(?:d\/b\/a|dba|doing business as|t\/a|trading as|a\/k\/a|aka|fka|f\/k\/a)\s+/i
|
|
211
184
|
|
|
212
185
|
/**
|
|
213
|
-
* Canonicalize one name fragment: lowercase, strip accents, connectives → `and`,
|
|
214
|
-
* `the`, strip legal designations, collapse whitespace.
|
|
186
|
+
* Canonicalize one name fragment: lowercase, strip accents, connectives → `and`,
|
|
187
|
+
* drop punctuation, remove a leading `the`, strip legal designations, collapse whitespace.
|
|
188
|
+
*
|
|
189
|
+
* @returns the key plus the designations it removed.
|
|
215
190
|
*/
|
|
216
191
|
function canonicalizeFragment(
|
|
217
192
|
fragment: string,
|
|
@@ -236,12 +211,11 @@ function canonicalizeFragment(
|
|
|
236
211
|
}
|
|
237
212
|
|
|
238
213
|
/**
|
|
239
|
-
* Canonicalize an organization name: split off any `doing business as` clause,
|
|
240
|
-
*
|
|
214
|
+
* Canonicalize an organization name: split off any `doing business as` clause,
|
|
215
|
+
* then reduce the legal name to a designation-stripped key.
|
|
241
216
|
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
244
|
-
* used and the result is byte-for-byte the legacy behavior.
|
|
217
|
+
* Returns `null` for empty input. {@link CanonicalizeOptions} resolves conflicts
|
|
218
|
+
* between jurisdiction and domain rules.
|
|
245
219
|
*/
|
|
246
220
|
export function canonicalizeOrganizationName(
|
|
247
221
|
input: string | null | undefined,
|