@mailwoman/core 7.2.0 → 7.2.1
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/api/APIClient.ts +175 -0
- package/api/headless.ts +23 -0
- package/api/index.ts +11 -0
- package/api/responses.ts +135 -0
- package/coarse-placer/coarse-placer.ts +300 -0
- package/coarse-placer/featurize.ts +171 -0
- package/coarse-placer/tools/build-dataset.ts +223 -0
- package/coarse-placer/tools/build-outlier-exposure.ts +172 -0
- package/coarse-placer/tools/build-outlier-latin.ts +200 -0
- package/coarse-placer/tools/build-outlier-oa.ts +231 -0
- package/coarse-placer/tools/eval-latin-offmap.ts +140 -0
- package/coarse-placer/tools/eval-openset.ts +519 -0
- package/coarse-placer/tools/eval-quant-compare.ts +151 -0
- package/coarse-placer/tools/eval.ts +187 -0
- package/coarse-placer/tools/fnv-hash.ts +24 -0
- package/coarse-placer/tools/index.ts +23 -0
- package/coarse-placer/tools/probe-frontier.ts +216 -0
- package/coarse-placer/tools/quantize.ts +116 -0
- package/coarse-placer/tools/train.ts +244 -0
- package/collections.ts +134 -0
- package/db/schema.ts +21 -0
- package/decoder/build-tree.ts +228 -0
- package/decoder/calibration.ts +101 -0
- package/decoder/containment.ts +87 -0
- package/decoder/index.ts +17 -0
- package/decoder/proposals-to-tree.ts +83 -0
- package/decoder/resolve-proposal-overlaps.ts +79 -0
- package/decoder/serialize-json.ts +70 -0
- package/decoder/serialize-tuples.ts +61 -0
- package/decoder/serialize-xml.ts +209 -0
- package/decoder/types.ts +159 -0
- package/decoder/unknown-spans.ts +103 -0
- package/decoder/validate-tree.ts +100 -0
- package/env/index.ts +42 -0
- package/env/load.ts +14 -0
- package/env/paths.ts +40 -0
- package/env/schema.ts +125 -0
- package/errors/index.ts +9 -0
- package/errors/schema.ts +155 -0
- package/fs/node.ts +38 -0
- package/identifiers.ts +58 -0
- package/index.ts +13 -0
- package/kysley/adapter.ts +29 -0
- package/kysley/client.ts +30 -0
- package/kysley/dialect-config.ts +30 -0
- package/kysley/dialect.ts +67 -0
- package/kysley/driver.ts +175 -0
- package/layers/index.ts +12 -0
- package/layers/manifest.ts +154 -0
- package/layers/schema.ts +105 -0
- package/lifecycle/ServiceSymbol.ts +95 -0
- package/lifecycle/index.ts +11 -0
- package/lifecycle/lru-cache.ts +44 -0
- package/lifecycle/services.ts +322 -0
- package/locale/index.ts +9 -0
- package/locale/locale.ts +49 -0
- package/locale/profiles/en-us.ts +69 -0
- package/locale/profiles/fr-fr.ts +75 -0
- package/locale/profiles/index.ts +9 -0
- package/locale/profiles/ja-jp.ts +33 -0
- package/locale/registry.ts +68 -0
- package/logging/index.ts +8 -0
- package/logging/shared.ts +145 -0
- package/logging/utils.ts +46 -0
- package/objects.ts +211 -0
- package/package.json +195 -104
- package/pipeline/index.ts +46 -0
- package/pipeline/reconcile-lookups.ts +108 -0
- package/pipeline/reconcile.ts +542 -0
- package/pipeline/runtime-pipeline.ts +732 -0
- package/pipeline/span-logit-aggregation.ts +151 -0
- package/pipeline/span-proposer.ts +587 -0
- package/pipeline/types.ts +348 -0
- package/policy/defaults.ts +35 -0
- package/policy/index.ts +10 -0
- package/policy/policy.ts +60 -0
- package/policy/proposal-collection.ts +64 -0
- package/policy/registry.ts +149 -0
- package/resolver/index.ts +29 -0
- package/resolver/types.ts +584 -0
- package/resources/LocaleIndex.ts +124 -0
- package/resources/ResourceMapCache.ts +126 -0
- package/resources/collections.ts +93 -0
- package/resources/debugging.ts +19 -0
- package/resources/git.ts +54 -0
- package/resources/index.ts +17 -0
- package/resources/languages/index.ts +49 -0
- package/resources/languages/types.gen.ts +1368 -0
- package/resources/libaddressinput.ts +105 -0
- package/resources/libpostal.ts +182 -0
- package/resources/locale.ts +23 -0
- package/resources/set.ts +179 -0
- package/resources/whosonfirst/DataSourceCache.ts +72 -0
- package/resources/whosonfirst/PlacetypeDataSource.ts +199 -0
- package/resources/whosonfirst/index.ts +10 -0
- package/resources/whosonfirst/loader.ts +157 -0
- package/resources/whosonfirst/placetypes/Placetype.ts +438 -0
- package/resources/whosonfirst/placetypes/admin.ts +173 -0
- package/resources/whosonfirst/placetypes/definition.ts +135 -0
- package/resources/whosonfirst/placetypes/graph.ts +0 -0
- package/resources/whosonfirst/placetypes/index.ts +12 -0
- package/resources/whosonfirst/placetypes/mermaid.ts +168 -0
- package/resources/whosonfirst/placetypes/tree.ts +46 -0
- package/routing/index.ts +208 -0
- package/scripting/index.ts +40 -0
- package/scripting/utils/index.ts +106 -0
- package/tokenization/Graph.ts +50 -0
- package/tokenization/Span.ts +297 -0
- package/tokenization/index.ts +9 -0
- package/tokenization/normalizer.ts +123 -0
- package/tools/download-libpostal-resources.ts +98 -0
- package/tools/download-ssl-address.ts +99 -0
- package/tools/generate-language-types.ts +194 -0
- package/tools/index.ts +13 -0
- package/types/Classification.ts +211 -0
- package/types/classifier.ts +133 -0
- package/types/component.ts +75 -0
- package/types/index.ts +10 -0
- package/types/mapping.ts +81 -0
- package/utils/data-root.ts +56 -0
- package/utils/hash.ts +27 -0
- package/utils/index.ts +14 -0
- package/utils/jsonl.ts +43 -0
- package/utils/python-json.ts +105 -0
- package/utils/python-random.ts +90 -0
- package/utils/repo.ts +118 -0
- package/utils/sealed-db.ts +85 -0
- package/utils/stats.ts +32 -0
- package/vitest.config.ts +53 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Per-span logit aggregation — Option C from the DeepSeek synthesis review.
|
|
7
|
+
*
|
|
8
|
+
* Takes the per-token logits already emitted by the ONNX model (currently discarded after argmax in
|
|
9
|
+
* `classifier.ts`) and aggregates them over phrase-grouper spans to produce per-span top-K tag
|
|
10
|
+
* candidates. These feed directly into `reconcileSpans` as the `classifierTopK` input.
|
|
11
|
+
*
|
|
12
|
+
* Why this instead of a sequence-level beam decoder: the reconciler takes `(span, tag, score)`
|
|
13
|
+
* triples — per-span confidence, not BIO-sequence-level confidence. The phrase grouper has
|
|
14
|
+
* already done boundary discovery. This module answers "given these boundaries, what tags does
|
|
15
|
+
* the classifier think each span is?" — which is the right abstraction for joint decoding.
|
|
16
|
+
*
|
|
17
|
+
* Code path matches the eventual production runtime: when a top-k-trained classifier exists, the TS
|
|
18
|
+
* runtime just swaps "per-token softmax aggregation" for "classifier's native top-k API." Same
|
|
19
|
+
* downstream contract.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { ComponentTag } from "../types/component.ts"
|
|
23
|
+
import type { ClassifierCandidate } from "./reconcile.ts"
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A token piece with character-level offsets into the original text.
|
|
27
|
+
*/
|
|
28
|
+
export interface TokenPiece {
|
|
29
|
+
start: number
|
|
30
|
+
end: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A span proposal from the phrase grouper, in character offsets.
|
|
35
|
+
*/
|
|
36
|
+
export interface SpanBounds {
|
|
37
|
+
start: number
|
|
38
|
+
end: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Given per-token logits and phrase-grouper spans, produce per-span top-K tag candidates.
|
|
43
|
+
*
|
|
44
|
+
* For each span, finds the tokens whose character ranges overlap the span, sums their softmax probabilities per-tag,
|
|
45
|
+
* normalizes, and emits the top K tags with their aggregated scores.
|
|
46
|
+
*
|
|
47
|
+
* BIO prefix stripping: the model emits BIO labels (`B-locality`, `I-locality`, etc.) but the reconciler works with
|
|
48
|
+
* component tags (`locality`). This function strips the `B-`/`I-` prefix and merges probabilities: `score(locality) =
|
|
49
|
+
* sum(score(B-locality) + score(I-locality))` across the span's tokens.
|
|
50
|
+
*
|
|
51
|
+
* @param logits Per-token logits from ONNX inference, shape `[seqLen][numLabels]`.
|
|
52
|
+
* @param pieces Token pieces with character-level offsets (from the tokenizer's `encode`).
|
|
53
|
+
* @param spans Phrase-grouper span proposals in character offsets.
|
|
54
|
+
* @param opts Options bag.
|
|
55
|
+
* @param opts.topK Number of top-scoring tags to return per span (default 3).
|
|
56
|
+
* @param opts.labels The BIO label vocabulary the model emits, e.g. `["O", "B-locality", ...]`.
|
|
57
|
+
*/
|
|
58
|
+
export function aggregateSpanLogits(
|
|
59
|
+
logits: number[][],
|
|
60
|
+
pieces: readonly TokenPiece[],
|
|
61
|
+
spans: readonly SpanBounds[],
|
|
62
|
+
opts: { topK?: number; labels: readonly string[]; text?: string }
|
|
63
|
+
): ClassifierCandidate[] {
|
|
64
|
+
const topK = opts.topK ?? 3
|
|
65
|
+
const labels = opts.labels
|
|
66
|
+
const text = opts.text
|
|
67
|
+
|
|
68
|
+
const candidates: ClassifierCandidate[] = []
|
|
69
|
+
|
|
70
|
+
for (const span of spans) {
|
|
71
|
+
// Inherently-numeric components can't live on a span with no digit. An OOD model facing the
|
|
72
|
+
// "<postcode> <City>" order routinely mistags the trailing city as a postcode (Toulouse →
|
|
73
|
+
// postcode:0.77); since postcodes and house numbers contain a digit in every locale we handle,
|
|
74
|
+
// drop those candidates for a digit-less span so the reconciler picks the real component (#425).
|
|
75
|
+
const spanHasNoDigit = text !== undefined && !/\d/.test(text.slice(span.start, span.end))
|
|
76
|
+
// Find tokens overlapping this span (character-level).
|
|
77
|
+
const overlapping: number[] = []
|
|
78
|
+
|
|
79
|
+
for (let t = 0; t < pieces.length; t++) {
|
|
80
|
+
const p = pieces[t]!
|
|
81
|
+
|
|
82
|
+
if (p.end <= span.start) continue
|
|
83
|
+
|
|
84
|
+
if (p.start >= span.end) break
|
|
85
|
+
overlapping.push(t)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (overlapping.length === 0) continue
|
|
89
|
+
|
|
90
|
+
// Aggregate softmax probabilities per component tag (strip BIO prefix).
|
|
91
|
+
const tagScores = new Map<string, number>()
|
|
92
|
+
|
|
93
|
+
for (const t of overlapping) {
|
|
94
|
+
const probs = softmax(logits[t]!)
|
|
95
|
+
|
|
96
|
+
for (let l = 0; l < labels.length; l++) {
|
|
97
|
+
const bioLabel = labels[l]!
|
|
98
|
+
const tag = stripBIOPrefix(bioLabel)
|
|
99
|
+
|
|
100
|
+
if (tag === "O") continue
|
|
101
|
+
|
|
102
|
+
if (spanHasNoDigit && (tag === "postcode" || tag === "house_number")) continue
|
|
103
|
+
const prev = tagScores.get(tag) ?? 0
|
|
104
|
+
tagScores.set(tag, prev + probs[l]!)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Normalize by number of overlapping tokens so longer spans don't auto-win.
|
|
109
|
+
const norm = overlapping.length
|
|
110
|
+
const sorted = [...tagScores.entries()]
|
|
111
|
+
.map(([tag, score]) => ({ tag, score: score / norm }))
|
|
112
|
+
.sort((a, b) => b.score - a.score)
|
|
113
|
+
.slice(0, topK)
|
|
114
|
+
|
|
115
|
+
for (const { tag, score } of sorted) {
|
|
116
|
+
candidates.push({
|
|
117
|
+
span: { start: span.start, end: span.end },
|
|
118
|
+
tag: tag as ComponentTag,
|
|
119
|
+
score,
|
|
120
|
+
})
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return candidates
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Strip `B-` or `I-` prefix from a BIO label, returning the component tag. `O` returns `"O"`.
|
|
129
|
+
*/
|
|
130
|
+
function stripBIOPrefix(label: string): string {
|
|
131
|
+
if (label === "O") return "O"
|
|
132
|
+
const dash = label.indexOf("-")
|
|
133
|
+
|
|
134
|
+
if (dash === -1) return label
|
|
135
|
+
|
|
136
|
+
return label.slice(dash + 1)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Numerically stable softmax over a row of logits. */
|
|
140
|
+
function softmax(row: readonly number[]): number[] {
|
|
141
|
+
let max = row[0]!
|
|
142
|
+
|
|
143
|
+
for (let i = 1; i < row.length; i++)
|
|
144
|
+
if (row[i]! > max) {
|
|
145
|
+
max = row[i]!
|
|
146
|
+
}
|
|
147
|
+
const exps = row.map((v) => Math.exp(v - max))
|
|
148
|
+
const sum = exps.reduce((a, b) => a + b, 0)
|
|
149
|
+
|
|
150
|
+
return exps.map((e) => e / sum)
|
|
151
|
+
}
|