mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 14:17:31 +00:00
fix(atlas): count the cities the address parser used to miss
The Atlas reads a place's city from its address text, and several common
address shapes lost it:
- The TREK index writes "street, postcode, locality" with no country, and the
parser always dropped the last part as the country, so a place picked from
TREK's own search counted no city.
- A city that is its own region (Berlin, Wien, Malé) was skipped as the region
even where it stands beside its postcode, which is how such an address names
its city.
- An atoll or a state written with its generic word ("Kaafu Atoll" for the
region Kaafu) was not recognised as the region and counted as the city of
every island in it.
- Country names came from a short English and German list, so "Maldives",
"Österreich" or "Italia" could be counted as a city.
The locality of a postcode-and-locality tail now counts, a region-named part
with a postcode counts when no part before it names a city, a region matches
with its generic word, and countries are recognised by their name in every
language TREK speaks. A region named without a postcode (a mountain in
"Bayern", a resort in "Kaafu") still counts no city, and the lines the parser
already read right read the same.
Closes #3200
This commit is contained in:
@@ -34,7 +34,7 @@ import {
|
||||
reverseGeocodeRegion,
|
||||
} from './atlas-geo';
|
||||
import type { RegionInfo } from './atlas-geo';
|
||||
import { KNOWN_COUNTRIES } from './known-countries';
|
||||
import { isCountryName } from './known-countries';
|
||||
import { cityFromAddress } from './city-from-address';
|
||||
import { transferEndpointIds } from './transfer-endpoints';
|
||||
import { countryVisitDates } from './visit-dates';
|
||||
@@ -1046,7 +1046,7 @@ export class AtlasService {
|
||||
// Explicit null checks: lat/lng of exactly 0 (equator / prime meridian)
|
||||
// are valid coordinates a falsy check would drop.
|
||||
if (p.lat != null && p.lng != null) coords.push({ lat: p.lat, lng: p.lng });
|
||||
const city = cityFromAddress(p.address, (part) => KNOWN_COUNTRIES.has(part), p.region_name);
|
||||
const city = cityFromAddress(p.address, isCountryName, p.region_name);
|
||||
if (city && !cities.has(city.toLowerCase())) cities.set(city.toLowerCase(), city);
|
||||
}
|
||||
return { totalPlaces: places.length, cities: [...cities.values()], coords };
|
||||
|
||||
@@ -60,6 +60,25 @@ function normalize(value: string): string {
|
||||
return value.trim().toLowerCase();
|
||||
}
|
||||
|
||||
/** A part that is a postcode and nothing else ("10117", "150-0002"), as the TREK index writes it. */
|
||||
function isBarePostcode(part: string): boolean {
|
||||
let rest = part;
|
||||
for (const pattern of POSTCODES) rest = rest.replace(pattern, ' ');
|
||||
return rest !== part && !/[\p{L}\d]/u.test(rest);
|
||||
}
|
||||
|
||||
/**
|
||||
* The generic word a geocoder writes after a region's own name: "Kaafu Atoll" for
|
||||
* the region the map calls Kaafu. Without it the atoll was read as the city, and
|
||||
* every island in it counted as one (#3200).
|
||||
*/
|
||||
const REGION_WORD = /\s+(?:atoll|province|region|state|prefecture|county|governorate|oblast)$/i;
|
||||
|
||||
function sameRegion(part: string, region: string): boolean {
|
||||
const name = normalize(part);
|
||||
return name === region || name.replace(REGION_WORD, '') === region;
|
||||
}
|
||||
|
||||
export function cityFromAddress(
|
||||
address: string | null | undefined,
|
||||
isKnownCountry: (part: string) => boolean,
|
||||
@@ -76,21 +95,43 @@ export function cityFromAddress(
|
||||
|
||||
const region = regionName ? normalize(regionName) : null;
|
||||
|
||||
// The TREK index writes "street, postcode, locality" with no country: the locality
|
||||
// is the city, whatever the region is called (#3200).
|
||||
const tail = parts[parts.length - 1] as string;
|
||||
if (
|
||||
parts.length >= 3 &&
|
||||
isBarePostcode(parts[parts.length - 2] as string) &&
|
||||
!isKnownCountry(tail) &&
|
||||
!hasDigits(tail) &&
|
||||
/\p{L}/u.test(tail)
|
||||
) {
|
||||
return tail;
|
||||
}
|
||||
|
||||
// A geocoder always ends on the country, so drop the last part outright once the
|
||||
// address is long enough for that to be certain. The country-name list below still
|
||||
// runs for every part, but it is a finite list (it has no "Nederland", for one), and
|
||||
// this way a country it has never heard of cannot be counted as a city.
|
||||
const last = parts.length >= 3 ? parts.length - 2 : parts.length - 1;
|
||||
|
||||
// A city that is its own region (Berlin, Wien, Malé) is skipped like any region,
|
||||
// but written beside its postcode ("10117 Berlin") it is the city of the address.
|
||||
// It counts when no part before it names a city (#3200).
|
||||
let cityRegion: string | null = null;
|
||||
|
||||
// Index 0 stays out of the running: geocoders lead with the place itself.
|
||||
for (let i = last; i >= 1; i--) {
|
||||
const part = hasDigits(parts[i]) ? besidePostcode(parts[i]) : parts[i];
|
||||
const withPostcode = hasDigits(parts[i]);
|
||||
const part = withPostcode ? besidePostcode(parts[i]) : parts[i];
|
||||
if (!part) continue;
|
||||
if (isKnownCountry(part)) continue;
|
||||
if (region && normalize(part) === region) continue;
|
||||
if (region && sameRegion(part, region)) {
|
||||
if (withPostcode) cityRegion ??= part;
|
||||
continue;
|
||||
}
|
||||
// Anything with no letter at all (stray punctuation, a lone dash) is not a name.
|
||||
if (!/\p{L}/u.test(part)) continue;
|
||||
return part;
|
||||
}
|
||||
return null;
|
||||
return cityRegion;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { SUPPORTED_LANGUAGES } from '@trek/shared';
|
||||
|
||||
/**
|
||||
* Country names, English and German, used to tell a city apart from a country
|
||||
* when splitting a free-text place address. Moved here from auth.helpers.ts
|
||||
@@ -6,7 +8,7 @@
|
||||
* Deliberately not ISO codes: it matches against whatever the geocoder wrote
|
||||
* into places.address, which is display text in the user's language.
|
||||
*/
|
||||
export const KNOWN_COUNTRIES = new Set([
|
||||
const KNOWN_COUNTRIES = new Set([
|
||||
// Nominatim inserts an extra tier above the country for France and its overseas
|
||||
// territories, which reads exactly like a place name and would otherwise be counted
|
||||
// as a city.
|
||||
@@ -186,3 +188,34 @@ export const KNOWN_COUNTRIES = new Set([
|
||||
'Belarus',
|
||||
'Weissrussland',
|
||||
]);
|
||||
|
||||
/**
|
||||
* Every country's name in every language TREK speaks, as the platform's ICU writes
|
||||
* it, lower-cased. The list above is English and German only, so an address ending
|
||||
* in "Maldives", "Österreich" or "Italia" counted the country as its city (#3200).
|
||||
* The TREK index writes its country with the same Intl names.
|
||||
*/
|
||||
const COUNTRY_NAMES: ReadonlySet<string> = (() => {
|
||||
const names = new Set<string>();
|
||||
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
for (const { locale } of SUPPORTED_LANGUAGES) {
|
||||
let display: Intl.DisplayNames;
|
||||
try {
|
||||
display = new Intl.DisplayNames([locale], { type: 'region', fallback: 'none' });
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
for (const a of letters) {
|
||||
for (const b of letters) {
|
||||
const name = display.of(a + b);
|
||||
if (name) names.add(name.normalize('NFC').toLowerCase());
|
||||
}
|
||||
}
|
||||
}
|
||||
return names;
|
||||
})();
|
||||
|
||||
/** Whether a part of an address names a country, in any language TREK speaks. */
|
||||
export function isCountryName(part: string): boolean {
|
||||
return KNOWN_COUNTRIES.has(part) || COUNTRY_NAMES.has(part.trim().normalize('NFC').toLowerCase());
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
* invented examples and failed against actual data.
|
||||
*/
|
||||
import { cityFromAddress } from '../../../src/nest/atlas/city-from-address';
|
||||
import { isCountryName } from '../../../src/nest/atlas/known-countries';
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
@@ -79,3 +80,71 @@ describe('cityFromAddress with the postcode in the same part as the city (#3137)
|
||||
expect(city('CN Tower, Toronto, ON M5V 3L9, Canada')).toBe('Toronto');
|
||||
});
|
||||
});
|
||||
|
||||
// #3200: a Maldives traveller saw more regions than cities. Each of these addresses
|
||||
// lost its city, or counted something that is none, before.
|
||||
describe('cityFromAddress for the cities the count used to miss (#3200)', () => {
|
||||
const real = (address: string, region?: string | null) => cityFromAddress(address, isCountryName, region);
|
||||
|
||||
it('reads the locality of the line the TREK index writes, which ends without a country', () => {
|
||||
expect(city('Pariser Platz 1, 10117, Berlin', 'Berlin')).toBe('Berlin');
|
||||
expect(city('Boduthakurufaanu Magu, 20026, Malé', 'Malé')).toBe('Malé');
|
||||
expect(city('Rue de Rivoli 99, 75001, Paris', 'Île-de-France')).toBe('Paris');
|
||||
});
|
||||
|
||||
it('counts a city that is its own region when its postcode stands beside it', () => {
|
||||
expect(city('Pariser Platz, 10117 Berlin, Germany', 'Berlin')).toBe('Berlin');
|
||||
expect(city('Hotel Adlon Kempinski, Unter den Linden 77, 10117 Berlin, Germany', 'Berlin')).toBe('Berlin');
|
||||
expect(real('Michaelerkuppel, 1010 Wien, Österreich', 'Wien')).toBe('Wien');
|
||||
expect(real('Boduthakurufaanu Magu, Malé 20026, Maldives', 'Malé')).toBe('Malé');
|
||||
// A city below the region still wins, as before.
|
||||
expect(city('Shibuya Crossing, Shibuya City, Tokyo 150-0002, Japan', 'Tokyo')).toBe('Shibuya City');
|
||||
});
|
||||
|
||||
it('never counts a region named without a postcode, so a mountain or an atoll is no city', () => {
|
||||
expect(real('Zugspitze, Bayern, Deutschland', 'Bayern')).toBeNull();
|
||||
expect(real('Resort, Kaafu, Maldives', 'Kaafu')).toBeNull();
|
||||
});
|
||||
|
||||
it('reads the island below an atoll the geocoder writes with its generic word', () => {
|
||||
expect(real('Kurumba Maldives, Vihamanaafushi, Kaafu Atoll, Maldives', 'Kaafu')).toBe('Vihamanaafushi');
|
||||
expect(real('Maafushi Beach, Maafushi, Kaafu Atoll, 08090, Maldives', 'Kaafu')).toBe('Maafushi');
|
||||
});
|
||||
|
||||
it('never counts a country as the city, in any language TREK speaks', () => {
|
||||
expect(real('Malé, Maldives', 'Malé')).toBeNull();
|
||||
expect(real('Maafushi, Maldives', 'Kaafu')).toBeNull();
|
||||
expect(real('Somewhere, 08090, Maldives')).toBeNull();
|
||||
expect(real('Piazza Navona, Roma, Italia', 'Lazio')).toBe('Roma');
|
||||
});
|
||||
|
||||
it('keeps the lines it already read right', () => {
|
||||
expect(real('350 5th Ave, New York, NY 10118, USA')).toBe('New York');
|
||||
expect(real('CN Tower, Toronto, ON M5V 3L9, Canada')).toBe('Toronto');
|
||||
expect(real('Museum, Marienplatz, München, Bayern, 80331, Germany', 'Bayern')).toBe('München');
|
||||
expect(real('Marienplatz 1, München')).toBe('München');
|
||||
});
|
||||
});
|
||||
|
||||
describe('isCountryName', () => {
|
||||
it('knows a country by its name in the languages TREK speaks, ignoring case', () => {
|
||||
for (const name of [
|
||||
'Maldives',
|
||||
'Malediven',
|
||||
'Österreich',
|
||||
'Italia',
|
||||
'Sri Lanka',
|
||||
'maldives',
|
||||
'Deutschland',
|
||||
'Japan',
|
||||
]) {
|
||||
expect(isCountryName(name)).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('does not take a city or a region for a country', () => {
|
||||
for (const name of ['Malé', 'Berlin', 'Kaafu Atoll', 'München', 'Bayern', 'New York']) {
|
||||
expect(isCountryName(name)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user