Skip to main content

Annotate a result

Use this procedure when a consumer needs derived facts about a coordinate, such as coordinate formats or countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head. reference data. Keep the address 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. or geocode result separately when the consumer also needs repeated components, coordinate provenance, or resolution evidence. The examples use a fixed public coordinate and address fields supplied by hand, in place of a geocode result.

Prerequisites​

  • Node.js ^24.22.3 || >=26.13.1
  • ECMAScript module project with "type": "module" in its package.json
  • Start by installing the required packages:

    sh
    npm install @mailwoman/annotations @mailwoman/spatial @mailwoman/codex @mailwoman/core

    The coordinate-format and countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head.-reference annotators used below need no geographic database. The repeated-component check later uses @mailwoman/core.

    Compose and check two annotators​

    Save this as annotate.ts. composeAnnotators runs each annotator and merges its partial AnnotationSet. It skips an annotator that throws, and later annotators replace earlier values on a shared key. The explicit check stops this example if either expected field is absent.

    ts
    import { composeAnnotators, toNative, toOpenCage } from "@mailwoman/annotations"
    import { countryReferenceAnnotator } from "@mailwoman/codex/country"
    import { coordinateFormatAnnotator } from "@mailwoman/spatial"

    const annotate = composeAnnotators([coordinateFormatAnnotator, countryReferenceAnnotator])

    const set = await annotate({
    lat: 38.8977,
    lon: -77.0365,
    countryCode: "US",
    date: new Date("2026-11-09T12:00:00Z"),
    })

    if (!set.dms || set.iso3166?.alpha2 !== "US") {
    throw new Error("The coordinate or country annotator returned no expected value")
    }

    const native = toNative(set)
    const openCage = toOpenCage(set)

    console.log(JSON.stringify({ native, openCage }, null, 2))

    Run node annotate.ts. Published 11.0.0 produced the following complete output for this input:

    json
    {
    "native": {
    "dms": {
    "lat": "38° 53′ 51.72″ N",
    "lon": "77° 2′ 11.40″ W"
    },
    "mgrs": "18SUJ2339407395",
    "maidenhead": "FM18lv",
    "geohash": "dqcjqcpex",
    "mercator": {
    "x": -8575663.95249602,
    "y": 4707028.550805139
    },
    "qiblaBearing": 56.561521466160855,
    "sun": {
    "rise": 1794224722,
    "set": 1794261676,
    "noon": 1794243199
    },
    "callingCode": 1,
    "currency": {
    "isoCode": "USD",
    "name": "United States dollar",
    "symbol": "$"
    },
    "flag": "🇺🇸",
    "timezone": null,
    "iso3166": {
    "alpha2": "US",
    "alpha3": null,
    "numeric": null
    },
    "nuts": null,
    "unLocode": null,
    "fips": null,
    "wikidata": null
    },
    "openCage": {
    "DMS": {
    "lat": "38° 53′ 51.72″ N",
    "lng": "77° 2′ 11.40″ W"
    },
    "MGRS": "18SUJ2339407395",
    "Maidenhead": "FM18lv",
    "Mercator": {
    "x": -8575663.95249602,
    "y": 4707028.550805139
    },
    "geohash": "dqcjqcpex",
    "qibla": 56.561521466160855,
    "sun": {
    "rise": {
    "apparent": 1794224722
    },
    "set": {
    "apparent": 1794261676
    }
    },
    "callingcode": 1,
    "currency": {
    "iso_code": "USD",
    "name": "United States dollar",
    "symbol": "$"
    },
    "flag": "🇺🇸"
    }
    }

    toNative(set) returns the full AnnotationSet, including keys whose annotator supplied no value. toOpenCage(set) emits populated fields under its consumer's key names. It maps dms.lon to DMS.lng, qiblaBearing to qibla, and currency.isoCode to currency.iso_code. The OpenCage projection has no slot for iso3166 or sun.noon, so those values from this run are absent. A null native timezone here means the chosen annotators left it unset, and the coordinate's timezone is unknown from this run.

    Build a schema.org Place from address fields​

    toSchemaOrg accepts its own SchemaOrgInput type: a coordinate and the address fields your application has already chosen. Save this as place.ts:

    ts
    import { composeStreetAddress, toSchemaOrg } from "@mailwoman/annotations"

    const place = toSchemaOrg({
    lat: 38.8977,
    lon: -77.0365,
    streetAddress: composeStreetAddress({ houseNumber: "1600", street: "Pennsylvania Avenue NW" }),
    locality: "Washington",
    region: "DC",
    postalCode: "20500",
    countryCode: "US",
    })

    console.log(JSON.stringify(place, null, 2))

    Run node place.ts. Published 11.0.0 returned:

    json
    {
    "@context": "https://schema.org",
    "@type": "Place",
    "geo": {
    "@type": "GeoCoordinates",
    "latitude": 38.8977,
    "longitude": -77.0365
    },
    "address": {
    "@type": "PostalAddress",
    "streetAddress": "1600 Pennsylvania Avenue NW",
    "addressLocality": "Washington",
    "addressRegion": "DC",
    "postalCode": "20500",
    "addressCountry": "US"
    }
    }

    This serializer joins the supplied house numberhouse numberThe numeric or alphanumeric identifier of a building on a street. Mailwoman's house_number component; its position relative to the street name flips between locales. and streetstreetThe named linear feature along which house numbers are ordered. Decomposes into a name plus street affixes; one of the Tier 2 fine labels. into one streetAddress string. Its input holds a coordinate and address fields only. Keep the original parse treeparse treeThe hierarchical address structure produced by decoding BIO labels into spans and nesting them per the schema's containment rules (house_number → street → locality → region → country). or geocode result when the consumer needs the AnnotationSet, repeated address components, or geocode provenance. The result schema describes dropped_components, unfollowed_components, and coordinate_source on a geocode result.

    Check a flat projection before discarding the tree​

    The following fixture constructs a tree with two locality nodes by hand, to show what decodeAsJSON does with a known shape. Save this as repeated.ts:

    ts
    import { decodeAsJSON, type AddressNode, type AddressTree } from "@mailwoman/core/decoder"

    const raw = "07691 Portopetro, Illes Balears, Spain"

    const node = (tag: AddressNode["tag"], value: string, children: AddressNode[] = []): AddressNode => {
    const start = raw.indexOf(value)

    return { tag, value, start, end: start + value.length, confidence: 1, children }
    }

    const tree: AddressTree = {
    raw,
    roots: [
    node("country", "Spain", [
    node("locality", "Portopetro", [node("postcode", "07691")]),
    node("locality", "Illes Balears"),
    ]),
    ],
    }

    const flat = decodeAsJSON(tree, { includeDropped: true })

    console.log(
    JSON.stringify(
    {
    treeLocalities: tree.roots[0].children.map((child) => child.value),
    flatLocality: flat.locality,
    dropped: flat.dropped,
    },
    null,
    2
    )
    )

    Run node repeated.ts. Published 11.0.0 returned:

    json
    {
    "treeLocalities": ["Portopetro", "Illes Balears"],
    "flatLocality": "Portopetro",
    "dropped": [
    {
    "tag": "locality",
    "value": "Illes Balears",
    "kept": "Portopetro"
    }
    ]
    }

    The flat object has one locality value. includeDropped: true reports the omitted 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. and the value that occupied its slot. Keep tree when the exact 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 their relationships matter. A geocode result also holds provenance fields. These annotation serializers leave them out. Geocode a file of addresses shows a real coordinate_source value.

    Add database-backed annotations​

    Published 11.0.0 also declares makeTimezoneAnnotator, makeUNLocodeAnnotator, and makeNUTSAnnotator in their lookup packages. Each one reads its own lookup database, and no output from them is shown here. Check each expected output field after composing an annotator, because composeAnnotators skips an annotator that throws and leaves its fields null.

    The published annotation serializer, coordinate annotator, country annotator, and tree-to-JSON projection define the interfaces exercised above. The library reference covers the parser and geocoder calls that produce an address result.