Skip to content

Fuzzy matching and cities in text

The user writes "moro em sao jose dos campos - sp", "vou pra floripa" or "sao paolo". You need to know which city it is, with its IBGE code and coordinate, without an external service and without failing on a typo.

To match what the user typed against a known list of names (cities, products, categories), lexical matching beats an embedding: no model to download, no GPU, and a typo costs one edit. When the user describes it in other words, this same page shows search by meaning (embeddings), the combined search, and the agent tools; semantic search over long passages stays in genai (Retriever, HybridRetriever).

This recipe has two parts:

  1. tempest_fastapi_sdk.text: normalization, edit distance and a fuzzy index, with no dependency.
  2. tempest_fastapi_sdk.geo: the 5571 IBGE municipalities bundled, lookup by name and finding the city in free text.

Normalize and compare

from tempest_fastapi_sdk.text import FuzzyIndex, normalize_text, similarity

print(normalize_text("  São José-dos Campos/SP "))
print(round(similarity("florianopols", "florianopolis"), 2))

index: FuzzyIndex[int] = FuzzyIndex(
    [("Camiseta básica", 101), ("Calça jeans", 102), ("Camisa social", 103)]
)
for match in index.search("camizeta basica"):
    print(match.key, match.value, round(match.score, 2))

Output:

sao jose dos campos sp
0.92
camiseta basica 101 0.93
  • normalize_text strips accents, ignores case and turns punctuation into spaces. It is the form everything else compares.
  • similarity goes from 0.0 to 1.0: 1 - edits / longer length. Swapping two neighbouring letters ("soa" for "sao") counts as one edit.
  • FuzzyIndex holds name → value. exact() finds the normalized name; search(query, cutoff=0.8, limit=5) accepts typos down to cutoff.

Why it is fast

The index stores the trigrams (three-letter slices) of each name. A query only computes the edit distance against the names that share enough trigrams with it. The filter loses nothing: a name d edits away shares at least trigrams - 4d with the query, and the index only drops below that. When that bound reaches zero (a short query with a low cutoff), the index compares against every name of a close length. A test compares the index with comparing against every name, over 300 random queries for each cutoff from 0.5 to 0.9, and the two agree.

Find the city in a text

from tempest_fastapi_sdk.geo import find_municipalities_in_text

for text in [
    "moro em sao jose dos campos - sp",
    "Sou de Campinas, São Paulo",
    "vou pra floripa semana que vem",
    "estou feliz hoje",
    "nasci em bom jesus",
]:
    matches = find_municipalities_in_text(text, limit=3)
    print(text)
    for match in matches:
        city = match.municipality
        print(
            f"  {city.name}/{city.uf.value} ibge={city.ibge_code}"
            f" score={match.score:.2f} confirmed={match.uf_confirmed}"
            f" text={match.matched_text!r}"
        )

Output:

moro em sao jose dos campos - sp
  São José dos Campos/SP ibge=3549904 score=1.00 confirmed=True text='sao jose dos campos'
Sou de Campinas, São Paulo
  Campinas/SP ibge=3509502 score=1.00 confirmed=True text='Campinas'
vou pra floripa semana que vem
  Florianópolis/SC ibge=4205407 score=0.90 confirmed=False text='floripa'
estou feliz hoje
nasci em bom jesus
  Bom Jesus/PI ibge=2201903 score=0.90 confirmed=False text='bom jesus'
  Bom Jesus/RN ibge=2401701 score=0.90 confirmed=False text='bom jesus'
  Bom Jesus/PB ibge=2502201 score=0.90 confirmed=False text='bom jesus'

What each case shows:

  • A state right after confirms. "- sp", "/SP", ", São Paulo" or "SP" right after the name keep only that state's city and set uf_confirmed=True (score 1.0). A state the name does not exist in ("Campinas/RJ") drops the city.
  • The state is not a city. In "Campinas, São Paulo", "São Paulo" is Campinas' state and does not show up as the capital.
  • Nicknames count: bh, sampa, floripa, poa, bsb, jampa, sjc, rio (BR_CITY_NICKNAMES). The Federal District's administrative regions (Ceilândia, Gama, ...) point at Brasília, and older spellings ("Açu" for "Assú") match too. A nickname is a one-word name like any other: it needs a sign of being a place ("vou pra bh", "Rio", "sampa, sp", or the whole text being just "bh").
  • An everyday word is not a city. "Feliz", "Natal" and "Alegre" are municipalities. Lowercase, among other words, with no "em/de/moro" before and no state after, the word keeps half its score and falls below the default min_score (0.6). "moro em Feliz/RS" finds Feliz, and so does a text that is only the name ("feliz", a form's city field).
  • Homonyms without a state tie. "bom jesus" returns every Bom Jesus with the same score; you decide, with other information (the CEP, the area code, the next question to the user).

matched_text, start and end point at the text as the user wrote it, so you can highlight or replace it in the original.

Typos inside the sentence

Every run of up to four words, at least five letters long and with no exact match, goes through the fuzzy lookup with cutoff=0.85. "trabalho em sao paolo" finds São Paulo at 0.80: the name similarity (0.89) times 0.9, since no state confirmed it.

Trip request: from where, to where, when

Finding the city is half of a trip search. The other half is its role: in "quero ir de Teresina pra Parnaíba sexta de manhã", Teresina is the origin, Parnaíba the destination, Friday the date and morning the period. parse_trip_intent reads all of it from a typed or transcribed phrase:

from datetime import date

from tempest_fastapi_sdk.geo import parse_trip_intent

today = date(2026, 10, 14)
for text in [
    "quero ir de teresinha pra parnaiba sexta de manhã",
    "de Pico pra Sobrau amanhã",
    "de floripa pra sampa dia 20",
    "de pedro segundo pra piripiri à noite",
]:
    intent = parse_trip_intent(text, today)
    origin = intent.origin.name if intent.origin else None
    destination = intent.destination.name if intent.destination else None
    print(text, "->", origin, destination, intent.departure_date, intent.period)
quero ir de teresinha pra parnaiba sexta de manhã -> Teresina Parnaíba 2026-10-16 manha
de Pico pra Sobrau amanhã -> Picos Sobral 2026-10-15 None
de floripa pra sampa dia 20 -> Florianópolis São Paulo 2026-10-20 None
de pedro segundo pra piripiri à noite -> Pedro II Piripiri None noite

Each line shows an error speech-to-text makes and the search absorbs:

  • "teresinha", "Pico": a letter swapped or dropped. FuzzyIndex gathers candidates by edit distance, and the difflib ratio decides between them (Teresina beats Terezinha/PE).
  • "Sobrau": the syllable-final "l" spoken Portuguese says as "u". A phonetic key (vocalized_key) matches exactly, without lowering the cutoff.
  • "floripa", "sampa": the nicknames in BR_CITY_NICKNAMES.
  • "pedro segundo": the Roman numeral said as a word.

Only the cities you serve

Pass municipalities= with the cities that have routes, and only those are recognized. A real city outside the list comes back in unrecognized, with what was said, for the app to ask instead of searching without a destination:

from datetime import date

from tempest_fastapi_sdk.geo import (
    build_trip_city_index,
    municipality_by_name,
    parse_trip_intent,
)

served = [
    municipality_by_name(name, uf)
    for name, uf in [("Teresina", "PI"), ("Picos", "PI"), ("Sobral", "CE")]
]
index = build_trip_city_index([city for city in served if city is not None])

intent = parse_trip_intent(
    "de Teresina pra Natal amanhã", date(2026, 10, 14), municipalities=index
)
print(intent.origin.name, intent.destination, dict(intent.unrecognized))
# Teresina None {<TripRoleEnum.DESTINATION: 'destination'>: 'natal'}

municipality_by_name links the name your database stores ("SAO LUIS", "MA") to the IBGE record, ignoring case and accents. Build the index once and reuse it.

Homonyms and ambiguity

"Batalha" exists in PI and AL. Without a state both come back in intent.origin.candidates (resolved is None, name is "Batalha"), so the decision is yours. "Batalha no Piauí" or "Batalha PI" settles it.

Ordinary words country-wide

Without municipalities=, a lowercase loose word only becomes a city when the phrase is nothing but cities ("picos sobral"): "estou feliz de ir pra fortaleza" is not the town of Feliz (RS). With a restricted list, that guard does not apply.

Leaving now

In voice search, "quero ir agora" ("I want to go now") is as common as "hoje". agora, agora mesmo, já, imediatamente, o quanto antes, o mais rápido possível, neste momento and nesse momento become departure_date=today and set departure_now:

from datetime import date

from tempest_fastapi_sdk.geo import parse_trip_intent

today = date(2026, 11, 13)
for text in [
    "quero ir de picos para teresina agora",
    "picos teresina agora mesmo",
    "de picos pra teresina o quanto antes",
    "agora quero ver de picos pra teresina amanhã",
]:
    intent = parse_trip_intent(text, today)
    print(text, "->", intent.departure_date, intent.departure_now)
quero ir de picos para teresina agora -> 2026-11-13 True
picos teresina agora mesmo -> 2026-11-13 True
de picos pra teresina o quanto antes -> 2026-11-13 True
agora quero ver de picos pra teresina amanhã -> 2026-11-14 False
  • Whole words: "agorafobia" and "Jaqueira" (the town in PE) do not count, regardless of case and accents ("JÁ!" counts).
  • A date said wins: with "amanhã", "sábado" or "20/10" in the phrase, the date is the one said and departure_now is False. Only a date said that falls on today ("hoje agora", or "sexta" on a Friday) keeps departure_now=True. A date that does not exist ("dia 31 de fevereiro") wins too: the date stays None for the app to ask.
  • No prepositions: "picos teresina agora mesmo" falls into the cities-in-order rule (first origin, second destination), because "agora mesmo" leaves the phrase first and only cities are left.

The parser has no clock

parse_trip_intent receives today: date, not the time. departure_now comes with no period_start: the caller applies "from now on" with its own clock, showing only today's trips still to depart.

Quality is fixed by a table of 65 real phrases (tests/geo/test_trip_intent.py) from BusCar's production trip search. A change that breaks a line is a search regression.

By name and by code

from tempest_fastapi_sdk.geo import municipality_by_ibge_code, search_municipalities

for match in search_municipalities("sao paolo", limit=2):
    print(match.municipality.name, match.municipality.uf.value, round(match.score, 2))

city = municipality_by_ibge_code(3550308)
assert city is not None
print(city.name, city.coordinate.latitude, city.coordinate.longitude, city.capital)

Output:

São Paulo SP 0.89
São Paulo -23.5329 -46.6395 True

search_municipalities(name, uf=...) fits a form's "city" field: with uf, that state only. br_municipalities() returns the whole list, ordered by code.

Which point the coordinate is

coordinate is the municipality's seat (the city where the town hall is): the point to mark the city on a map, measure a distance to it or rank by "nearest to me". The point field says what the coordinate is:

from tempest_fastapi_sdk.geo import br_municipalities, municipality_by_ibge_code

picos = municipality_by_ibge_code(2208007)
assert picos is not None
print(picos.name, picos.coordinate.latitude, picos.coordinate.longitude, picos.point)

fallback = [m.name for m in br_municipalities() if m.point == "centroid"]
print(fallback)

Output:

Picos -7.07721 -41.467 seat
["Pau D'Arco", 'São Caitano', 'São Sebastião da Vargem Alegre', 'Cerro Azul', 'Alto Paraíso']

Five municipalities answer point == "centroid": the source puts their seat outside their own territory (Pau D'Arco/PA 750 km away, near Belém; Cerro Azul/PR 360 km; Alto Paraíso/PR 288 km; São Sebastião da Vargem Alegre/MG 157 km; São Caitano/PE 7.5 km past the boundary — measured point-in-polygon against the IBGE 2022 municipal mesh at maximum quality). For those, the coordinate is the territory centroid, the same point tempest-react-sdk returns with point: "centroid". The other 5566 coordinates are identical to tempest-react-sdk's br-seats.json, and a test pins that.

FuzzyIndex finds the name spelled alike; it does not find the name that means the same. "tênis de corrida" shares no letters with "calçado esportivo". That is what an embedding is for, and genai.rag brings two indexes over any SDK embedder (Embedder, OllamaEmbedder, OpenAICompatEmbedder, OnnxEmbedder):

  • SemanticNameIndex: ranks the names by cosine similarity to the query. The names are embedded once, in a batch, on the first search.
  • HybridNameIndex: runs both sides, spelling (word by word, typos included) and meaning, and fuses the rankings with reciprocal_rank_fusion. Each result carries lexical_score and semantic_score, so you can see why it came back.
import asyncio

from tempest_fastapi_sdk.genai import OllamaEmbedder
from tempest_fastapi_sdk.genai.rag import HybridNameIndex

PRODUCTS: list[tuple[str, int]] = [
    ("Calçado esportivo para corrida", 1),
    ("Camiseta básica de algodão", 2),
    ("Calça jeans masculina", 3),
    ("Garrafa térmica 1 litro", 4),
    ("Mochila para notebook", 5),
    ("Fone de ouvido bluetooth", 6),
]


async def main() -> None:
    embedder = OllamaEmbedder(model="nomic-embed-text")
    index: HybridNameIndex[int] = HybridNameIndex(embedder, PRODUCTS)
    for query in ["camizeta basica", "tênis de corrida", "headphone sem fio"]:
        best = (await index.search(query, limit=1))[0]
        spelling = "-" if best.lexical_score is None else f"{best.lexical_score:.2f}"
        meaning = "-" if best.semantic_score is None else f"{best.semantic_score:.2f}"
        print(f"{query!r} -> {best.key} (spelling {spelling}, meaning {meaning})")


asyncio.run(main())

Output, with nomic-embed-text on a local Ollama:

'camizeta basica' -> Camiseta básica de algodão (spelling 0.94, meaning 0.84)
'tênis de corrida' -> Calçado esportivo para corrida (spelling 0.50, meaning 0.64)
'headphone sem fio' -> Fone de ouvido bluetooth (spelling -, meaning 0.57)
  • "camizeta basica" came through both sides: spelling matches both misspelled words, and so does meaning.
  • "tênis de corrida" came through meaning. The word "corrida" matches, but alone it covers only half the query.
  • "headphone sem fio" came only through meaning: no word matches.

Short words do not count

The spelling side ignores words shorter than three letters (MIN_LEXICAL_WORD_LENGTH). Without that, the "de" in "tênis de corrida" matched every name holding a "de".

When to use which

A list of proper names (cities, people, brands): FuzzyIndex, no model. A catalogue where users describe what they want in other words: HybridNameIndex, which keeps spelling as a signal of its own instead of relying on the model having seen the misspelling.

In a conversation: agent tool

An agent talking to a user needs to find the city without you writing the step. municipality_search_tool() gives it the municipality lookup (sentence or name, optional uf), and name_search_tool(index) exposes any of the three indexes.

import asyncio

from tempest_fastapi_sdk.agents import Agent, municipality_search_tool
from tempest_fastapi_sdk.genai import OllamaGenerator


async def main() -> None:
    agent = Agent(
        OllamaGenerator(model="qwen2.5:3b"),
        tools=[municipality_search_tool()],
    )
    run = await agent.run(
        "O usuário escreveu: 'vou pra floripa semana que vem'. "
        "Qual é a cidade, a UF e o código IBGE?"
    )
    print([step.name for step in run.steps if step.kind == "tool"])
    print(run.output)


asyncio.run(main())

Output of one of the runs with qwen2.5:3b on a local Ollama:

['find_brazilian_city']
A cidade é Florianópolis, que está localizada no estado de Santa Catarina (UF SC). Seu código IBGE é 4205407 e as coordenadas geográficas são lat -27.5945, lon -48.5477.

Over 20 runs in a row (N=20) the model called the tool in every one and answered Florianópolis/SC with code 4205407 in every one. In 3 of them it passed uf="SC" along with the sentence: the state filters what the sentence found, instead of replacing the sentence search with a lookup of the whole text as a name. The answer's wording changes from run to run; what the tool returns does not.

What it costs

Measured on x86_64 with Python 3.12.7:

What Median
First call in the process (builds the index of 5571 names) 45.9 ms (N=5)
"vou pra floripa semana que vem" 0.79 ms (N=200)
"moro em sao jose dos campos - sp" 3.02 ms (N=200)
A sentence with three cities, one misspelled 3.27 ms (N=200)
parse_trip_intent, phrase with no typo (whole country) 0.03 ms (N=200)
parse_trip_intent, "de Pico pra Sobrau amanhã" (whole country, cold cache) 8.4 ms (N=200)
parse_trip_intent, "teresinha" + "floripa" (whole country, cold cache) 28.3 ms (N=200)
Index restricted to 4 cities (build_trip_city_index) 0.05 ms

The index is built once per process and stays in memory.

Where the data comes from

Name and state come from the IBGE localities API (public data); coordinate, capital and the older spellings come from kelvins/municipios-brasileiros (MIT, with the license notice in the generated module). Both sources list the same 5571 codes. The list_states() table (BR helpers recipe) counts 5606 because it includes the Federal District's 35 administrative regions; here they are nicknames of Brasília. To refresh: uv run python scripts/regen_br_municipalities.py. The kelvins file is read from a pinned commit (503e2f70) and refused unless its SHA-256 matches, with a size cap and a strict CSV parse; the same commit tempest-react-sdk uses. A test pins the data hash, so a regeneration shows up in the PR diff.

Recap

  • tempest_fastapi_sdk.text: normalize_text, similarity and FuzzyIndex to match what the user types against any list of names, with no dependency.
  • find_municipalities_in_text: finds the city in a sentence, with the state confirming and breaking ties, nicknames, and everyday words filtered.
  • parse_trip_intent: origin, destination, date and period of a trip phrase, with transcribed speech (typos, vocalized "l", numerals as words) and restricted to the cities you serve (municipalities=). "Agora", "já" and "o quanto antes" are today with departure_now; the time filter is yours.
  • search_municipalities / municipality_by_ibge_code: lookup by name (typos included) and by code, with coordinate and capital.
  • Homonyms without a state come back tied. The decision is yours.
  • SemanticNameIndex / HybridNameIndex: search by meaning, and by meaning plus spelling, over any SDK embedder.
  • municipality_search_tool / name_search_tool: the same searches as an agent tool.