@mailwoman/corpus 11.5.0 → 12.1.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 (232) hide show
  1. package/data/address-source-register.json +545 -11
  2. package/data/builds/v0.8.22-wofrank-overlay/OVERLAY_PLAN.json +51 -0
  3. package/data/builds/v0.8.24-everyplace/inputs.json +35 -0
  4. package/data/license-decisions.json +440 -9
  5. package/data/licenses/PROVENANCE.md +27 -18
  6. package/data/licenses/at-bev-adressregister-cc-by-4.0.md +25 -0
  7. package/data/licenses/br-ibge-cnefe-open-data.md +21 -0
  8. package/data/licenses/ca-qc-mrnf-cc-by-4.0.md +29 -0
  9. package/data/licenses/ca-statcan-open-licence.md +41 -0
  10. package/data/licenses/ca-yukon-ogl-2.0.md +19 -0
  11. package/data/licenses/dk-dmikg-cc-by-4.0.md +43 -0
  12. package/data/licenses/jp-mlit-isj-pdl-1.0.md +35 -0
  13. package/data/source-additions.json +90 -0
  14. package/data/source-resolutions.json +3 -2
  15. package/data/training-configs/v8.17.0-stage1-gazetteer-v0822-s42.yaml +557 -0
  16. package/data/training-configs/v8.17.0-stage2-corpus-v0822-s42.yaml +1008 -0
  17. package/data/training-configs/v8.18.0-gazetteer-then-mixture-v0822-s42.yaml +1225 -0
  18. package/data/training-configs/v8.20.0-wofrank-v0822-s42.yaml +863 -0
  19. package/data/training-configs/v8.21.0-outcomes-v0822-s42.yaml +873 -0
  20. package/data/training-configs/v8.22.0-stage1-everyplace-v0824-s42.yaml +374 -0
  21. package/data/training-configs/v8.22.0-stage2-corpus-v0824-s42.yaml +808 -0
  22. package/data/training-configs/v8.22.1-stage1-everyplace-v0824-s42.yaml +376 -0
  23. package/data/training-configs/v8.22.1-stage2-corpus-v0824-s42.yaml +793 -0
  24. package/data/training-configs/v9.0.0-bytestem-v090-s42.yaml +665 -0
  25. package/data/training-configs/v9.1.0-bytestem-global-v091-s42.yaml +710 -0
  26. package/data/training-configs/v9.2.0-bytestem-global-v092-s42.yaml +714 -0
  27. package/data/training-manifests/v0.8.22-wofrank-overlay.json +9000 -0
  28. package/data/training-manifests/v0.8.24-everyplace-overlay.json +9000 -0
  29. package/data/training-manifests/v0.9.0-compact.effective.json +2556 -0
  30. package/data/training-manifests/v0.9.0-compact.json +9000 -0
  31. package/data/training-manifests/v0.9.1-compact.json +9660 -0
  32. package/data/training-manifests/v0.9.2-compact.effective.json +2670 -0
  33. package/data/training-manifests/v0.9.2-compact.json +9660 -0
  34. package/lib/adapters/geonames/features/adapter.ts +29 -11
  35. package/lib/adapters/overture/adapter.ts +26 -6
  36. package/lib/adapters/street/line.ts +6 -5
  37. package/lib/adapters/street/unit.ts +7 -0
  38. package/lib/adapters/wof/admin/json/adapter.ts +38 -3
  39. package/lib/adapters/wof/json-rows.ts +84 -5
  40. package/lib/adapters/wof/sample.ts +42 -6
  41. package/lib/au/adapters/gnaf/source.ts +12 -0
  42. package/lib/build/code-hash.ts +1 -1
  43. package/lib/cjk/foreign-places.ts +1 -1
  44. package/lib/fr/adapters/ban/adapter.ts +57 -8
  45. package/lib/fr/recipes/admin-split.ts +14 -2
  46. package/lib/fr/recipes/lieudit.ts +1 -1
  47. package/lib/fr/recipes/order.ts +14 -5
  48. package/lib/international/recipes/locale.ts +1 -1
  49. package/lib/kr/adapters/localdata/permit-rows.ts +1 -1
  50. package/lib/parquet/schema.ts +7 -0
  51. package/lib/recipes/po/box/cedex/recipe.ts +1 -1
  52. package/lib/recipes/po/box/cedex/vocabulary.ts +4 -4
  53. package/lib/recipes/trailing-region.ts +147 -17
  54. package/lib/synthesizers/po-box.ts +51 -12
  55. package/lib/test-kit/corpus-recipe.ts +3 -0
  56. package/lib/tools/anthropic/batches.ts +221 -0
  57. package/lib/tools/anthropic/labeling.ts +263 -0
  58. package/lib/tools/compact-v9.ts +890 -0
  59. package/lib/tools/fetch/wikidata/vital-places/entities.ts +20 -2
  60. package/lib/tools/fetch/wikidata/vital-places/qrank.ts +5 -4
  61. package/lib/tools/fragment-assay.run.ts +2 -1
  62. package/lib/tools/name-roles/name-roles.run.ts +296 -0
  63. package/lib/tools/name-roles/prompt.ts +253 -0
  64. package/lib/tools/overlay/deletions.ts +1 -1
  65. package/lib/tools/overlay/draw-weight.ts +275 -0
  66. package/lib/tools/place-twins/place-twins.run.ts +528 -0
  67. package/lib/tools/place-twins/prompt.ts +276 -0
  68. package/lib/tools/prominent-shapes/prominent-shapes.run.ts +420 -0
  69. package/lib/tools/prominent-shapes/prompt.ts +189 -0
  70. package/lib/tools/prominent-shapes/read-seeds.ts +286 -0
  71. package/lib/tools/prominent-shapes/seeds.ts +281 -0
  72. package/lib/tools/prominent-shapes/validate.ts +563 -0
  73. package/lib/tools/source-register/build.ts +1 -0
  74. package/lib/training/config/admission.ts +9 -2
  75. package/lib/training/config/schema.ts +68 -1
  76. package/lib/types.ts +23 -0
  77. package/lib/us/adapters/usgov/usgs-structures/adapter.ts +103 -6
  78. package/lib/utils/split.ts +1 -1
  79. package/out/adapters/geonames/features/adapter.d.ts +2 -2
  80. package/out/adapters/geonames/features/adapter.d.ts.map +1 -1
  81. package/out/adapters/geonames/features/adapter.js +25 -11
  82. package/out/adapters/geonames/features/adapter.js.map +1 -1
  83. package/out/adapters/overture/adapter.d.ts.map +1 -1
  84. package/out/adapters/overture/adapter.js +21 -5
  85. package/out/adapters/overture/adapter.js.map +1 -1
  86. package/out/adapters/street/line.d.ts +4 -4
  87. package/out/adapters/street/line.d.ts.map +1 -1
  88. package/out/adapters/street/line.js +5 -5
  89. package/out/adapters/street/line.js.map +1 -1
  90. package/out/adapters/street/unit.d.ts +4 -0
  91. package/out/adapters/street/unit.d.ts.map +1 -1
  92. package/out/adapters/street/unit.js +3 -0
  93. package/out/adapters/street/unit.js.map +1 -1
  94. package/out/adapters/wof/admin/json/adapter.d.ts +7 -0
  95. package/out/adapters/wof/admin/json/adapter.d.ts.map +1 -1
  96. package/out/adapters/wof/admin/json/adapter.js +28 -3
  97. package/out/adapters/wof/admin/json/adapter.js.map +1 -1
  98. package/out/adapters/wof/json-rows.d.ts +8 -0
  99. package/out/adapters/wof/json-rows.d.ts.map +1 -1
  100. package/out/adapters/wof/json-rows.js +56 -4
  101. package/out/adapters/wof/json-rows.js.map +1 -1
  102. package/out/adapters/wof/sample.d.ts +12 -0
  103. package/out/adapters/wof/sample.d.ts.map +1 -1
  104. package/out/adapters/wof/sample.js +37 -6
  105. package/out/adapters/wof/sample.js.map +1 -1
  106. package/out/au/adapters/gnaf/source.d.ts +9 -0
  107. package/out/au/adapters/gnaf/source.d.ts.map +1 -1
  108. package/out/au/adapters/gnaf/source.js +9 -0
  109. package/out/au/adapters/gnaf/source.js.map +1 -1
  110. package/out/build/code-hash.js +1 -1
  111. package/out/build/code-hash.js.map +1 -1
  112. package/out/cjk/foreign-places.js +1 -1
  113. package/out/cjk/foreign-places.js.map +1 -1
  114. package/out/fr/adapters/ban/adapter.d.ts.map +1 -1
  115. package/out/fr/adapters/ban/adapter.js +49 -7
  116. package/out/fr/adapters/ban/adapter.js.map +1 -1
  117. package/out/fr/recipes/admin-split.d.ts.map +1 -1
  118. package/out/fr/recipes/admin-split.js +15 -2
  119. package/out/fr/recipes/admin-split.js.map +1 -1
  120. package/out/fr/recipes/lieudit.js +1 -1
  121. package/out/fr/recipes/lieudit.js.map +1 -1
  122. package/out/fr/recipes/order.d.ts.map +1 -1
  123. package/out/fr/recipes/order.js +16 -5
  124. package/out/fr/recipes/order.js.map +1 -1
  125. package/out/international/recipes/locale.js +1 -1
  126. package/out/international/recipes/locale.js.map +1 -1
  127. package/out/kr/adapters/localdata/permit-rows.js +1 -1
  128. package/out/kr/adapters/localdata/permit-rows.js.map +1 -1
  129. package/out/parquet/schema.d.ts +6 -0
  130. package/out/parquet/schema.d.ts.map +1 -1
  131. package/out/parquet/schema.js +6 -0
  132. package/out/parquet/schema.js.map +1 -1
  133. package/out/recipes/po/box/cedex/recipe.js +1 -1
  134. package/out/recipes/po/box/cedex/recipe.js.map +1 -1
  135. package/out/recipes/po/box/cedex/vocabulary.d.ts +1 -1
  136. package/out/recipes/po/box/cedex/vocabulary.d.ts.map +1 -1
  137. package/out/recipes/po/box/cedex/vocabulary.js +4 -4
  138. package/out/recipes/po/box/cedex/vocabulary.js.map +1 -1
  139. package/out/recipes/trailing-region.d.ts.map +1 -1
  140. package/out/recipes/trailing-region.js +122 -17
  141. package/out/recipes/trailing-region.js.map +1 -1
  142. package/out/synthesizers/po-box.d.ts +7 -0
  143. package/out/synthesizers/po-box.d.ts.map +1 -1
  144. package/out/synthesizers/po-box.js +36 -12
  145. package/out/synthesizers/po-box.js.map +1 -1
  146. package/out/test-kit/corpus-recipe.d.ts +3 -0
  147. package/out/test-kit/corpus-recipe.d.ts.map +1 -1
  148. package/out/test-kit/corpus-recipe.js.map +1 -1
  149. package/out/tools/anthropic/batches.d.ts +117 -0
  150. package/out/tools/anthropic/batches.d.ts.map +1 -0
  151. package/out/tools/anthropic/batches.js +141 -0
  152. package/out/tools/anthropic/batches.js.map +1 -0
  153. package/out/tools/anthropic/labeling.d.ts +97 -0
  154. package/out/tools/anthropic/labeling.d.ts.map +1 -0
  155. package/out/tools/anthropic/labeling.js +146 -0
  156. package/out/tools/anthropic/labeling.js.map +1 -0
  157. package/out/tools/compact-v9.d.ts +124 -0
  158. package/out/tools/compact-v9.d.ts.map +1 -0
  159. package/out/tools/compact-v9.js +507 -0
  160. package/out/tools/compact-v9.js.map +1 -0
  161. package/out/tools/fetch/wikidata/vital-places/entities.d.ts +3 -0
  162. package/out/tools/fetch/wikidata/vital-places/entities.d.ts.map +1 -1
  163. package/out/tools/fetch/wikidata/vital-places/entities.js +19 -1
  164. package/out/tools/fetch/wikidata/vital-places/entities.js.map +1 -1
  165. package/out/tools/fetch/wikidata/vital-places/qrank.d.ts.map +1 -1
  166. package/out/tools/fetch/wikidata/vital-places/qrank.js +4 -4
  167. package/out/tools/fetch/wikidata/vital-places/qrank.js.map +1 -1
  168. package/out/tools/fragment-assay.run.js +1 -1
  169. package/out/tools/fragment-assay.run.js.map +1 -1
  170. package/out/tools/name-roles/name-roles.run.d.ts +2 -0
  171. package/out/tools/name-roles/name-roles.run.d.ts.map +1 -0
  172. package/out/tools/name-roles/name-roles.run.js +230 -0
  173. package/out/tools/name-roles/name-roles.run.js.map +1 -0
  174. package/out/tools/name-roles/prompt.d.ts +166 -0
  175. package/out/tools/name-roles/prompt.d.ts.map +1 -0
  176. package/out/tools/name-roles/prompt.js +176 -0
  177. package/out/tools/name-roles/prompt.js.map +1 -0
  178. package/out/tools/overlay/deletions.js +1 -1
  179. package/out/tools/overlay/deletions.js.map +1 -1
  180. package/out/tools/overlay/draw-weight.d.ts +86 -0
  181. package/out/tools/overlay/draw-weight.d.ts.map +1 -0
  182. package/out/tools/overlay/draw-weight.js +131 -0
  183. package/out/tools/overlay/draw-weight.js.map +1 -0
  184. package/out/tools/place-twins/place-twins.run.d.ts +2 -0
  185. package/out/tools/place-twins/place-twins.run.d.ts.map +1 -0
  186. package/out/tools/place-twins/place-twins.run.js +385 -0
  187. package/out/tools/place-twins/place-twins.run.js.map +1 -0
  188. package/out/tools/place-twins/prompt.d.ts +169 -0
  189. package/out/tools/place-twins/prompt.d.ts.map +1 -0
  190. package/out/tools/place-twins/prompt.js +183 -0
  191. package/out/tools/place-twins/prompt.js.map +1 -0
  192. package/out/tools/prominent-shapes/prominent-shapes.run.d.ts +2 -0
  193. package/out/tools/prominent-shapes/prominent-shapes.run.d.ts.map +1 -0
  194. package/out/tools/prominent-shapes/prominent-shapes.run.js +302 -0
  195. package/out/tools/prominent-shapes/prominent-shapes.run.js.map +1 -0
  196. package/out/tools/prominent-shapes/prompt.d.ts +74 -0
  197. package/out/tools/prominent-shapes/prompt.d.ts.map +1 -0
  198. package/out/tools/prominent-shapes/prompt.js +156 -0
  199. package/out/tools/prominent-shapes/prompt.js.map +1 -0
  200. package/out/tools/prominent-shapes/read-seeds.d.ts +23 -0
  201. package/out/tools/prominent-shapes/read-seeds.d.ts.map +1 -0
  202. package/out/tools/prominent-shapes/read-seeds.js +219 -0
  203. package/out/tools/prominent-shapes/read-seeds.js.map +1 -0
  204. package/out/tools/prominent-shapes/seeds.d.ts +132 -0
  205. package/out/tools/prominent-shapes/seeds.d.ts.map +1 -0
  206. package/out/tools/prominent-shapes/seeds.js +178 -0
  207. package/out/tools/prominent-shapes/seeds.js.map +1 -0
  208. package/out/tools/prominent-shapes/validate.d.ts +152 -0
  209. package/out/tools/prominent-shapes/validate.d.ts.map +1 -0
  210. package/out/tools/prominent-shapes/validate.js +389 -0
  211. package/out/tools/prominent-shapes/validate.js.map +1 -0
  212. package/out/tools/source-register/build.d.ts.map +1 -1
  213. package/out/tools/source-register/build.js +1 -0
  214. package/out/tools/source-register/build.js.map +1 -1
  215. package/out/training/config/admission.d.ts +2 -1
  216. package/out/training/config/admission.d.ts.map +1 -1
  217. package/out/training/config/admission.js +9 -2
  218. package/out/training/config/admission.js.map +1 -1
  219. package/out/training/config/schema.d.ts +276 -1
  220. package/out/training/config/schema.d.ts.map +1 -1
  221. package/out/training/config/schema.js +64 -1
  222. package/out/training/config/schema.js.map +1 -1
  223. package/out/types.d.ts +21 -0
  224. package/out/types.d.ts.map +1 -1
  225. package/out/types.js +13 -0
  226. package/out/types.js.map +1 -1
  227. package/out/us/adapters/usgov/usgs-structures/adapter.d.ts.map +1 -1
  228. package/out/us/adapters/usgov/usgs-structures/adapter.js +78 -4
  229. package/out/us/adapters/usgov/usgs-structures/adapter.js.map +1 -1
  230. package/out/utils/split.js +1 -1
  231. package/out/utils/split.js.map +1 -1
  232. package/package.json +24 -24
@@ -0,0 +1,281 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * @file The seed of one prominent place: the verified surfaces a query form may be written from (#3156).
6
+ *
7
+ * A seed lists every surface a form may contain, each under one slot and with the record it came from.
8
+ * The language model that writes the forms chooses among these surfaces and supplies none of its own,
9
+ * so a form's labels are the seed's.
10
+ *
11
+ * The slots follow the #3156 label policy (`prominentPlaceTag` in `#recipes/prominent/places`):
12
+ * a locality is a `locality` in every form, a sub-locality is a `dependent_locality` beside its parent
13
+ * locality, and a postal pair is a `locality` beside its parent's name as a `region`.
14
+ */
15
+
16
+ import { foldCaseWhitespace } from "@mailwoman/normalize/fold"
17
+
18
+ import type { PlaceBand } from "#recipes/prominent/places"
19
+
20
+ /**
21
+ * The slots a surface fills, by the letter its key starts with.
22
+ */
23
+ export const SurfaceSlot = {
24
+ /**
25
+ * The place's own name.
26
+ */
27
+ Name: "n",
28
+ /**
29
+ * The parent locality of a sub-locality.
30
+ */
31
+ Parent: "p",
32
+ /**
33
+ * The region's name.
34
+ */
35
+ Region: "r",
36
+ /**
37
+ * The region's abbreviation.
38
+ */
39
+ RegionAbbreviation: "a",
40
+ /**
41
+ * The country.
42
+ */
43
+ Country: "c",
44
+ } as const
45
+
46
+ /**
47
+ * One slot.
48
+ */
49
+ export type SurfaceSlot = (typeof SurfaceSlot)[keyof typeof SurfaceSlot]
50
+
51
+ /**
52
+ * One surface a form may contain.
53
+ */
54
+ export interface SeedSurface {
55
+ /**
56
+ * The slot letter and a one-based index, the key the model cites: `n1`, `c3`.
57
+ */
58
+ key: string
59
+ slot: SurfaceSlot
60
+ text: string
61
+ /**
62
+ * The ISO 639-3 language of a WOF `names` row, or `null` for a surface recorded without one.
63
+ */
64
+ language: string | null
65
+ /**
66
+ * The record the surface was read from, such as `wof:names:85922583:eng:preferred`.
67
+ */
68
+ origin: string
69
+ }
70
+
71
+ /**
72
+ * A surface before it has a key.
73
+ */
74
+ export type SurfaceDraft = Omit<SeedSurface, "key" | "slot">
75
+
76
+ /**
77
+ * One prominent place and the surfaces its forms are written from.
78
+ */
79
+ export interface ShapeSeed {
80
+ wof_id: number
81
+ country: string
82
+ band: PlaceBand
83
+ /**
84
+ * The place's rank within its country and band, or `null` for a capital the rank left out.
85
+ */
86
+ rank: number | null
87
+ capital: boolean
88
+ /**
89
+ * The tag of the place's own span.
90
+ */
91
+ place_tag: "locality" | "dependent_locality"
92
+ /**
93
+ * The tag of a parent span, or `null` when the seed has no parent slot.
94
+ */
95
+ parent_tag: "locality" | "region" | null
96
+ surfaces: SeedSurface[]
97
+ }
98
+
99
+ /**
100
+ * The reasons a prominent place gets no seed.
101
+ */
102
+ export const SeedSkip = {
103
+ /**
104
+ * A sub-locality without a parent locality: the label policy writes it only beside one.
105
+ */
106
+ NoParent: "sublocality_without_parent",
107
+ /**
108
+ * A sub-locality whose name equals its parent's, so every query form reads the same for both.
109
+ */
110
+ ParentNamesake: "sublocality_named_as_parent",
111
+ /**
112
+ * Every name of the place is refused as a typed surface, so no form could cite a name.
113
+ */
114
+ NoName: "no_typed_name",
115
+ } as const
116
+
117
+ /**
118
+ * One skip reason.
119
+ */
120
+ export type SeedSkip = (typeof SeedSkip)[keyof typeof SeedSkip]
121
+
122
+ /**
123
+ * What one prominent place contributes to a seed, as read from the census and Who's On First.
124
+ */
125
+ export interface SeedInput {
126
+ wof_id: number
127
+ country: string
128
+ band: PlaceBand
129
+ rank: number | null
130
+ capital: boolean
131
+ postal_pair: boolean
132
+ names: readonly SurfaceDraft[]
133
+ parents: readonly SurfaceDraft[]
134
+ regions: readonly SurfaceDraft[]
135
+ region_abbreviations: readonly SurfaceDraft[]
136
+ countries: readonly SurfaceDraft[]
137
+ }
138
+
139
+ /**
140
+ * A region abbreviation a form may write: one to three Latin letters or digits, the #3156 census's filter.
141
+ */
142
+ function isWritableRegionAbbreviation(abbreviation: string): boolean {
143
+ return /^[A-Za-z0-9]{1,3}$/u.test(abbreviation)
144
+ }
145
+
146
+ /**
147
+ * Text that marks a name as an article title or a rendering artifact rather than a typed surface:
148
+ * a parenthesized qualifier (`Vaticano (disambigua)`) or a bidirectional control character.
149
+ */
150
+ const UNTYPED_SURFACE = /[()‎‏‪-‮]/u
151
+
152
+ /**
153
+ * The drafts with a typed, non-empty text, one per folded text, in the order given.
154
+ */
155
+ function distinctDrafts(drafts: readonly SurfaceDraft[]): SurfaceDraft[] {
156
+ const seen = new Set<string>()
157
+ const kept: SurfaceDraft[] = []
158
+
159
+ for (const draft of drafts) {
160
+ const fold = foldCaseWhitespace(draft.text)
161
+
162
+ if (fold === "" || seen.has(fold) || UNTYPED_SURFACE.test(draft.text)) continue
163
+
164
+ seen.add(fold)
165
+ kept.push({ ...draft, text: draft.text.trim() })
166
+ }
167
+
168
+ return kept
169
+ }
170
+
171
+ function keyed(slot: SurfaceSlot, drafts: readonly SurfaceDraft[]): SeedSurface[] {
172
+ return distinctDrafts(drafts).map((draft, index) => ({ key: `${slot}${index + 1}`, slot, ...draft }))
173
+ }
174
+
175
+ /**
176
+ * Builds a place's seed, or returns the reason the label policy writes no form for it.
177
+ *
178
+ * A place whose every name is refused gets no seed in either band, because every form needs one name.
179
+ * A locality has no parent slot.
180
+ *
181
+ * A postal pair writes its parent's name in the region slot, and keeps the region's
182
+ * abbreviations only when the region is that same name (`Brooklyn, NY`).
183
+ */
184
+ export function assembleSeed(input: SeedInput): ShapeSeed | SeedSkip {
185
+ const names = keyed(SurfaceSlot.Name, input.names)
186
+
187
+ if (!names.length) return SeedSkip.NoName
188
+
189
+ const parents = distinctDrafts(input.parents)
190
+
191
+ const base = {
192
+ wof_id: input.wof_id,
193
+ country: input.country,
194
+ band: input.band,
195
+ rank: input.rank,
196
+ capital: input.capital,
197
+ }
198
+
199
+ const countries = keyed(SurfaceSlot.Country, input.countries)
200
+ const abbreviations = input.region_abbreviations.filter((draft) => isWritableRegionAbbreviation(draft.text))
201
+
202
+ if (input.band === "locality") {
203
+ return {
204
+ ...base,
205
+ place_tag: "locality",
206
+ parent_tag: null,
207
+ surfaces: [
208
+ ...names,
209
+ ...keyed(SurfaceSlot.Region, input.regions),
210
+ ...keyed(SurfaceSlot.RegionAbbreviation, abbreviations),
211
+ ...countries,
212
+ ],
213
+ }
214
+ }
215
+
216
+ if (!parents.length) return SeedSkip.NoParent
217
+
218
+ const nameFolds = new Set(names.map((name) => foldCaseWhitespace(name.text)))
219
+
220
+ if (parents.some((parent) => nameFolds.has(foldCaseWhitespace(parent.text)))) return SeedSkip.ParentNamesake
221
+
222
+ if (input.postal_pair) {
223
+ const parentFolds = new Set(parents.map((parent) => foldCaseWhitespace(parent.text)))
224
+ const regionIsParent = input.regions.some((region) => parentFolds.has(foldCaseWhitespace(region.text)))
225
+
226
+ return {
227
+ ...base,
228
+ place_tag: "locality",
229
+ parent_tag: "region",
230
+ surfaces: [
231
+ ...names,
232
+ ...keyed(SurfaceSlot.Parent, parents),
233
+ ...(regionIsParent ? keyed(SurfaceSlot.RegionAbbreviation, abbreviations) : []),
234
+ ...countries,
235
+ ],
236
+ }
237
+ }
238
+
239
+ return {
240
+ ...base,
241
+ place_tag: "dependent_locality",
242
+ parent_tag: "locality",
243
+ surfaces: [
244
+ ...names,
245
+ ...keyed(SurfaceSlot.Parent, parents),
246
+ ...keyed(SurfaceSlot.Region, input.regions),
247
+ ...keyed(SurfaceSlot.RegionAbbreviation, abbreviations),
248
+ ...countries,
249
+ ],
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Keeps each country and band's highest-ranked `limit` seeds, and every capital.
255
+ */
256
+ export function limitSeedsPerBand(seeds: readonly ShapeSeed[], limit: number): ShapeSeed[] {
257
+ return [...Map.groupBy(seeds, (seed) => `${seed.country}\u0000${seed.band}`).values()].flatMap((group) =>
258
+ group
259
+ .toSorted((a, b) => (a.rank ?? Infinity) - (b.rank ?? Infinity))
260
+ .filter((seed, index) => index < limit || seed.capital)
261
+ )
262
+ }
263
+
264
+ /**
265
+ * Groups seeds into requests of at most `size` places, each of one country,
266
+ * so a request lists its country's surfaces once.
267
+ */
268
+ export function chunkSeedsByCountry(seeds: readonly ShapeSeed[], size: number): ShapeSeed[][] {
269
+ const chunks: ShapeSeed[][] = []
270
+ const byCountry = Map.groupBy(seeds, (seed) => seed.country)
271
+
272
+ for (const country of [...byCountry.keys()].toSorted()) {
273
+ const group = byCountry.get(country)!
274
+
275
+ for (let start = 0; start < group.length; start += size) {
276
+ chunks.push(group.slice(start, start + size))
277
+ }
278
+ }
279
+
280
+ return chunks
281
+ }