@mailwoman/variant-aliases 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.
Files changed (4) hide show
  1. package/index.ts +27 -0
  2. package/lookup.ts +105 -0
  3. package/package.json +20 -6
  4. package/types.ts +59 -0
package/index.ts ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * `@mailwoman/variant-aliases` — regional variant alias table for amenity/brand queries.
7
+ *
8
+ * The data file (`data/aliases.json`) catalogs ~35 hand-curated regional terms: "servo" → fuel
9
+ * (en-AU), "マクド" → McDonald's (ja-JP), "PFK" → KFC (fr-CA), etc.
10
+ *
11
+ * See:
12
+ *
13
+ * - `docs/articles/understanding/exotic-poi/regional-variant-queries.md` for the linguistic
14
+ * background and the source tables this data is derived from.
15
+ * - Issue #166 for the v0.6.0+ runtime integration plan (kind classifier consumes this table to emit
16
+ * `kind=amenity` / `kind=brand` proposals, gated by locale-gate output).
17
+ */
18
+
19
+ export { VARIANT_ALIAS_VERSION, getAllAliases, lookupVariantAliases } from "./lookup.ts"
20
+ export type {
21
+ AliasLookupResult,
22
+ AmenityAlias,
23
+ BrandAlias,
24
+ VariantAlias,
25
+ VariantAliasTable,
26
+ VariantKind,
27
+ } from "./types.ts"
package/lookup.ts ADDED
@@ -0,0 +1,105 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Regional variant alias lookup. Given a token (or short phrase) and a detected locale, return the
7
+ * canonical amenity category or brand it refers to.
8
+ *
9
+ * This is the data-side foundation for #166 (variant alias table + locale-gated category matching).
10
+ * The runtime integration into the kind classifier is v0.6.0+ work.
11
+ */
12
+
13
+ import { readFileSync } from "node:fs"
14
+ import { resolve } from "node:path"
15
+
16
+ import type { AliasLookupResult, VariantAlias, VariantAliasTable } from "./types.ts"
17
+
18
+ const moduleDir = import.meta.dirname
19
+
20
+ function loadTable(): VariantAliasTable {
21
+ const candidates = [
22
+ resolve(moduleDir, "data/aliases.json"),
23
+ resolve(moduleDir, "../data/aliases.json"),
24
+ resolve(moduleDir, "../../variant-aliases/data/aliases.json"),
25
+ ]
26
+
27
+ for (const path of candidates) {
28
+ try {
29
+ return JSON.parse(readFileSync(path, "utf8")) as VariantAliasTable
30
+ } catch {
31
+ // try next
32
+ }
33
+ }
34
+ throw new Error("variant-aliases: could not find data/aliases.json")
35
+ }
36
+
37
+ const TABLE = loadTable()
38
+
39
+ /**
40
+ * Indexed by lowercased variant string for O(1) lookup. Multiple entries can share the same variant key (e.g. ambiguous
41
+ * "takeaway" only matches GB but not AU if both list it differently), so each entry is an array of all aliases that
42
+ * share the key.
43
+ */
44
+ const INDEX: ReadonlyMap<string, ReadonlyArray<VariantAlias>> = (() => {
45
+ const map = new Map<string, VariantAlias[]>()
46
+
47
+ for (const a of TABLE.aliases) {
48
+ const key = a.variant.toLowerCase()
49
+ const existing = map.get(key) ?? []
50
+ existing.push(a)
51
+ map.set(key, existing)
52
+ }
53
+
54
+ return map
55
+ })()
56
+
57
+ /**
58
+ * Match a query token against the variant alias table, gated by detected locale.
59
+ *
60
+ * Confidence:
61
+ *
62
+ * - `1.0` when the detected locale (e.g. `en-AU`) is in the alias's `locales` list.
63
+ * - `0.5` when only the language part matches (e.g. detected `en-IE`, alias supports `en-AU`). This is intentionally
64
+ * weaker because regional variants are by definition regional.
65
+ * - No match when neither holds.
66
+ *
67
+ * Returns ALL matches sorted by confidence descending. Multi-locale variants (like "petrol station" →
68
+ * en-GB/en-AU/en-NZ/en-ZA) return one entry per locale list — the caller picks.
69
+ */
70
+ export function lookupVariantAliases(text: string, locale: string): AliasLookupResult[] {
71
+ const norm = text.trim().toLowerCase()
72
+
73
+ if (!norm) return []
74
+
75
+ const candidates = INDEX.get(norm)
76
+
77
+ if (!candidates || candidates.length === 0) return []
78
+
79
+ const language = locale.split(/[-_]/)[0]
80
+ const results: AliasLookupResult[] = []
81
+
82
+ for (const alias of candidates) {
83
+ if (alias.locales.includes(locale)) {
84
+ results.push({ alias, confidence: 1.0 })
85
+ continue
86
+ }
87
+ // Relaxed match: any locale in `locales` that shares the same language part.
88
+ const langMatch = alias.locales.some((l) => l.split(/[-_]/)[0] === language)
89
+
90
+ if (langMatch) {
91
+ results.push({ alias, confidence: 0.5 })
92
+ }
93
+ }
94
+
95
+ results.sort((a, b) => b.confidence - a.confidence)
96
+
97
+ return results
98
+ }
99
+
100
+ /** Pure-data accessor for callers that want to enumerate the table (e.g. corpus synthesis). */
101
+ export function getAllAliases(): ReadonlyArray<VariantAlias> {
102
+ return TABLE.aliases
103
+ }
104
+
105
+ export const VARIANT_ALIAS_VERSION = TABLE.version
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mailwoman/variant-aliases",
3
- "version": "7.2.0",
3
+ "version": "7.2.1",
4
4
  "description": "Regional variant alias table — local terms for amenities/brands gated by locale. 'servo' → fuel (en-AU), 'マクド' → McDonald's (ja-JP), 'PFK' → KFC (fr-CA).",
5
5
  "license": "AGPL-3.0-only OR LicenseRef-Commercial",
6
6
  "repository": {
@@ -13,18 +13,32 @@
13
13
  "out/**/*.js.map",
14
14
  "out/**/*.d.ts",
15
15
  "out/**/*.d.ts.map",
16
- "data/**/*.json"
16
+ "data/**/*.json",
17
+ "*.ts",
18
+ "*.tsx",
19
+ "**/*.ts",
20
+ "**/*.tsx",
21
+ "!*.test.ts",
22
+ "!*.test.tsx",
23
+ "!**/*.test.ts",
24
+ "!**/*.test.tsx"
17
25
  ],
18
26
  "type": "module",
19
27
  "exports": {
20
28
  "./package.json": "./package.json",
21
29
  ".": {
22
- "node": "./index.ts",
23
- "default": "./out/index.js",
24
- "types": "./out/index.d.ts"
30
+ "types": "./out/index.d.ts",
31
+ "default": "./out/index.js"
25
32
  }
26
33
  },
27
34
  "publishConfig": {
28
- "access": "public"
35
+ "access": "public",
36
+ "exports": {
37
+ "./package.json": "./package.json",
38
+ ".": {
39
+ "types": "./out/index.d.ts",
40
+ "default": "./out/index.js"
41
+ }
42
+ }
29
43
  }
30
44
  }
package/types.ts ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Types for the regional variant alias table.
7
+ */
8
+
9
+ /**
10
+ * The semantic kind of a variant — either a generic amenity category or a specific brand.
11
+ *
12
+ * - `amenity` aliases resolve to a category (`fuel`, `pharmacy`, `convenience`, ...). Several variants can map to the
13
+ * same category ("servo" and "petrol station" both → `fuel`).
14
+ * - `brand` aliases resolve to a canonical brand name. Multiple regional variants of the same brand ("Macca's", "McDo",
15
+ * "Mickey D's", "マクド") all map to `McDonald's`.
16
+ */
17
+ export type VariantKind = "amenity" | "brand"
18
+
19
+ export interface VariantAliasBase {
20
+ /** The user-typed variant. Always lowercase for the lookup key (CJK preserved as-is). */
21
+ variant: string
22
+ /**
23
+ * BCP-47 locale tags where this variant is in active use. Used to gate lookups: only consider an alias when the
24
+ * detected locale matches one of these. A query in `en-US` won't match Australian "servo" because `en-AU` is not in
25
+ * `["en-US"]`.
26
+ */
27
+ locales: ReadonlyArray<string>
28
+ /**
29
+ * Free-form regional refinement within the locale (e.g. "NYC", "Kansai", "Quebec"). Not used for matching today;
30
+ * informational. A future enhancement could combine this with a coarse geolocation signal to further disambiguate.
31
+ */
32
+ regionHint?: string
33
+ }
34
+
35
+ export interface AmenityAlias extends VariantAliasBase {
36
+ kind: "amenity"
37
+ /** Canonical amenity category (e.g. `fuel`, `pharmacy`, `convenience`, `alcohol`). */
38
+ category: string
39
+ }
40
+
41
+ export interface BrandAlias extends VariantAliasBase {
42
+ kind: "brand"
43
+ /** Canonical brand display name (e.g. `McDonald's`, `7-Eleven`). */
44
+ brand: string
45
+ }
46
+
47
+ export type VariantAlias = AmenityAlias | BrandAlias
48
+
49
+ export interface VariantAliasTable {
50
+ version: string
51
+ description: string
52
+ aliases: ReadonlyArray<VariantAlias>
53
+ }
54
+
55
+ export interface AliasLookupResult {
56
+ alias: VariantAlias
57
+ /** Confidence in the match. 1.0 = exact locale match. 0.5 = relaxed (locale fallback to language). */
58
+ confidence: number
59
+ }