mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 01:32:19 +00:00
Keep a country named with "and" whole, and show places as they were typed
"flights near Trinidad and Tobago" flew to Trinidad and offered Tobago on
its own; places whose own name holds "and" or "&" (Trinidad and Tobago,
Bosnia and Herzegovina, Antigua and Barbuda and the like) now stay one
place, while "Austin and Dallas" is still two. "İstanbul" was searched with
a stray combining dot, and labels title-cased what was typed with capitals
("IPhone Store"); a place is searched in plain lower case and labelled as
it was typed.
Co-authored-by: Manjunath Paparaju <[email protected]>
This commit is contained in:
1 parent
0421813e26
commit
91700f89f8
3 files changed
+121
-14
No files matched your search
+77
-3
@@ -367,6 +367,66 @@ const GO_TAIL =
|
||||
const SEPARATORS =
|
||||
/(\s*(?:,|;(?!\s*[-+]?\d)|\+(?!\d)|&|\band then\b|\bthen\b|\band\b|\balso\b)\s*)/;
|
||||
|
||||
/**
|
||||
* Places whose own name holds "and" (or "&"), kept whole after "near" or
|
||||
* "in" instead of read as two places: "flights near Trinidad and Tobago".
|
||||
*/
|
||||
const COMPOUND_PLACES = new Set([
|
||||
'trinidad and tobago',
|
||||
'antigua and barbuda',
|
||||
'bosnia and herzegovina',
|
||||
'saint kitts and nevis',
|
||||
'st kitts and nevis',
|
||||
'st. kitts and nevis',
|
||||
'saint vincent and the grenadines',
|
||||
'st vincent and the grenadines',
|
||||
'st. vincent and the grenadines',
|
||||
'sao tome and principe',
|
||||
'são tomé and príncipe',
|
||||
'turks and caicos',
|
||||
'turks and caicos islands',
|
||||
'wallis and futuna',
|
||||
'saint pierre and miquelon',
|
||||
'heard island and mcdonald islands',
|
||||
'svalbard and jan mayen',
|
||||
'south georgia and the south sandwich islands',
|
||||
'newfoundland and labrador',
|
||||
'jammu and kashmir',
|
||||
'andaman and nicobar islands',
|
||||
'dadra and nagar haveli',
|
||||
'brighton and hove',
|
||||
]);
|
||||
const isCompoundPlace = (first, second) =>
|
||||
COMPOUND_PLACES.has(`${first} and ${second}`.toLowerCase());
|
||||
|
||||
/**
|
||||
* Lower case one character at a time, so the line keeps its length and
|
||||
* "İstanbul" reads "istanbul" (not "i̇stanbul" with a combining dot).
|
||||
*/
|
||||
function lowerEach(text) {
|
||||
return Array.from(text, (char) =>
|
||||
char === 'İ' ? 'i' : char.toLowerCase().normalize('NFC'),
|
||||
).join('');
|
||||
}
|
||||
|
||||
/**
|
||||
* A place as it was typed: "İstanbul", "iPhone store", "McAllen" are kept,
|
||||
* where the line read them in lower case. A place the line put together
|
||||
* ("Trinidad and Tobago" from "Trinidad & Tobago") stays as read.
|
||||
*/
|
||||
export function asTypedIn(raw, place) {
|
||||
const lowered = lowerEach(raw).replace(/\s+/g, ' ');
|
||||
const spaced = raw.replace(/\s+/g, ' ');
|
||||
if (lowered.length !== spaced.length) return place;
|
||||
const at = lowered.indexOf(place);
|
||||
if (at < 0) return place;
|
||||
const before = lowered[at - 1];
|
||||
const after = lowered[at + place.length];
|
||||
if (/[\p{L}\p{N}]/u.test(before || '') || /[\p{L}\p{N}]/u.test(after || ''))
|
||||
return place;
|
||||
return spaced.slice(at, at + place.length);
|
||||
}
|
||||
|
||||
function tidy(text) {
|
||||
return text.replace(/\s+/g, ' ').trim();
|
||||
}
|
||||
@@ -415,7 +475,7 @@ export function parseOmnibar(
|
||||
{ isEntity = () => false } = {},
|
||||
) {
|
||||
const raw = String(text || '').trim();
|
||||
const line = tidy(raw.toLowerCase()).replace(
|
||||
const line = tidy(lowerEach(raw)).replace(
|
||||
/\b(black[- ]and[- ]white|b ?& ?w)\b/g,
|
||||
'noir',
|
||||
);
|
||||
@@ -483,6 +543,7 @@ export function parseOmnibar(
|
||||
for (let i = 0; i < clauses.length; i += 2) {
|
||||
let clause = tidy(clauses[i]);
|
||||
const separator = clauses[i - 1]?.trim();
|
||||
const afterPlace = previousHadPlace;
|
||||
const afterPlaceComma = previousHadPlace && separator === ',';
|
||||
// "trinidad and tobago": a bare place joined to a bare place by "and".
|
||||
const afterBarePlaceAnd = previousBarePlace && separator === 'and';
|
||||
@@ -526,7 +587,14 @@ export function parseOmnibar(
|
||||
const addPlace = (value, { bare = false, leftover = false } = {}) => {
|
||||
if (!value) return;
|
||||
if (leftover) placeClauses.add(i);
|
||||
if (place && bare && afterBarePlaceAnd) place += ` and ${value}`;
|
||||
// "near Trinidad and Tobago", "in Bosnia & Herzegovina": one place.
|
||||
const joinsName =
|
||||
place &&
|
||||
afterPlace &&
|
||||
(separator === 'and' || separator === '&') &&
|
||||
isCompoundPlace(place.split(', ').at(-1), value);
|
||||
if (joinsName) place += ` and ${value}`;
|
||||
else if (place && bare && afterBarePlaceAnd) place += ` and ${value}`;
|
||||
else if (place && !afterPlaceComma) {
|
||||
if (separator === 'and') extraPlaces.push(value);
|
||||
return;
|
||||
@@ -834,7 +902,13 @@ export function parseOmnibar(
|
||||
// Thermal"): no plan runs on Enter; the line may still be a name.
|
||||
const unasked = steps.length > 0 && steps.every((step) => !asked.has(step));
|
||||
if (place && !placeFrom) placeFrom = placeBare ? 'bare' : 'near';
|
||||
if (place) steps.unshift({ kind: 'place', place });
|
||||
if (place) {
|
||||
const step = { kind: 'place', place };
|
||||
// How the place was typed, for its label only ("İstanbul", "iPhone
|
||||
// store"); kept off the step's fields, which plans compare.
|
||||
Object.defineProperty(step, 'shown', { value: asTypedIn(raw, place) });
|
||||
steps.unshift(step);
|
||||
}
|
||||
steps.verb = LAYER_OFF.test(line) || LAYER_ON.test(line) || GO.test(line);
|
||||
// Following happens once the camera has settled on the new view.
|
||||
steps.sort((a, b) => (a.kind === 'follow') - (b.kind === 'follow'));
|
||||
|
||||
+20
-11
@@ -22,6 +22,7 @@ import {
|
||||
hasActionVerb,
|
||||
isPanelText,
|
||||
isRealQuestion,
|
||||
asTypedIn,
|
||||
isWholeMapClear,
|
||||
magnitudeFloor,
|
||||
normalizeLine,
|
||||
@@ -393,12 +394,19 @@ const SMALL_WORDS = new Set([
|
||||
'da',
|
||||
'des',
|
||||
]);
|
||||
/**
|
||||
* A place's name for a row: as typed when it has a capital ("iPhone store",
|
||||
* "McAllen", "İstanbul"), else each word capitalised but the small joining
|
||||
* ones ("santa fe de bogotá" reads "Santa Fe de Bogotá").
|
||||
*/
|
||||
export const titleCase = (text) =>
|
||||
text.replace(/\S+/g, (word, at) =>
|
||||
at > 0 && SMALL_WORDS.has(word)
|
||||
? word
|
||||
: word[0].toUpperCase() + word.slice(1),
|
||||
);
|
||||
/\p{Lu}/u.test(text)
|
||||
? text
|
||||
: text.replace(/\S+/g, (word, at) =>
|
||||
at > 0 && SMALL_WORDS.has(word)
|
||||
? word
|
||||
: word[0].toUpperCase() + word.slice(1),
|
||||
);
|
||||
|
||||
const STYLE_LABELS = {
|
||||
noir: msg('Style: Noir'),
|
||||
@@ -707,7 +715,7 @@ export function matchCommands(text) {
|
||||
export function stepLabel(step) {
|
||||
switch (step.kind) {
|
||||
case 'place':
|
||||
return titleCase(step.place);
|
||||
return titleCase(step.shown ?? step.place);
|
||||
case 'layer':
|
||||
return step.enabled
|
||||
? t('{layer} on', { layer: step.layer.shown })
|
||||
@@ -819,12 +827,13 @@ const askRow = (text) => ({
|
||||
label: t('Ask GEV: “{query}”', { query: text }),
|
||||
hint: t('Opens chat'),
|
||||
});
|
||||
const placeRow = (place) => ({
|
||||
/** `shown`: the place as typed, for the label ("İstanbul", "iPhone store"). */
|
||||
const placeRow = (place, shown = place) => ({
|
||||
key: `place:${place}`,
|
||||
kind: 'place',
|
||||
icon: 'location_on',
|
||||
place,
|
||||
label: t('Go to {place}', { place: titleCase(place) }),
|
||||
label: t('Go to {place}', { place: titleCase(shown) }),
|
||||
hint: t('Place or coordinates'),
|
||||
});
|
||||
/**
|
||||
@@ -1049,7 +1058,7 @@ export function createOmnibar({ layers = [], isEntity = () => false } = {}) {
|
||||
realPlaces.length
|
||||
? realPlaces
|
||||
: place.trim().length >= minPlaceChars(place)
|
||||
? [placeRow(place)]
|
||||
? [placeRow(place, asTypedIn(text, place))]
|
||||
: [];
|
||||
|
||||
const titled = writtenAsName(text.split(/\s+/));
|
||||
@@ -1260,7 +1269,7 @@ export function createOmnibar({ layers = [], isEntity = () => false } = {}) {
|
||||
...(placeStep ? placeRows(placeStep.place).slice(0, 2) : []),
|
||||
// "near New York and Paris": the plan goes to the first; the others
|
||||
// are offered rather than dropped.
|
||||
...extraPlaces.map((extra) => placeRow(extra)),
|
||||
...extraPlaces.map((extra) => placeRow(extra, asTypedIn(text, extra))),
|
||||
...steps
|
||||
.filter((step) => step.kind === 'layer')
|
||||
.map((step) => layerRow(step.layer, step.enabled)),
|
||||
@@ -1272,7 +1281,7 @@ export function createOmnibar({ layers = [], isEntity = () => false } = {}) {
|
||||
if (ambiguous)
|
||||
return withEntities(
|
||||
[
|
||||
placeRow(peeled ? placeStep.place : text),
|
||||
peeled ? placeRow(placeStep.place, placeStep.shown) : placeRow(text),
|
||||
plan,
|
||||
...alternatives.filter((row) => row.kind !== 'place'),
|
||||
ask,
|
||||
|
||||
@@ -646,3 +646,27 @@ test('several switches in one line are one plan', () => {
|
||||
assert.equal(rows[defaultIndex].kind, 'plan', text);
|
||||
}
|
||||
});
|
||||
|
||||
test('a place named with "and" stays whole, and a place keeps how it was typed', () => {
|
||||
const omnibar = createOmnibar({
|
||||
layers: [{ id: 'flights', name: 'Live Flights', enabled: false }],
|
||||
});
|
||||
const first = (text) => omnibar.rows(text, { places: [] }).rows[0];
|
||||
const plan = first('flights near Trinidad and Tobago');
|
||||
assert.equal(plan.kind, 'plan');
|
||||
assert.equal(plan.steps[0].place, 'trinidad and tobago');
|
||||
assert.equal(
|
||||
first('flights near Bosnia & Herzegovina').steps[0].place,
|
||||
'bosnia and herzegovina',
|
||||
);
|
||||
// Two places joined by "and" are still two.
|
||||
assert.equal(
|
||||
first('flights near Austin and Dallas').steps[0].place,
|
||||
'austin',
|
||||
);
|
||||
// "İstanbul" is searched without a stray combining dot, and labelled as typed.
|
||||
const istanbul = first('show flights in İstanbul');
|
||||
assert.equal(istanbul.steps[0].place, 'istanbul');
|
||||
assert.match(istanbul.label, /^İstanbul /);
|
||||
assert.equal(first('iPhone store').label, 'Go to iPhone store');
|
||||
});
|
||||
Reference in new issue
Block a user