@mailwoman/resolver-wof-sqlite 12.0.0 → 12.2.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.
Files changed (173) hide show
  1. package/lib/address/keys/build.ts +35 -0
  2. package/lib/address/keys/lookup.ts +318 -6
  3. package/lib/address/keys/open.ts +1 -0
  4. package/lib/address/keys/point-lookup.ts +61 -2
  5. package/lib/address/keys/schema.ts +37 -0
  6. package/lib/address/number-forms.ts +32 -13
  7. package/lib/address/point/interpolation.ts +23 -6
  8. package/lib/address/point/schema.ts +198 -2
  9. package/lib/address/point/scope-search.ts +179 -0
  10. package/lib/address/point.ts +365 -32
  11. package/lib/build/candidate.ts +36 -9
  12. package/lib/candidate/ancestors/sidecar.ts +73 -4
  13. package/lib/candidate/del1.ts +218 -0
  14. package/lib/candidate/lookup.ts +159 -100
  15. package/lib/candidate/schema.ts +14 -2
  16. package/lib/fst/binary-matcher.ts +9 -1
  17. package/lib/fst/builder.ts +7 -0
  18. package/lib/fst/del1-sidecar.ts +782 -0
  19. package/lib/fst/deserialize-web.ts +14 -2
  20. package/lib/fst/matcher.ts +7 -1
  21. package/lib/fst/types.ts +8 -0
  22. package/lib/fst.ts +1 -0
  23. package/lib/index.ts +22 -2
  24. package/lib/labels/street/canonical.ts +333 -0
  25. package/lib/labels/street/centroid/schema.ts +23 -1
  26. package/lib/labels/street/interpolation.ts +32 -6
  27. package/lib/labels/street/morphology/fst/builder.ts +13 -0
  28. package/lib/labels/street/morphology/fst/loader.ts +32 -2
  29. package/lib/labels/street/normalize.ts +153 -4
  30. package/lib/labels/street.ts +1 -0
  31. package/lib/landmark.ts +43 -2
  32. package/lib/lookup/types.ts +0 -6
  33. package/lib/poi/lookup.ts +148 -52
  34. package/lib/poi/schema.ts +9 -1
  35. package/lib/schema/overture-match.ts +2 -1
  36. package/lib/weights/overlay-linker.ts +6 -2
  37. package/out/address/keys/build.d.ts.map +1 -1
  38. package/out/address/keys/build.js +28 -1
  39. package/out/address/keys/build.js.map +1 -1
  40. package/out/address/keys/lookup.d.ts +42 -0
  41. package/out/address/keys/lookup.d.ts.map +1 -1
  42. package/out/address/keys/lookup.js +182 -6
  43. package/out/address/keys/lookup.js.map +1 -1
  44. package/out/address/keys/open.d.ts.map +1 -1
  45. package/out/address/keys/open.js +1 -0
  46. package/out/address/keys/open.js.map +1 -1
  47. package/out/address/keys/point-lookup.d.ts +6 -1
  48. package/out/address/keys/point-lookup.d.ts.map +1 -1
  49. package/out/address/keys/point-lookup.js +43 -1
  50. package/out/address/keys/point-lookup.js.map +1 -1
  51. package/out/address/keys/schema.d.ts +21 -0
  52. package/out/address/keys/schema.d.ts.map +1 -1
  53. package/out/address/keys/schema.js +18 -0
  54. package/out/address/keys/schema.js.map +1 -1
  55. package/out/address/number-forms.d.ts +10 -1
  56. package/out/address/number-forms.d.ts.map +1 -1
  57. package/out/address/number-forms.js +20 -11
  58. package/out/address/number-forms.js.map +1 -1
  59. package/out/address/point/interpolation.d.ts.map +1 -1
  60. package/out/address/point/interpolation.js +15 -5
  61. package/out/address/point/interpolation.js.map +1 -1
  62. package/out/address/point/schema.d.ts +45 -2
  63. package/out/address/point/schema.d.ts.map +1 -1
  64. package/out/address/point/schema.js +129 -0
  65. package/out/address/point/schema.js.map +1 -1
  66. package/out/address/point/scope-search.d.ts +77 -0
  67. package/out/address/point/scope-search.d.ts.map +1 -0
  68. package/out/address/point/scope-search.js +107 -0
  69. package/out/address/point/scope-search.js.map +1 -0
  70. package/out/address/point.d.ts +24 -1
  71. package/out/address/point.d.ts.map +1 -1
  72. package/out/address/point.js +231 -30
  73. package/out/address/point.js.map +1 -1
  74. package/out/build/candidate.d.ts +8 -0
  75. package/out/build/candidate.d.ts.map +1 -1
  76. package/out/build/candidate.js +17 -7
  77. package/out/build/candidate.js.map +1 -1
  78. package/out/candidate/ancestors/sidecar.d.ts +7 -3
  79. package/out/candidate/ancestors/sidecar.d.ts.map +1 -1
  80. package/out/candidate/ancestors/sidecar.js +56 -4
  81. package/out/candidate/ancestors/sidecar.js.map +1 -1
  82. package/out/candidate/del1.d.ts +61 -0
  83. package/out/candidate/del1.d.ts.map +1 -0
  84. package/out/candidate/del1.js +158 -0
  85. package/out/candidate/del1.js.map +1 -0
  86. package/out/candidate/lookup.d.ts +2 -14
  87. package/out/candidate/lookup.d.ts.map +1 -1
  88. package/out/candidate/lookup.js +122 -84
  89. package/out/candidate/lookup.js.map +1 -1
  90. package/out/candidate/schema.d.ts +8 -2
  91. package/out/candidate/schema.d.ts.map +1 -1
  92. package/out/candidate/schema.js +8 -2
  93. package/out/candidate/schema.js.map +1 -1
  94. package/out/fst/binary-matcher.d.ts +7 -1
  95. package/out/fst/binary-matcher.d.ts.map +1 -1
  96. package/out/fst/binary-matcher.js +7 -1
  97. package/out/fst/binary-matcher.js.map +1 -1
  98. package/out/fst/builder.d.ts +2 -0
  99. package/out/fst/builder.d.ts.map +1 -1
  100. package/out/fst/builder.js +5 -0
  101. package/out/fst/builder.js.map +1 -1
  102. package/out/fst/del1-sidecar.d.ts +158 -0
  103. package/out/fst/del1-sidecar.d.ts.map +1 -0
  104. package/out/fst/del1-sidecar.js +581 -0
  105. package/out/fst/del1-sidecar.js.map +1 -0
  106. package/out/fst/deserialize-web.d.ts +3 -1
  107. package/out/fst/deserialize-web.d.ts.map +1 -1
  108. package/out/fst/deserialize-web.js +9 -2
  109. package/out/fst/deserialize-web.js.map +1 -1
  110. package/out/fst/matcher.d.ts +6 -1
  111. package/out/fst/matcher.d.ts.map +1 -1
  112. package/out/fst/matcher.js +6 -1
  113. package/out/fst/matcher.js.map +1 -1
  114. package/out/fst/types.d.ts +7 -0
  115. package/out/fst/types.d.ts.map +1 -1
  116. package/out/fst.d.ts +1 -0
  117. package/out/fst.d.ts.map +1 -1
  118. package/out/fst.js +1 -0
  119. package/out/fst.js.map +1 -1
  120. package/out/index.d.ts +4 -3
  121. package/out/index.d.ts.map +1 -1
  122. package/out/index.js +3 -2
  123. package/out/index.js.map +1 -1
  124. package/out/labels/street/canonical.d.ts +68 -0
  125. package/out/labels/street/canonical.d.ts.map +1 -0
  126. package/out/labels/street/canonical.js +234 -0
  127. package/out/labels/street/canonical.js.map +1 -0
  128. package/out/labels/street/centroid/schema.d.ts +10 -1
  129. package/out/labels/street/centroid/schema.d.ts.map +1 -1
  130. package/out/labels/street/centroid/schema.js +12 -0
  131. package/out/labels/street/centroid/schema.js.map +1 -1
  132. package/out/labels/street/interpolation.d.ts +9 -1
  133. package/out/labels/street/interpolation.d.ts.map +1 -1
  134. package/out/labels/street/interpolation.js +8 -5
  135. package/out/labels/street/interpolation.js.map +1 -1
  136. package/out/labels/street/morphology/fst/builder.d.ts +8 -0
  137. package/out/labels/street/morphology/fst/builder.d.ts.map +1 -1
  138. package/out/labels/street/morphology/fst/builder.js +5 -0
  139. package/out/labels/street/morphology/fst/builder.js.map +1 -1
  140. package/out/labels/street/morphology/fst/loader.d.ts +12 -0
  141. package/out/labels/street/morphology/fst/loader.d.ts.map +1 -1
  142. package/out/labels/street/morphology/fst/loader.js +18 -2
  143. package/out/labels/street/morphology/fst/loader.js.map +1 -1
  144. package/out/labels/street/normalize.d.ts +8 -0
  145. package/out/labels/street/normalize.d.ts.map +1 -1
  146. package/out/labels/street/normalize.js +138 -4
  147. package/out/labels/street/normalize.js.map +1 -1
  148. package/out/labels/street.d.ts +1 -0
  149. package/out/labels/street.d.ts.map +1 -1
  150. package/out/labels/street.js +1 -0
  151. package/out/labels/street.js.map +1 -1
  152. package/out/landmark.d.ts +8 -0
  153. package/out/landmark.d.ts.map +1 -1
  154. package/out/landmark.js +25 -2
  155. package/out/landmark.js.map +1 -1
  156. package/out/lookup/types.d.ts +0 -5
  157. package/out/lookup/types.d.ts.map +1 -1
  158. package/out/poi/lookup.d.ts +9 -1
  159. package/out/poi/lookup.d.ts.map +1 -1
  160. package/out/poi/lookup.js +111 -53
  161. package/out/poi/lookup.js.map +1 -1
  162. package/out/poi/schema.d.ts +7 -2
  163. package/out/poi/schema.d.ts.map +1 -1
  164. package/out/poi/schema.js +3 -0
  165. package/out/poi/schema.js.map +1 -1
  166. package/out/schema/overture-match.d.ts +2 -1
  167. package/out/schema/overture-match.d.ts.map +1 -1
  168. package/out/schema/overture-match.js.map +1 -1
  169. package/out/weights/overlay-linker.d.ts +1 -1
  170. package/out/weights/overlay-linker.d.ts.map +1 -1
  171. package/out/weights/overlay-linker.js +5 -1
  172. package/out/weights/overlay-linker.js.map +1 -1
  173. package/package.json +12 -12
@@ -26,6 +26,7 @@ import { pathExists } from "@mailwoman/core/fs/readers/stat"
26
26
  import { resourceDictionaryPath } from "@mailwoman/dictionaries/paths"
27
27
  import type { PathBuilder, PathBuilderLike } from "path-ts"
28
28
 
29
+ import { BinaryFSTDel1Lookup, del1SidecarPathFor } from "#fst/del1-sidecar"
29
30
  import type { FSTMatcher } from "#fst/matcher"
30
31
  import { deserializeFST, readFSTProvenance } from "#fst/serialize"
31
32
  import type { FSTProvenance } from "#fst/types"
@@ -59,6 +60,12 @@ export interface LoadStreetMorphologyFSTOpts {
59
60
  * @defaultValue the libpostal dictionaries `@mailwoman/dictionaries` ships.
60
61
  */
61
62
  dictionariesDir?: PathBuilderLike
63
+ /**
64
+ * When true, builds an in-memory `FD1\0` sidecar on the dictionary-build fallback path.
65
+ *
66
+ * @defaultValue `false`
67
+ */
68
+ buildDel1Sidecar?: boolean
62
69
  /**
63
70
  * Unreadable-artifact diagnostics.
64
71
  *
@@ -69,6 +76,11 @@ export interface LoadStreetMorphologyFSTOpts {
69
76
 
70
77
  export interface LoadedStreetMorphologyFST {
71
78
  matcher: FSTMatcher
79
+ /**
80
+ * Companion `FD1\0` 1-delete sidecar loaded from `<artifact>.del1.bin`
81
+ * or built on fallback, or `null` when absent.
82
+ */
83
+ del1Sidecar: BinaryFSTDel1Lookup | null
72
84
  /**
73
85
  * Which rung produced the matcher: a sealed-artifact deserialize, or the per-process dictionary build.
74
86
  */
@@ -98,8 +110,22 @@ export async function loadStreetMorphologyFST(
98
110
  const buf = await readLocalBuffer(artifactPath)
99
111
  const matcher = deserializeFST(buf)
100
112
  const provenance = readFSTProvenance(buf)
113
+ let del1Sidecar: BinaryFSTDel1Lookup | null = null
114
+ const sidecarPath = del1SidecarPathFor(artifactPath)
115
+
116
+ if (await pathExists(sidecarPath)) {
117
+ try {
118
+ const sidecarBuf = await readLocalBuffer(sidecarPath)
119
+ del1Sidecar = new BinaryFSTDel1Lookup(sidecarBuf)
120
+ matcher.del1Sidecar = del1Sidecar
121
+ } catch (sidecarError) {
122
+ warn(
123
+ `street-morphology del1 sidecar at ${sidecarPath} unreadable (${(sidecarError as Error).message}) — continuing without fuzzy sidecar`
124
+ )
125
+ }
126
+ }
101
127
 
102
- return { matcher, source: "artifact", path: artifactPath, provenance }
128
+ return { matcher, del1Sidecar, source: "artifact", path: artifactPath, provenance }
103
129
  } catch (error) {
104
130
  warn(
105
131
  `street-morphology artifact at ${artifactPath} unreadable (${(error as Error).message}) — falling back to the dictionary build`
@@ -109,7 +135,11 @@ export async function loadStreetMorphologyFST(
109
135
 
110
136
  const built = await buildStreetMorphologyFST({
111
137
  dictionariesDir: (opts.dictionariesDir ?? resourceDictionaryPath("libpostal")).toString(),
138
+ ...(opts.buildDel1Sidecar ? { buildDel1Sidecar: true } : {}),
112
139
  })
113
140
 
114
- return { matcher: built.matcher, source: "built", path: null, provenance: built.provenance }
141
+ const del1Sidecar = built.del1Sidecar ? new BinaryFSTDel1Lookup(built.del1Sidecar.bytes) : null
142
+ built.matcher.del1Sidecar = del1Sidecar
143
+
144
+ return { matcher: built.matcher, del1Sidecar, source: "built", path: null, provenance: built.provenance }
115
145
  }
@@ -5,6 +5,8 @@ import { HOUSE_NUMBER_REPETITION_INDEXES } from "@mailwoman/codex/labels/house-n
5
5
  import { AbbreviationToDirectional, US_STREET_SUFFIX_LOOKUP } from "@mailwoman/codex/us"
6
6
  import type { Tagged } from "type-fest"
7
7
 
8
+ import { addressPostcodeKey } from "#address/keys/postcode"
9
+
8
10
  /**
9
11
  * A place name folded by {@link normalizeLocalityForKey}, minted only by calling the fold.
10
12
  */
@@ -271,6 +273,14 @@ export interface StreetKeyRules {
271
273
  * Builds the key a house number is stored and probed under.
272
274
  */
273
275
  houseNumberKey: (number: string) => string
276
+ /**
277
+ * Builds the normalized scope key a postcode is indexed and compared under.
278
+ */
279
+ postcodeKey: (postcode: string | null | undefined) => string | null
280
+ /**
281
+ * Street-type morphology used by `canonicalStreetKey` when classifying thoroughfare types.
282
+ */
283
+ canonicalMorphology: "leading" | "trailing-english" | "germanic-fused" | "han"
274
284
  /**
275
285
  * Matches a house number written as a base number with joined sub-numbers, and captures the base.
276
286
  * A reader probes the base after the written number misses.
@@ -347,6 +357,14 @@ function expandStreetSuffix(full: string): (words: string[]) => void {
347
357
  }
348
358
  }
349
359
 
360
+ /**
361
+ * Matches a Central European dual house number (`conscription/orientation` or `building/flat`),
362
+ * such as Czech `481/32` or Polish `12/4`, capturing the leading building number.
363
+ *
364
+ * Kept off `en` and `us`, where `12/345` denotes unit 12 at house number 345.
365
+ */
366
+ const SLASH_HOUSE_NUMBER_BASE = /^(\d+[a-z]?)\/\d+[a-z]?$/
367
+
350
368
  /**
351
369
  * The rules every locale has unless its row states another.
352
370
  */
@@ -359,6 +377,8 @@ const SHARED_KEY_RULES: StreetKeyRules = {
359
377
  arrondissementAtBaseCity: false,
360
378
  localityFromRegionAndSubregion: false,
361
379
  houseNumberKey: (number) => number.trim().toLowerCase(),
380
+ postcodeKey: (postcode) => addressPostcodeKey("", postcode),
381
+ canonicalMorphology: "leading",
362
382
  houseNumberBase: null,
363
383
  houseNumberRepetition: null,
364
384
  }
@@ -377,13 +397,21 @@ const SHARED_KEY_RULES: StreetKeyRules = {
377
397
  * - `und` has the shared rules only, for a country whose official languages select no one row.
378
398
  */
379
399
  const STREET_KEY_RULES: Readonly<Record<string, StreetKeyRules>> = {
380
- us: { ...SHARED_KEY_RULES, streetKey: normalizeStreetForKey, englishRespellings: true, localityKeys: "abbreviated" },
400
+ us: {
401
+ ...SHARED_KEY_RULES,
402
+ streetKey: normalizeStreetForKey,
403
+ englishRespellings: true,
404
+ localityKeys: "abbreviated",
405
+ postcodeKey: (postcode) => addressPostcodeKey("US", postcode),
406
+ canonicalMorphology: "trailing-english",
407
+ },
381
408
  en: {
382
409
  ...SHARED_KEY_RULES,
383
410
  streetKey: normalizeStreetForKey,
384
411
  // The route reads the surface and never the province, so a bilingual Canadian extract keys each street one way.
385
412
  surfaceRoute: { pattern: FRENCH_LEAD_TYPE, locale: "fr" },
386
413
  englishRespellings: true,
414
+ canonicalMorphology: "trailing-english",
387
415
  },
388
416
  es: { ...SHARED_KEY_RULES, streetKey: (street) => wordStreetKey(street, expandLeadingWord(ES_STREET_TYPE_ABBREV)) },
389
417
  fr: {
@@ -393,9 +421,18 @@ const STREET_KEY_RULES: Readonly<Record<string, StreetKeyRules>> = {
393
421
  // BAN writes `3 bis` across most of France and `3 b` in Paris, and a query may write either joined.
394
422
  houseNumberRepetition: HOUSE_NUMBER_REPETITION_INDEXES,
395
423
  },
396
- de: { ...SHARED_KEY_RULES, streetKey: (street) => wordStreetKey(street, expandStreetSuffix("strasse")) },
424
+ de: {
425
+ ...SHARED_KEY_RULES,
426
+ streetKey: (street) => wordStreetKey(street, expandStreetSuffix("strasse")),
427
+ canonicalMorphology: "germanic-fused",
428
+ houseNumberBase: SLASH_HOUSE_NUMBER_BASE,
429
+ },
397
430
  it: { ...SHARED_KEY_RULES, streetKey: (street) => wordStreetKey(street, expandLeadingWord(IT_STREET_TYPE_ABBREV)) },
398
- nl: { ...SHARED_KEY_RULES, streetKey: (street) => wordStreetKey(street, expandStreetSuffix("straat")) },
431
+ nl: {
432
+ ...SHARED_KEY_RULES,
433
+ streetKey: (street) => wordStreetKey(street, expandStreetSuffix("straat")),
434
+ canonicalMorphology: "germanic-fused",
435
+ },
399
436
  pl: {
400
437
  ...SHARED_KEY_RULES,
401
438
  streetKey: (street) =>
@@ -408,6 +445,7 @@ const STREET_KEY_RULES: Readonly<Record<string, StreetKeyRules>> = {
408
445
  words.shift()
409
446
  }
410
447
  }),
448
+ houseNumberBase: SLASH_HOUSE_NUMBER_BASE,
411
449
  },
412
450
  vn: {
413
451
  ...SHARED_KEY_RULES,
@@ -425,6 +463,7 @@ const STREET_KEY_RULES: Readonly<Record<string, StreetKeyRules>> = {
425
463
  streetKey: (street) => foldHan(street) as StreetKey,
426
464
  localityKey: (locality) => foldHan(locality) as NameKey,
427
465
  localityFromRegionAndSubregion: true,
466
+ canonicalMorphology: "han",
428
467
  // A Taiwanese house number ends in 號 and may be written in full-width digits.
429
468
  houseNumberKey: (number) =>
430
469
  foldHan(number)
@@ -433,7 +472,10 @@ const STREET_KEY_RULES: Readonly<Record<string, StreetKeyRules>> = {
433
472
  // 之 and 附 join a sub-number to a base number, and the Taiwanese register stores the base.
434
473
  houseNumberBase: /^(\d+)(?:[之附]\d+)+$/u,
435
474
  },
436
- und: SHARED_KEY_RULES,
475
+ und: {
476
+ ...SHARED_KEY_RULES,
477
+ houseNumberBase: SLASH_HOUSE_NUMBER_BASE,
478
+ },
437
479
  }
438
480
 
439
481
  /**
@@ -674,6 +716,88 @@ export function directionalRelaxedKeys(key: StreetKey): StreetKey[] {
674
716
  return forms.filter((form) => form !== key) as StreetKey[]
675
717
  }
676
718
 
719
+ /**
720
+ * Canonical USPS topographic and natural-feature words that appear abbreviated mid-street before a
721
+ * thoroughfare suffix (`Little Crk Xing` → `little creek crossing`, `Oak Rdg Rd` → `oak ridge road`).
722
+ *
723
+ * This restriction to feature words keeps doubled road-suffix tails
724
+ * (`Saint Pauls PL St` → `saint pauls place`) unchanged.
725
+ */
726
+ const INTERIOR_FEATURE_CANONICALS: ReadonlySet<string> = new Set([
727
+ "beach",
728
+ "bluff",
729
+ "bluffs",
730
+ "branch",
731
+ "brook",
732
+ "brooks",
733
+ "burg",
734
+ "canyon",
735
+ "cliff",
736
+ "cliffs",
737
+ "cove",
738
+ "coves",
739
+ "creek",
740
+ "crest",
741
+ "dale",
742
+ "falls",
743
+ "field",
744
+ "fields",
745
+ "ford",
746
+ "fords",
747
+ "forest",
748
+ "fork",
749
+ "forks",
750
+ "glen",
751
+ "glens",
752
+ "grove",
753
+ "groves",
754
+ "harbor",
755
+ "harbors",
756
+ "haven",
757
+ "hill",
758
+ "hills",
759
+ "hollow",
760
+ "island",
761
+ "islands",
762
+ "knoll",
763
+ "knolls",
764
+ "lake",
765
+ "lakes",
766
+ "meadow",
767
+ "meadows",
768
+ "mill",
769
+ "mills",
770
+ "mountain",
771
+ "mountains",
772
+ "orchard",
773
+ "pine",
774
+ "pines",
775
+ "plain",
776
+ "plains",
777
+ "point",
778
+ "points",
779
+ "prairie",
780
+ "ranch",
781
+ "rapids",
782
+ "ridge",
783
+ "ridges",
784
+ "river",
785
+ "shoal",
786
+ "shoals",
787
+ "shore",
788
+ "shores",
789
+ "spring",
790
+ "springs",
791
+ "stream",
792
+ "summit",
793
+ "valley",
794
+ "valleys",
795
+ "view",
796
+ "views",
797
+ "well",
798
+ "wells",
799
+ ])
800
+
677
801
  /**
678
802
  * Lists the lookup keys to probe for a street span, most literal first, adding the
679
803
  * English respellings for a locale whose rules probe them.
@@ -704,6 +828,31 @@ export function streetKeyVariants(street: string, locale: StreetLocale): StreetK
704
828
  }
705
829
  }
706
830
 
831
+ // `normalizeStreetForKey` stops after expanding the trailing suffix, leaving abbreviated
832
+ // topographic words before it unexpanded ("Little Crk Xing" → "little crk crossing").
833
+ // Probe the interior-expanded form as a query-side variant so pre-built databases stay valid.
834
+ if (tokens.length >= MIN_TOKENS_FOR_TAIL_MERGE) {
835
+ let expandedAny = false
836
+
837
+ const expanded = tokens.map((token, i) => {
838
+ if (i === 0 || i === tokens.length - 1) return token
839
+ const canonical = US_STREET_SUFFIX_LOOKUP.get(token)?.toLowerCase()
840
+
841
+ if (!canonical || canonical === token || !INTERIOR_FEATURE_CANONICALS.has(canonical)) return token
842
+ expandedAny = true
843
+
844
+ return canonical
845
+ })
846
+
847
+ if (expandedAny) {
848
+ const candidate = expanded.join(" ") as StreetKey
849
+
850
+ if (!variants.includes(candidate)) {
851
+ variants.push(candidate)
852
+ }
853
+ }
854
+ }
855
+
707
856
  // NAD writes a numbered street with its ordinal suffix ("48th St")
708
857
  // and NYC Open Data writes the bare number ("48 ST").
709
858
  // The build keys each row as written, so the reader probes both forms.
@@ -1,6 +1,7 @@
1
1
  // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
2
  // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
3
 
4
+ export * from "#labels/street/canonical"
4
5
  export * from "#labels/street/centroid"
5
6
  export * from "#labels/street/centroid/schema"
6
7
  export * from "#labels/street/morphology/fst/builder"
package/lib/landmark.ts CHANGED
@@ -19,8 +19,17 @@ import { missingOutcomeTablesIn, OutcomeMembershipReader, outcomeAdmitPredicate
19
19
  import type { LandmarkOutcomeDatabase } from "#outcome/schema"
20
20
  import { prepareAll, type PreparedAll } from "#sqlite-utils"
21
21
 
22
+ /**
23
+ * One country that a border or single-country landmark spans.
24
+ */
25
+ export interface LandmarkCountryTable {
26
+ qid: string
27
+ country: string
28
+ }
29
+
22
30
  export interface LandmarkDatabase extends LandmarkOutcomeDatabase {
23
31
  landmark_name: LandmarkNameTable
32
+ landmark_country: LandmarkCountryTable
24
33
  }
25
34
 
26
35
  /**
@@ -64,6 +73,14 @@ export async function createLandmarkTables(db: Kysely<LandmarkDatabase>): Promis
64
73
  .addPrimaryKeyConstraint("landmark_name_pk", ["name_key", "qid"])
65
74
  .modifyEnd(sql`without rowid`)
66
75
  .execute()
76
+
77
+ await db.schema
78
+ .createTable("landmark_country")
79
+ .addColumn("qid", "text", (c) => c.notNull())
80
+ .addColumn("country", "text", (c) => c.notNull())
81
+ .addPrimaryKeyConstraint("landmark_country_pk", ["qid", "country"])
82
+ .modifyEnd(sql`without rowid`)
83
+ .execute()
67
84
  }
68
85
 
69
86
  /**
@@ -81,6 +98,7 @@ export class LandmarkSqliteLookup implements LandmarkLookup, Disposable {
81
98
  readonly #db: DatabaseClient<LandmarkDatabase>
82
99
 
83
100
  readonly #byName: PreparedAll<[nameKey: string], LandmarkNameTable>
101
+ readonly #countriesByQID: PreparedAll<[qid: string], Pick<LandmarkCountryTable, "country">> | null
84
102
 
85
103
  readonly #layer: string
86
104
  readonly #missingOutcomeTables: readonly string[]
@@ -94,6 +112,15 @@ export class LandmarkSqliteLookup implements LandmarkLookup, Disposable {
94
112
  this.#db = new DatabaseClient<LandmarkDatabase>(path, { readOnly: true })
95
113
 
96
114
  this.#byName = prepareAll(this.#db, LANDMARK_BY_NAME_SQL)
115
+
116
+ const hasCountryTable = Boolean(
117
+ this.#db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'landmark_country'").get()
118
+ )
119
+
120
+ this.#countriesByQID = hasCountryTable
121
+ ? prepareAll(this.#db, "SELECT country FROM landmark_country WHERE qid = ? ORDER BY country")
122
+ : null
123
+
97
124
  this.#layer = layer
98
125
  this.#missingOutcomeTables = missingOutcomeTablesIn(this.#db)
99
126
  this.#outcomes = OutcomeMembershipReader.open(this.#db, { recordColumn: "qid", layer })
@@ -107,19 +134,33 @@ export class LandmarkSqliteLookup implements LandmarkLookup, Disposable {
107
134
  find(name: string, outcome?: OutcomeQuery): LandmarkHit[] {
108
135
  const key = landmarkNameKey(name)
109
136
 
110
- if (!outcome) return landmarkHitsFromRows(this.#byName(key), name)
137
+ if (!outcome) return this.#attachCountries(landmarkHitsFromRows(this.#byName(key), name))
111
138
 
112
139
  refuseMissingOutcomeTables(`the ${this.#layer} database`, this.#missingOutcomeTables)
113
140
 
114
141
  const rows = outcome.admit ? this.#admittedRows(key, outcome.admit) : this.#byName(key)
115
142
 
116
143
  // A landmark row's defaults are keyed by its `kind`, as a candidate row's are by its placetype.
117
- return landmarkHitsFromRows(rows, name).map((hit) => ({
144
+ return this.#attachCountries(landmarkHitsFromRows(rows, name)).map((hit) => ({
118
145
  ...hit,
119
146
  outcomes: this.#outcomes!.read(hit.qid, hit.kind, outcome),
120
147
  }))
121
148
  }
122
149
 
150
+ #attachCountries(hits: LandmarkHit[]): LandmarkHit[] {
151
+ if (!this.#countriesByQID || !hits.length) return hits
152
+
153
+ return hits.map((hit) => {
154
+ const stored = this.#countriesByQID!(hit.qid).map((row) => row.country)
155
+
156
+ if (!stored.length) return hit
157
+
158
+ const ordered = hit.country ? [hit.country, ...stored.filter((code) => code !== hit.country)] : stored
159
+
160
+ return { ...hit, countries: ordered }
161
+ })
162
+ }
163
+
123
164
  missingOutcomeTables(): readonly string[] {
124
165
  return this.#missingOutcomeTables
125
166
  }
@@ -200,12 +200,6 @@ export interface FindPlaceQuery {
200
200
  */
201
201
  postcode?: string
202
202
 
203
- /**
204
- * Whether a locality query with `postcode` moves candidates near the postcode's centroid
205
- * to the front, sorted by distance, with the rest keeping their order.
206
- */
207
- postcodeContainmentCoherence?: boolean
208
-
209
203
  /**
210
204
  * The parsed region qualifier for a locality lookup.
211
205
  *