@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,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import * as fs from "node:fs/promises"
|
|
8
|
+
|
|
9
|
+
import FastGlob from "fast-glob"
|
|
10
|
+
import { PathBuilder } from "path-ts"
|
|
11
|
+
|
|
12
|
+
import { takeInParallel } from "../../collections.ts"
|
|
13
|
+
import { prepareRepositoryDirectories, type RepositorySource } from "../../git.ts"
|
|
14
|
+
import {
|
|
15
|
+
normalizePlacetypeDefinition,
|
|
16
|
+
type PlacetypeDefinition,
|
|
17
|
+
type PlacetypeDefinitionInit,
|
|
18
|
+
type PlacetypeRole,
|
|
19
|
+
PlacetypeRoleOrder,
|
|
20
|
+
} from "./definition.ts"
|
|
21
|
+
|
|
22
|
+
export const PLACETYPES_REPO_SOURCE = {
|
|
23
|
+
url: "https://github.com/whosonfirst/whosonfirst-placetypes.git",
|
|
24
|
+
owner: "whosonfirst",
|
|
25
|
+
name: "whosonfirst-placetypes",
|
|
26
|
+
} as const satisfies RepositorySource
|
|
27
|
+
|
|
28
|
+
interface PlacetypeServiceInit {
|
|
29
|
+
batchSize: number
|
|
30
|
+
localRepoDirectory: PathBuilder
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class Placetype implements Disposable {
|
|
34
|
+
public [Symbol.dispose]() {
|
|
35
|
+
this.parentNames.clear()
|
|
36
|
+
this.parents.clear()
|
|
37
|
+
this.siblings.clear()
|
|
38
|
+
this.children.clear()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
//#region Static Caches
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A map of placetypes indexed by their Brooklyn Integers ID.
|
|
45
|
+
*
|
|
46
|
+
* Note that these IDs are not in any specific order. Avoid using them for sorting.
|
|
47
|
+
*/
|
|
48
|
+
static #byID = new Map<number, Placetype>()
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A map of placetypes indexed by their name.
|
|
52
|
+
*/
|
|
53
|
+
static #byName = new Map<string, Placetype>()
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A map of placetype children indexed by their parent's name.
|
|
57
|
+
*/
|
|
58
|
+
static #childrenOfParentName = new Map<string, Set<Placetype>>()
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* A map of placetype children names indexed by their parent's name.
|
|
62
|
+
*/
|
|
63
|
+
static #childNamesOfParentName = new Map<string, Set<string>>()
|
|
64
|
+
|
|
65
|
+
//#endregion
|
|
66
|
+
|
|
67
|
+
//#region Static Methods
|
|
68
|
+
|
|
69
|
+
static async prepare({ batchSize, localRepoDirectory }: PlacetypeServiceInit) {
|
|
70
|
+
const { repoDirectory, exists } = await prepareRepositoryDirectories(PLACETYPES_REPO_SOURCE, localRepoDirectory)
|
|
71
|
+
|
|
72
|
+
if (!exists) return
|
|
73
|
+
|
|
74
|
+
const definitionPaths = FastGlob.stream(["*.json"], {
|
|
75
|
+
cwd: repoDirectory("placetypes").toString(),
|
|
76
|
+
absolute: true,
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const batchIterator = takeInParallel(definitionPaths, batchSize, async (definitionPath) => {
|
|
80
|
+
const definitionContent = await fs.readFile(definitionPath, "utf8")
|
|
81
|
+
const definition: PlacetypeDefinition = JSON.parse(definitionContent)
|
|
82
|
+
|
|
83
|
+
Placetype.register(definition)
|
|
84
|
+
|
|
85
|
+
return definition
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
await Array.fromAsync(batchIterator)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Find a placetype by its name or ID.
|
|
93
|
+
*/
|
|
94
|
+
static find = (idOrName: number | string): Placetype | null => {
|
|
95
|
+
if (typeof idOrName === "string") {
|
|
96
|
+
return Placetype.#byName.get(idOrName) || null
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return Placetype.#byID.get(idOrName) || null
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Find a placetype by its name or ID.
|
|
104
|
+
*
|
|
105
|
+
* @throws {Error} If no placetype is found with the given name.
|
|
106
|
+
*/
|
|
107
|
+
static findOrThrow = (idOrName: number | string): Placetype => {
|
|
108
|
+
const placetype = Placetype.find(idOrName)
|
|
109
|
+
|
|
110
|
+
if (!placetype) {
|
|
111
|
+
throw new Error(`No placetype found with id ${idOrName}`)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return placetype
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Compare two placetypes, sorting less specific (i.e. bigger or higher) placetypes first.
|
|
119
|
+
*
|
|
120
|
+
* Note that the two placetype IDs have no affect on the comparison as they are not sequential, only unique.
|
|
121
|
+
*/
|
|
122
|
+
static comparatorAsc(a: Placetype, b: Placetype): number {
|
|
123
|
+
if (a.siblings.has(b)) {
|
|
124
|
+
return PlacetypeRoleOrder[a.role] - PlacetypeRoleOrder[b.role]
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (a.children.has(b)) return -1
|
|
128
|
+
|
|
129
|
+
if (b.children.has(a)) return 1
|
|
130
|
+
|
|
131
|
+
return 0
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Compare two placetypes, sorting more specific (i.e. smaller or lower) placetypes first.
|
|
136
|
+
*
|
|
137
|
+
* Note that the two placetype IDs have no affect on the comparison as they are not sequential, only unique.
|
|
138
|
+
*/
|
|
139
|
+
static comparatorDesc(a: Placetype, b: Placetype): number {
|
|
140
|
+
if (a.siblings.has(b)) {
|
|
141
|
+
return PlacetypeRoleOrder[b.role] - PlacetypeRoleOrder[a.role]
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (a.children.has(b)) return 1
|
|
145
|
+
|
|
146
|
+
if (b.children.has(a)) return -1
|
|
147
|
+
|
|
148
|
+
return 0
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* The source definition of this placetype.
|
|
155
|
+
*/
|
|
156
|
+
readonly #definition: PlacetypeDefinition
|
|
157
|
+
|
|
158
|
+
//#region Properties
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* The Brooklyn Integers ID of this placetype.
|
|
162
|
+
*
|
|
163
|
+
* This is effectively a unique identifier for the placetype, but any pattern of sequential integers is coincidental.
|
|
164
|
+
*/
|
|
165
|
+
public get id(): number {
|
|
166
|
+
return this.#definition.id
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The name of this placetype, e.g. "country", "city", "neighborhood"
|
|
171
|
+
*/
|
|
172
|
+
public get name(): string {
|
|
173
|
+
return this.#definition.name
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The role of this placetype, indicating its level of requirement to represent a full hierarchy.
|
|
178
|
+
*
|
|
179
|
+
* Some placetypes only exist to provide a supplementary role and are not required for a full hierarchy.
|
|
180
|
+
*/
|
|
181
|
+
public get role(): PlacetypeRole {
|
|
182
|
+
return this.#definition.role
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* The parent names of this placetype, i.e. those that this Placetype is a child of.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* Continent is a parent of "country"
|
|
190
|
+
*/
|
|
191
|
+
public parentNames: Set<string>
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The parents of this Placetype, i.e. those with this Placetype as a child.
|
|
195
|
+
*/
|
|
196
|
+
public parents: Set<Placetype>
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Sibling Placetypes of this Placetype, i.e. those with the same parent.
|
|
200
|
+
*
|
|
201
|
+
* Note that this is **all** siblings across **all** parents.
|
|
202
|
+
*/
|
|
203
|
+
public siblings: Set<Placetype>
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Children of this Placetype, i.e. those with **this Placetype as a parent**.
|
|
207
|
+
*/
|
|
208
|
+
public children: Set<Placetype>
|
|
209
|
+
|
|
210
|
+
//#endregion
|
|
211
|
+
|
|
212
|
+
//#region Predicates
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Check if this placetype has a child matching the given name, ID, or Placetype instance.
|
|
216
|
+
*/
|
|
217
|
+
public hasChild(childLike: Placetype | string | number): boolean {
|
|
218
|
+
const children = Placetype.#childrenOfParentName.get(this.name)
|
|
219
|
+
|
|
220
|
+
if (!children) return false
|
|
221
|
+
|
|
222
|
+
if (childLike instanceof Placetype) {
|
|
223
|
+
return children.has(childLike)
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const child = Placetype.findOrThrow(childLike)
|
|
227
|
+
|
|
228
|
+
return children.has(child)
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
//#endregion
|
|
232
|
+
|
|
233
|
+
//#region Lineage Methods
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Find the ancestors of this placetype, optionally filtered by role.
|
|
237
|
+
*
|
|
238
|
+
* An ancestor is a parent, grandparent, great-grandparent, etc. of this placetype.
|
|
239
|
+
*/
|
|
240
|
+
public findAncestors(roles?: Iterable<PlacetypeRole> | null): Placetype[] {
|
|
241
|
+
const ancestorsContext = new Set<Placetype>()
|
|
242
|
+
const roleFilters = roles ? new Set(roles) : null
|
|
243
|
+
|
|
244
|
+
this.#collectAncestors(ancestorsContext, roleFilters)
|
|
245
|
+
|
|
246
|
+
return Array.from(ancestorsContext).sort(Placetype.comparatorAsc)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
#collectAncestors(ancestorsContext: Set<Placetype>, roleFilters?: Set<PlacetypeRole> | null): void {
|
|
250
|
+
for (const parent of this.parents) {
|
|
251
|
+
if (roleFilters && !roleFilters.has(parent.role)) continue
|
|
252
|
+
|
|
253
|
+
ancestorsContext.add(parent)
|
|
254
|
+
|
|
255
|
+
parent.#collectAncestors(ancestorsContext, roleFilters)
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Find the children of this placetype, optionally filtered by role.
|
|
261
|
+
*
|
|
262
|
+
* @see {@linkcode children} for children as a Set.
|
|
263
|
+
*/
|
|
264
|
+
public findChildren(roles?: Iterable<PlacetypeRole> | null): Placetype[] {
|
|
265
|
+
const roleFilters = roles ? new Set(roles) : null
|
|
266
|
+
const validChildren: Placetype[] = []
|
|
267
|
+
|
|
268
|
+
for (const child of this.children) {
|
|
269
|
+
if (roleFilters && !roleFilters.has(child.role)) continue
|
|
270
|
+
|
|
271
|
+
validChildren.push(child)
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return validChildren.sort(Placetype.comparatorAsc)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Find the parents of this placetype, optionally filtered by role.
|
|
279
|
+
*
|
|
280
|
+
* @see {@linkcode parents} for parents as a Set.
|
|
281
|
+
*/
|
|
282
|
+
public findParents(roles?: Iterable<PlacetypeRole> | null): Placetype[] {
|
|
283
|
+
const roleFilters = roles ? new Set(roles) : null
|
|
284
|
+
const validParents: Placetype[] = []
|
|
285
|
+
|
|
286
|
+
for (const parent of this.parents) {
|
|
287
|
+
if (roleFilters && !roleFilters.has(parent.role)) continue
|
|
288
|
+
|
|
289
|
+
validParents.push(parent)
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return validParents.sort(Placetype.comparatorDesc)
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Find the siblings of this placetype, optionally filtered by role.
|
|
297
|
+
*/
|
|
298
|
+
public findSiblings(roles?: Iterable<PlacetypeRole> | null): Placetype[] {
|
|
299
|
+
const roleFilters = roles ? new Set(roles) : null
|
|
300
|
+
|
|
301
|
+
const validSiblings = Array.from(this.siblings)
|
|
302
|
+
|
|
303
|
+
if (roleFilters) {
|
|
304
|
+
return validSiblings.filter((sibling) => roleFilters.has(sibling.role))
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return validSiblings.sort((a, b) => {
|
|
308
|
+
return PlacetypeRoleOrder[a.role] - PlacetypeRoleOrder[b.role]
|
|
309
|
+
})
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Find the descendants of this placetype, optionally filtered by role.
|
|
314
|
+
*
|
|
315
|
+
* A descendant is a child, grandchild, great-grandchild, etc. of this placetype.
|
|
316
|
+
*/
|
|
317
|
+
public findDescendants(roles?: Iterable<PlacetypeRole> | null): Placetype[] {
|
|
318
|
+
const descendantsContext = new Set<Placetype>()
|
|
319
|
+
const roleFilters = roles ? new Set(roles) : null
|
|
320
|
+
|
|
321
|
+
this.#collectDescendants(descendantsContext, roleFilters)
|
|
322
|
+
|
|
323
|
+
return Array.from(descendantsContext).sort(Placetype.comparatorAsc)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
#collectDescendants(descendantsContext: Set<Placetype>, roleFilters?: Set<PlacetypeRole> | null): void {
|
|
327
|
+
for (const child of this.children) {
|
|
328
|
+
if (roleFilters && !roleFilters.has(child.role)) continue
|
|
329
|
+
|
|
330
|
+
descendantsContext.add(child)
|
|
331
|
+
|
|
332
|
+
child.#collectDescendants(descendantsContext, roleFilters)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
//#endregion
|
|
337
|
+
|
|
338
|
+
//#region Constructors
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Register a placetype specification, creating a new Placetype instance and indexing it by id.
|
|
342
|
+
*/
|
|
343
|
+
static register(initLike: PlacetypeDefinition | PlacetypeDefinitionInit): Placetype {
|
|
344
|
+
const definition = normalizePlacetypeDefinition(initLike)
|
|
345
|
+
const placetype = new Placetype(definition)
|
|
346
|
+
|
|
347
|
+
return placetype
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Create a new Placetype instance from a PlacetypeDefinition
|
|
352
|
+
*
|
|
353
|
+
* @see {@linkcode Placetype.register}
|
|
354
|
+
*/
|
|
355
|
+
protected constructor(definition: PlacetypeDefinition) {
|
|
356
|
+
this.#definition = definition
|
|
357
|
+
this.parentNames = new Set(definition.parent)
|
|
358
|
+
this.parents = new Set()
|
|
359
|
+
this.siblings = new Set()
|
|
360
|
+
|
|
361
|
+
let children = Placetype.#childrenOfParentName.get(definition.name)
|
|
362
|
+
|
|
363
|
+
if (!children) {
|
|
364
|
+
children = new Set()
|
|
365
|
+
Placetype.#childrenOfParentName.set(definition.name, children)
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
this.children = children
|
|
369
|
+
|
|
370
|
+
// Constructor level indexes...
|
|
371
|
+
Placetype.#byID.set(definition.id, this)
|
|
372
|
+
Placetype.#byName.set(definition.name, this)
|
|
373
|
+
|
|
374
|
+
// Next, we need to prepare the parent/child relationships...
|
|
375
|
+
for (const parentName of definition.parent || []) {
|
|
376
|
+
// Create the children set if it doesn't exist...
|
|
377
|
+
let directSiblings = Placetype.#childrenOfParentName.get(parentName)
|
|
378
|
+
|
|
379
|
+
if (!directSiblings) {
|
|
380
|
+
directSiblings = new Set<Placetype>()
|
|
381
|
+
|
|
382
|
+
Placetype.#childrenOfParentName.set(parentName, directSiblings)
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// And add the placetype to the parent's children...
|
|
386
|
+
directSiblings.add(this)
|
|
387
|
+
|
|
388
|
+
// Once more, for the child names...
|
|
389
|
+
let childNames = Placetype.#childNamesOfParentName.get(parentName)
|
|
390
|
+
|
|
391
|
+
if (!childNames) {
|
|
392
|
+
childNames = new Set<string>()
|
|
393
|
+
|
|
394
|
+
Placetype.#childNamesOfParentName.set(parentName, childNames)
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// Adding the name to the parent's child names...
|
|
398
|
+
childNames.add(definition.name)
|
|
399
|
+
|
|
400
|
+
// It's possible that this parent has already been registered...
|
|
401
|
+
const parent = Placetype.find(parentName)
|
|
402
|
+
|
|
403
|
+
if (parent) {
|
|
404
|
+
this.parents.add(parent)
|
|
405
|
+
|
|
406
|
+
const siblings = Placetype.#childrenOfParentName.get(parentName)
|
|
407
|
+
|
|
408
|
+
for (const sibling of siblings || []) {
|
|
409
|
+
if (sibling === this) continue
|
|
410
|
+
|
|
411
|
+
this.siblings.add(sibling)
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Finally, there may be children who have registered before this placetype.
|
|
417
|
+
// We need to adopt them into our parentage...
|
|
418
|
+
const orphans = Placetype.#childrenOfParentName.get(definition.name)
|
|
419
|
+
|
|
420
|
+
for (const child of orphans || []) {
|
|
421
|
+
child.parents.add(this)
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
//#endregion
|
|
426
|
+
|
|
427
|
+
//#region Methods
|
|
428
|
+
|
|
429
|
+
public toString() {
|
|
430
|
+
const { name, id, role } = this.#definition
|
|
431
|
+
|
|
432
|
+
return `Placetype[${name}][${id}][${role}]`
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
public [Symbol.for("nodejs.util.inspect.custom")]() {
|
|
436
|
+
return this.toString()
|
|
437
|
+
}
|
|
438
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
Alpha2LanguageCode,
|
|
9
|
+
type Alpha3bLanguageCode,
|
|
10
|
+
Alpha3bToAlpha2,
|
|
11
|
+
isAlpha2LanguageCode,
|
|
12
|
+
isAlpha3bLanguageCode,
|
|
13
|
+
} from "@mailwoman/core/resources/languages"
|
|
14
|
+
import type { Feature, Geometry } from "geojson"
|
|
15
|
+
|
|
16
|
+
import type { WhosOnFirstPlacetype } from "./definition.ts"
|
|
17
|
+
|
|
18
|
+
export interface WOFBaseProperties {
|
|
19
|
+
"wof:id": number
|
|
20
|
+
"wof:name": string
|
|
21
|
+
"wof:src": string
|
|
22
|
+
"src:geom": string
|
|
23
|
+
"wof:placetype": WhosOnFirstPlacetype
|
|
24
|
+
"wof:parent_id": number
|
|
25
|
+
"wof:superseded_by"?: number[]
|
|
26
|
+
"wof:supersedes"?: number[]
|
|
27
|
+
"wof:country"?: string
|
|
28
|
+
"wof:concordances"?: Record<string, string | number>
|
|
29
|
+
"wof:population"?: number
|
|
30
|
+
"wof:lastmodified"?: number
|
|
31
|
+
"geom:latitude"?: number
|
|
32
|
+
"geom:longitude"?: number
|
|
33
|
+
"gn:population"?: number
|
|
34
|
+
"mz:is_current"?: number | string
|
|
35
|
+
"edtf:deprecated"?: string
|
|
36
|
+
"edtf:cessation"?: string
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const WOFNameKinds = ["preferred", "variant", "colloquial", "abbr", "short"] as const
|
|
40
|
+
export type WOFNameKind = (typeof WOFNameKinds)[number]
|
|
41
|
+
export type LanguageSpecificKey = `name:${Alpha3bLanguageCode}_x_${WOFNameKind}`
|
|
42
|
+
|
|
43
|
+
export type WOFLanguageProperties = {
|
|
44
|
+
[key in LanguageSpecificKey]?: string | string[]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type WOFProperties = WOFBaseProperties & WOFLanguageProperties
|
|
48
|
+
|
|
49
|
+
export type WOFFeature = Feature<Geometry, WOFProperties>
|
|
50
|
+
|
|
51
|
+
type LocalizedPlacetypePropMap = Map<WOFNameKind, string>
|
|
52
|
+
|
|
53
|
+
export interface ParsedWOFPlacetype {
|
|
54
|
+
id: number
|
|
55
|
+
parent_id: number
|
|
56
|
+
name: string
|
|
57
|
+
src: string
|
|
58
|
+
placetype: WhosOnFirstPlacetype
|
|
59
|
+
localizedPropMap: Map<Alpha3bLanguageCode, LocalizedPlacetypePropMap>
|
|
60
|
+
country?: string
|
|
61
|
+
latitude?: number
|
|
62
|
+
longitude?: number
|
|
63
|
+
population?: number
|
|
64
|
+
concordances?: Record<string, string | number>
|
|
65
|
+
isCurrent?: boolean
|
|
66
|
+
isDeprecated?: boolean
|
|
67
|
+
isCeased?: boolean
|
|
68
|
+
isSuperseded?: boolean
|
|
69
|
+
isSuperseding?: boolean
|
|
70
|
+
lastmodified?: number
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const langPattern = new RegExp(`:([a-z0-9\\-_]{2,3})_x_(${WOFNameKinds.join("|")})`)
|
|
74
|
+
|
|
75
|
+
export function parsePlacetypeSource(key: string) {
|
|
76
|
+
const [, languageCode, nameKind] = (langPattern.exec(key as string) ?? []) as Partial<
|
|
77
|
+
[string, Alpha3bLanguageCode, WOFNameKind]
|
|
78
|
+
>
|
|
79
|
+
|
|
80
|
+
return { languageCode, nameKind }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function pluckPlacetypeSpec({
|
|
84
|
+
"wof:id": id,
|
|
85
|
+
"wof:name": name,
|
|
86
|
+
"wof:src": wof_src,
|
|
87
|
+
"src:geom": geom_src,
|
|
88
|
+
"wof:parent_id": parent_id,
|
|
89
|
+
"wof:placetype": placetype,
|
|
90
|
+
"wof:superseded_by": superseded_by,
|
|
91
|
+
"wof:supersedes": supersedes,
|
|
92
|
+
"wof:country": country,
|
|
93
|
+
"wof:concordances": concordances,
|
|
94
|
+
"wof:population": wofPopulation,
|
|
95
|
+
"wof:lastmodified": lastmodified,
|
|
96
|
+
"geom:latitude": latitude,
|
|
97
|
+
"geom:longitude": longitude,
|
|
98
|
+
"gn:population": gnPopulation,
|
|
99
|
+
"mz:is_current": mzIsCurrent,
|
|
100
|
+
"edtf:deprecated": edtfDeprecated,
|
|
101
|
+
"edtf:cessation": edtfCessation,
|
|
102
|
+
...props
|
|
103
|
+
}: WOFProperties): ParsedWOFPlacetype {
|
|
104
|
+
const localizedPropMap = new Map<Alpha3bLanguageCode, LocalizedPlacetypePropMap>()
|
|
105
|
+
|
|
106
|
+
for (const [key, value] of Object.entries(props)) {
|
|
107
|
+
if (!value) continue
|
|
108
|
+
|
|
109
|
+
if (!value || typeof value === "number") continue
|
|
110
|
+
|
|
111
|
+
const { languageCode, nameKind } = parsePlacetypeSource(key)
|
|
112
|
+
|
|
113
|
+
if (!languageCode || !nameKind) continue
|
|
114
|
+
|
|
115
|
+
let langMap = localizedPropMap.get(languageCode)
|
|
116
|
+
|
|
117
|
+
if (!langMap) {
|
|
118
|
+
langMap = new Map()
|
|
119
|
+
localizedPropMap.set(languageCode, langMap)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const firstValue = Array.isArray(value) ? value[0] : value
|
|
123
|
+
|
|
124
|
+
if (firstValue) {
|
|
125
|
+
langMap.set(nameKind, firstValue)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const src = wof_src || geom_src || "unknown"
|
|
130
|
+
|
|
131
|
+
if (!src) throw new Error(`No source found for placetype ${placetype} with ID ${id}`)
|
|
132
|
+
|
|
133
|
+
const population =
|
|
134
|
+
typeof wofPopulation === "number" ? wofPopulation : typeof gnPopulation === "number" ? gnPopulation : undefined
|
|
135
|
+
const isCurrent = mzIsCurrent === undefined ? undefined : mzIsCurrent !== 0 && mzIsCurrent !== "0"
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
id,
|
|
139
|
+
name,
|
|
140
|
+
src,
|
|
141
|
+
parent_id,
|
|
142
|
+
placetype,
|
|
143
|
+
localizedPropMap,
|
|
144
|
+
country: country || undefined,
|
|
145
|
+
latitude: typeof latitude === "number" ? latitude : undefined,
|
|
146
|
+
longitude: typeof longitude === "number" ? longitude : undefined,
|
|
147
|
+
population,
|
|
148
|
+
concordances: concordances && Object.keys(concordances).length > 0 ? concordances : undefined,
|
|
149
|
+
isCurrent,
|
|
150
|
+
isDeprecated: !!edtfDeprecated,
|
|
151
|
+
isCeased: !!edtfCessation,
|
|
152
|
+
isSuperseded: !!(superseded_by && superseded_by.length > 0),
|
|
153
|
+
isSuperseding: !!(supersedes && supersedes.length > 0),
|
|
154
|
+
lastmodified: typeof lastmodified === "number" ? lastmodified : undefined,
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Given a WhosOnFirst filename, plucks and normalizes the language code into an ISO 639-1 alpha-2 code.
|
|
160
|
+
*
|
|
161
|
+
* @deprecated
|
|
162
|
+
*/
|
|
163
|
+
export function pluckFileNameLanguageCode(filename: string): Alpha2LanguageCode | null {
|
|
164
|
+
const [, languageCode] = filename.match(/name:([a-z]{2,3})_\w+\.txt/) || []
|
|
165
|
+
|
|
166
|
+
if (!languageCode) return null
|
|
167
|
+
|
|
168
|
+
if (isAlpha2LanguageCode(languageCode)) return languageCode
|
|
169
|
+
|
|
170
|
+
if (isAlpha3bLanguageCode(languageCode)) return Alpha3bToAlpha2.get(languageCode) ?? null
|
|
171
|
+
|
|
172
|
+
return null
|
|
173
|
+
}
|