Finding a place: names, transliteration and a clear choice
Search across spellings while retaining place identity and presenting ambiguity with enough context to choose.
A person types a settlement name and expects to recognize the right place. But one name may belong to several towns or villages, a Latin spelling may have several variants, and an omitted mark may turn an exact match into a search that makes an assumption. If the interface immediately opens the first result, even careful sorting can send the user somewhere unintended.
Useful search has two parts: retrieve plausible candidates and give the person enough context to choose. We will examine both in an original Python 3.12.3 example containing twelve fictional places. It has no coordinates, real administrative boundaries or requests to an external service. Its purpose is to make matching rules and their mistakes visible.
Place identity does not depend on spelling
The corpus contains three different places called “Зореліс”: two in fictional country A and one in country B. Each has a separate stable id, a country, a district, localized names and alternative spellings. The search string helps find these records; it does not determine whether they represent the same place.
This separation has a practical consequence. Renaming a display label should not change the reference to a selected place. Adding a Latin variant does not create another town. Conversely, identical names on two records are no reason to merge their identifiers.
The GeoNames format also separates a record identifier, its main name, an ASCII spelling and alternate names; alternatives can carry a language and a historical-name flag. Our corpus does not import that gazetteer. It uses a much smaller original model with every relationship declared explicitly.
After searching, the application should retain the selected id. If it stores only the text from the input field, the next change to names or result order will require guessing again what the person previously selected.
Write expectations before the algorithm
Before implementing search, we prepared a separate set of 27 queries and literal lists of acceptable identifiers. Twenty-three queries require at least one place; four have no acceptable results. The corpus and these judgments were frozen before the first run. Expectations were not changed after the measurements arrived.
These are author-written teaching judgments, not a study of real user behavior. They still impose a useful constraint: the code cannot declare everything it returns correct. Each query has a stated intent in advance. For example, north is an incomplete name with two acceptable completions, whereas Northmere is the complete name of one particular place.
| Query | Acceptable result under the stated intent |
|---|---|
Зореліс |
All three places with that name |
zorelis with country A and the Eastern district |
One place in that district |
Synyokray |
The place with that explicitly recorded alternative |
merive |
Mérive, despite the omitted accent |
Béla |
Only the place with that exact spelling |
lunora |
Łunora, which the user intended to find without the stroke |
The last row deliberately describes a reader's need, not an algorithmic promise. If the rule fails to satisfy it, the measurement must record a miss. Likewise, an extra result for a complete name remains extra even if it looks like a plausible suggestion.
Normalization, diacritics and transliteration are separate rules
The program first collapses extra whitespace, applies casefold() and uses Unicode NFC. This supports comparisons across case and canonically equivalent representations. For example, é can be one character or a sequence of e followed by a combining accent. Unicode Normalization Forms explains canonical equivalence; Python exposes the forms through unicodedata.normalize.
A separate, deliberately lossy rule follows: after NFD decomposition, remove combining marks only after a Latin base letter. This allows Mérive to match merive. The original display name remains unchanged. This is a search policy, not a claim that the two spellings are identical in every language.
The rule does not remove marks from Cyrillic. Doing so could unintentionally erase the distinction between й and и. It also does not turn Ł into L: that decomposition does not occur in this operation. Consequently, lunora does not find Łunora. The missing form needs a justified alias or a separate transliteration rule.
For “Синьокрай,” the Latin forms Syniokrai and Synyokray are recorded explicitly. The program does not generate arbitrary transliterations for every language. A mixed spelling containing Cyrillic С followed by Latin yniokrai is rejected as unsupported in this exercise. This is a narrow policy for one field, not a universal rule for multilingual names.
Several match methods, one place card
The algorithm compares the query with every recorded name and alias. The strongest matching method determines its ranking group: exact name, exact alias, a match after removing Latin marks, name prefix, or one permitted typo. If a place matches through three spellings, the response still contains one card with its id.
Within a group, the stable identifier determines order. There is no population, distance or hidden user history here. The order is reproducible, but that does not establish that the first candidate is the one a particular person most likely wants.
Typo matching uses Levenshtein distance with one insertion, deletion or substitution. Both strings must contain at least five characters, and their first letter must match. This limits the set of assumptions, but misses some human mistakes.
For example, Tykhoveza finds Tykhovezha, whereas Tykhovehza does not: swapping two neighboring characters requires two operations in our function. That behavior should not be equated with every fuzzy-search mode. Elasticsearch documents transpositions and expansion limits separately; its behavior depends on parameters. This experiment does not run Elasticsearch.
Administrative context is needed before navigation
For the three places called “Зореліс,” results include the country and district. The name alone leaves the choice unresolved. Filtering to country A leaves two places; adding the Eastern district leaves one. If a specified country has no such place, search does not silently expand to other countries.
The same distinction appears in GeoNames Search: country and administrative-code restrictions are separate from favoring a country in ranking. A filter determines where to search; a preference determines what to show earlier. Substituting one for the other changes the answer's meaning.
In our API, country and district are supplied as stable codes; names are labels only. Unknown codes are rejected. Known but incompatible filters produce an empty result. The response also identifies the language of the place name and of the administrative labels: the latter remain Ukrainian in this small corpus. The example does not fully localize a gazetteer.
An empty response should let the person correct the spelling, check filters or refine the search scope. It should not open an arbitrary place whose string happens to be closest. Failure to match our corpus says nothing about whether a settlement exists in the world.
A result limit does not remove ambiguity
Queries are limited to 100 characters and results to between one and five. Evaluation returns at most three. Empty queries and strings shorter than three characters do not trigger suggestions. The corpus is bounded too: this is a direct scan of a small collection, not a measurement of a large index's speed.
The program first finds every candidate in this corpus, then truncates the list. Even with limit=1, a query for “Зореліс” retains candidate_count=3, a truncation flag and the state needs_choice. One visible row does not turn three possible places into one confirmed location.
For a single candidate, the state is called candidate. The selected_id field stays empty until selection happens outside the search function. match_kind explains the matching method; it is not a probability. “One typo” is a more accurate label than an invented “97% confidence” without calibration on real queries.
What the measurement showed
We separately ran 25 unittest test methods covering stable identities, Unicode, duplicate aliases, filters, typos, limits, retained ambiguity and metric arithmetic. These confirm the implementation of the declared rules. Retrieval quality is measured in another run against the 27 frozen queries.
With a limit of three, the observed counts were:
| Measurement | Result |
|---|---|
| Queries needing a place that retrieved at least one acceptable result | 21 of 23 |
| Retrieved acceptable query–place pairs | 30 of 32 |
| Acceptable pairs among all actually returned pairs | 30 of 32 |
| Queries with no acceptable place that received an extra result | 0 of 4 |
Pooling the acceptable pairs gives recall@3 of 93.75%. The share of acceptable results among those actually returned is also 93.75%. The identical numbers are coincidental: the first denominator counts every required pair, the second every returned pair. This is not precision with a fixed denominator of three slots per query. Empty slots do not count as returned results.
The two misses are clear: lunora and the transposition in Tykhovehza. Two extra results are visible too: the accent-removal rule adds Bela for Béla, and the prefix rule adds Northmere Park for the complete name Northmere. Expectations were not rewritten to hide these cases.
Improvements begin with a specific mistake
Adding a missing alias, allowing transpositions or narrowing suggestions after an exact match are different changes with different consequences. Each may help one query while removing a useful result from another. The next step is therefore to record a rule change, add new independent examples and compare the full set while retaining the old expectations.
Twelve places and 27 author-written queries do not evaluate a worldwide gazetteer, production geocoding or search behavior across all languages. Coordinates, administrative currency, large-index speed and SEO benefits have not been tested. What the exercise does provide is a concrete interface result: a stable object, an explained matching method, visible context and a choice between several places without invented certainty.