@mailwoman/match 7.2.0 → 7.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mailwoman/match",
3
- "version": "7.2.0",
3
+ "version": "7.3.0",
4
4
  "description": "The geocode-first record matcher: block → score → cluster. This first cut ships the string comparators (Jaro / Jaro-Winkler + an edit-distance fallback for compound surnames) that the Fellegi-Sunter scorer is built on.",
5
5
  "license": "AGPL-3.0-only OR LicenseRef-Commercial",
6
6
  "repository": {
@@ -12,57 +12,92 @@
12
12
  "out/**/*.js",
13
13
  "out/**/*.js.map",
14
14
  "out/**/*.d.ts",
15
- "out/**/*.d.ts.map"
15
+ "out/**/*.d.ts.map",
16
+ "*.ts",
17
+ "*.tsx",
18
+ "**/*.ts",
19
+ "**/*.tsx",
20
+ "!*.test.ts",
21
+ "!*.test.tsx",
22
+ "!**/*.test.ts",
23
+ "!**/*.test.tsx"
16
24
  ],
17
25
  "type": "module",
18
26
  "exports": {
19
27
  "./package.json": "./package.json",
20
28
  ".": {
21
- "node": "./index.ts",
22
- "default": "./out/index.js",
23
- "types": "./out/index.d.ts"
29
+ "types": "./out/index.d.ts",
30
+ "default": "./out/index.js"
24
31
  },
25
32
  "./blocking": {
26
- "node": "./blocking.ts",
27
- "default": "./out/blocking.js",
28
- "types": "./out/blocking.d.ts"
33
+ "types": "./out/blocking.d.ts",
34
+ "default": "./out/blocking.js"
29
35
  },
30
36
  "./clustering": {
31
- "node": "./clustering.ts",
32
- "default": "./out/clustering.js",
33
- "types": "./out/clustering.d.ts"
37
+ "types": "./out/clustering.d.ts",
38
+ "default": "./out/clustering.js"
34
39
  },
35
40
  "./comparators": {
36
- "node": "./comparators.ts",
37
- "default": "./out/comparators.js",
38
- "types": "./out/comparators.d.ts"
41
+ "types": "./out/comparators.d.ts",
42
+ "default": "./out/comparators.js"
39
43
  },
40
44
  "./distance": {
41
- "node": "./distance.ts",
42
- "default": "./out/distance.js",
43
- "types": "./out/distance.d.ts"
45
+ "types": "./out/distance.d.ts",
46
+ "default": "./out/distance.js"
44
47
  },
45
48
  "./fellegi-sunter": {
46
- "node": "./fellegi-sunter.ts",
47
- "default": "./out/fellegi-sunter.js",
48
- "types": "./out/fellegi-sunter.d.ts"
49
+ "types": "./out/fellegi-sunter.d.ts",
50
+ "default": "./out/fellegi-sunter.js"
49
51
  },
50
52
  "./em": {
51
- "node": "./em.ts",
52
- "default": "./out/em.js",
53
- "types": "./out/em.d.ts"
53
+ "types": "./out/em.d.ts",
54
+ "default": "./out/em.js"
54
55
  },
55
56
  "./tf": {
56
- "node": "./tf.ts",
57
- "default": "./out/tf.js",
58
- "types": "./out/tf.d.ts"
57
+ "types": "./out/tf.d.ts",
58
+ "default": "./out/tf.js"
59
59
  }
60
60
  },
61
61
  "publishConfig": {
62
- "access": "public"
62
+ "access": "public",
63
+ "exports": {
64
+ "./package.json": "./package.json",
65
+ ".": {
66
+ "types": "./out/index.d.ts",
67
+ "default": "./out/index.js"
68
+ },
69
+ "./blocking": {
70
+ "types": "./out/blocking.d.ts",
71
+ "default": "./out/blocking.js"
72
+ },
73
+ "./clustering": {
74
+ "types": "./out/clustering.d.ts",
75
+ "default": "./out/clustering.js"
76
+ },
77
+ "./comparators": {
78
+ "types": "./out/comparators.d.ts",
79
+ "default": "./out/comparators.js"
80
+ },
81
+ "./distance": {
82
+ "types": "./out/distance.d.ts",
83
+ "default": "./out/distance.js"
84
+ },
85
+ "./fellegi-sunter": {
86
+ "types": "./out/fellegi-sunter.d.ts",
87
+ "default": "./out/fellegi-sunter.js"
88
+ },
89
+ "./em": {
90
+ "types": "./out/em.d.ts",
91
+ "default": "./out/em.js"
92
+ },
93
+ "./tf": {
94
+ "types": "./out/tf.d.ts",
95
+ "default": "./out/tf.js"
96
+ }
97
+ }
63
98
  },
64
99
  "dependencies": {
65
- "@mailwoman/spatial": "7.2.0",
100
+ "@mailwoman/spatial": "7.3.0",
66
101
  "fastest-levenshtein": "^1.0.16"
67
102
  },
68
103
  "devDependencies": {
package/tf.ts ADDED
@@ -0,0 +1,96 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Term-frequency adjustment — making a rare-value agreement count more than a common one.
7
+ *
8
+ * Two people both named "Vijayan" is far stronger evidence of a match than two both named "Smith",
9
+ * because "Smith" agreements happen by chance all the time and "Vijayan" agreements don't. The
10
+ * Fellegi-Sunter `m` (how often a true match agrees) is roughly the same either way; what differs
11
+ * is `u` — the chance a _non_-match agrees — which for an exact agreement on value `v` is just
12
+ * how common `v` is. So we leave `m`, and replace the level's average `u` with `frequency(v)`,
13
+ * adding `log2(u_level / frequency(v))` to the weight: a big positive bump for rare values, a
14
+ * penalty for common ones.
15
+ *
16
+ * Crucially for a label-free matcher: the frequencies are computed ON-THE-FLY from the input column
17
+ * (the Splink approach) — no external Census table required. Build a {@link TermFrequencyTable}
18
+ * from the values you're matching, then attach it to a comparison with {@link withTermFrequency}.
19
+ */
20
+
21
+ import type { Comparison, TermFrequencyAdjustment } from "./fellegi-sunter.ts"
22
+
23
+ /** A lookup of how common each value is, in (0, 1], built from a column of observed values. */
24
+ export interface TermFrequencyTable {
25
+ /** Relative frequency of a value (its normalized form), or 0 if never seen. */
26
+ frequency(value: string): number
27
+ /** Total observations the table was built from. */
28
+ readonly total: number
29
+ /** Number of distinct normalized values. */
30
+ readonly distinct: number
31
+ }
32
+
33
+ const defaultNormalize = (value: string): string => value.trim().toLowerCase().replace(/\s+/g, " ")
34
+
35
+ /**
36
+ * Build a {@link TermFrequencyTable} from an iterable of values (e.g. every `given` name in the dataset). Values are
37
+ * normalized (default: trim + lowercase + collapse whitespace) before counting, and `frequency()` normalizes its
38
+ * argument the same way, so callers pass raw field values.
39
+ */
40
+ export function buildTermFrequencyTable(
41
+ values: Iterable<string | null | undefined>,
42
+ opts: { normalize?: (value: string) => string } = {}
43
+ ): TermFrequencyTable {
44
+ const normalize = opts.normalize ?? defaultNormalize
45
+ const counts = new Map<string, number>()
46
+ let total = 0
47
+
48
+ for (const value of values) {
49
+ if (value == null) continue
50
+ const key = normalize(value)
51
+
52
+ if (!key) continue
53
+ counts.set(key, (counts.get(key) ?? 0) + 1)
54
+ total++
55
+ }
56
+
57
+ return {
58
+ total,
59
+ distinct: counts.size,
60
+ frequency(value) {
61
+ if (total === 0) return 0
62
+
63
+ return (counts.get(normalize(value)) ?? 0) / total
64
+ },
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Attach a term-frequency adjustment to a comparison. By default it applies to the exact level (index 0) and looks up
70
+ * the value via `value(a, b)` — usually the agreeing field extracted from one side. Returns a new comparison; the
71
+ * underlying `assess` and levels are untouched, so this composes with EM (which re-estimates the base `m`/`u` the
72
+ * adjustment sits on top of).
73
+ */
74
+ export function withTermFrequency<R>(
75
+ comparison: Comparison<R>,
76
+ config: {
77
+ table: TermFrequencyTable
78
+ value: (a: R, b: R) => string | null | undefined
79
+ /** Level indices to adjust. Default `[0]` (the exact level). */
80
+ levels?: Iterable<number>
81
+ /** Scale in [0, 1]. Default 1. */
82
+ weight?: number
83
+ /** Frequency floor bounding the boost on ultra-rare values. Default 1e-4. */
84
+ minimumFrequency?: number
85
+ }
86
+ ): Comparison<R> {
87
+ const adjustment: TermFrequencyAdjustment<R> = {
88
+ frequency: (value) => config.table.frequency(value),
89
+ levels: new Set(config.levels ?? [0]),
90
+ value: config.value,
91
+ weight: config.weight,
92
+ minimumFrequency: config.minimumFrequency,
93
+ }
94
+
95
+ return { ...comparison, termFrequency: adjustment }
96
+ }