Normalize an address before direct parsing
Use this procedure when your application calls NeuralAddressClassifier.parse directly and must preserve the caller's original text. The classifier parsesaddress parsingThe process of decomposing a free-text postal address string into structured components — house number, street name, locality, region, postcode, and country — so a geocoder can resolve them to coordinates. the exact string you pass to it. Run the deterministic normalizer first when your input has extra spaces or punctuation, and keep its raw and offsetMap beside the parseaddress parsingThe process of decomposing a free-text postal address string into structured components — house number, street name, locality, region, postcode, and country — so a geocoder can resolve them to coordinates. result. The first parse tutorial shows the full runtime pipelinestaged pipelineMailwoman's runtime architecture: a sequence of pure-function stages (normalize → query-shape → locale-hint → kind-classifier → phrase-grouper → classifier → decoder) connected by typed handoffs. Each stage is published as its own npm package.. That pipelinestaged pipelineMailwoman's runtime architecture: a sequence of pure-function stages (normalize → query-shape → locale-hint → kind-classifier → phrase-grouper → classifier → decoder) connected by typed handoffs. Each stage is published as its own npm package. runs the normalizer itself.
Prerequisites
^24.22.3 || >=26.13.1"type": "module" in its package.jsonInstall the packages used in this example:
- npm
- Yarn
- pnpm
- Bun
npm install @mailwoman/normalize @mailwoman/neural @mailwoman/neural-weights-en-us
yarn add @mailwoman/normalize @mailwoman/neural @mailwoman/neural-weights-en-us
pnpm add @mailwoman/normalize @mailwoman/neural @mailwoman/neural-weights-en-us
bun add @mailwoman/normalize @mailwoman/neural @mailwoman/neural-weights-en-us
The example uses the English modelneural classifierThe machine learning model at the core of Mailwoman's parser — a transformer encoder (~30M parameters) trained from scratch to do BIO token classification over addresses. It learns the 'grammar' of address formats; the gazetteer supplies the 'atlas.' and reads no geographic database. Save the next code block as normalize-address.ts.
Normalize, keep the source map, and parse
import { normalize } from "@mailwoman/normalize"
import { NeuralAddressClassifier } from "@mailwoman/neural"
const raw = " 350 5th Ave,New York, NY 10118 "
const result = normalize(raw)
const wanted = "5th Ave"
const start = result.normalized.indexOf(wanted)
if (start === -1) throw new Error("The normalized text is missing the expected street substring")
const end = start + wanted.length
const rawStart = result.offsetMap[start]
const rawEnd = result.offsetMap[end - 1] + 1
if (result.offsetMap.length !== result.normalized.length) throw new Error("Offset map length differs")
console.log("raw:", JSON.stringify(result.raw))
console.log("normalized:", JSON.stringify(result.normalized))
console.log("transforms:", JSON.stringify(result.transforms))
console.log("normalized street:", JSON.stringify(result.normalized.slice(start, end)), `[${start},${end})`)
console.log("raw street:", JSON.stringify(result.raw.slice(rawStart, rawEnd)), `[${rawStart},${rawEnd})`)
const classifier = await NeuralAddressClassifier.loadFromWeights({ locale: "en-US" })
const tree = await classifier.parse(result.normalized)
console.log("parser text:", JSON.stringify(tree.raw))
Run node normalize-address.ts. Published 11.0.0 returned this complete output:
raw: " 350 5th Ave,New York, NY 10118 "
normalized: "350 5th Ave, New York, NY 10118"
transforms: [{"kind":"nfc","changed":false},{"kind":"space_after_comma","inserted":1},{"kind":"collapse_whitespace","runs":3}]
normalized street: "5th Ave" [4,11)
raw street: "5th Ave" [8,15)
parser text: "350 5th Ave, New York, NY 10118"
normalize inserted one space after the first comma and collapsed three whitespace runs. Its transforms array also records the NFC pass with changed: false. In the returned normalized field, 5th Ave occupies [4,11), and result.normalized.slice(4, 11) is "5th Ave". The returned raw field preserves the exact input. In it, 5th Ave occupies [8,15), and result.raw.slice(8, 15) is also "5th Ave". The offsetMap maps each normalized character position to a source position. In this run, offsetMap[4] is 8 and offsetMap[10] is 14. The normalized and raw positions differ because the input has leading and repeated spaces.
Keep both strings and the map when you store the parseaddress parsingThe process of decomposing a free-text postal address string into structured components — house number, street name, locality, region, postcode, and country — so a geocoder can resolve them to coordinates.. A parser node's spanspanA contiguous range of characters or tokens in the input string, tagged with an address component type (street, locality, postcode, etc.). Parsed addresses are represented as collections of spans, possibly nested in a tree. belongs to the text in tree.raw, so check that field before relating the node to a normalization-stagestageOne of the dataflow stages in the runtime pipeline (normalize, locale check, kind classify, phrase group, token classify, sequence correct, reconcile, resolve). Distinct from tier (model vocabulary) and phase (plan milestone). position. The endpoint mapping above holds for the unchanged 5th Ave substring. An abbreviation expansion can map several output characters to one source character, and the same endpoint calculation then recovers only part of the original substring. When your application can't reconstruct the exact original spanspanA contiguous range of characters or tokens in the input string, tagged with an address component type (street, locality, postcode, etc.). Parsed addresses are represented as collections of spans, possibly nested in a tree., record its source spanspanA contiguous range of characters or tokens in the input string, tagged with an address component type (street, locality, postcode, etc.). Parsed addresses are represented as collections of spans, possibly nested in a tree. as unknown and keep the original input for review.
Normalization prepares text for parsingaddress parsingThe process of decomposing a free-text postal address string into structured components — house number, street name, locality, region, postcode, and country — so a geocoder can resolve them to coordinates.. Use the result schema for parseaddress parsingThe process of decomposing a free-text postal address string into structured components — house number, street name, locality, region, postcode, and country — so a geocoder can resolve them to coordinates. spansspanA contiguous range of characters or tokens in the input string, tagged with an address component type (street, locality, postcode, etc.). Parsed addresses are represented as collections of spans, possibly nested in a tree. and the confidence and limits explanation for what a node score can support.