Address suggestions without Google

A few days into Chaufr's beta, a rider named Munachiso booked a chauffeur and sent us one piece of feedback: the pickup field should suggest addresses, the way ride-hailing apps do. He was right. Anyone who has used Uber or Bolt expects to type a few letters and pick from a list.

Those apps buy their suggestions from map companies. We wanted the same help for riders without paying per search, and without sending what a rider types to anyone else. This post is how we built it from free data and free software.

The data: OpenStreetMap

OpenStreetMap is a free map of the world, built by volunteers. Geofabrik publishes it as a download per country. We took the Nigeria extract, cut out Lagos with Osmium, and kept everything with a name: streets, markets, churches, mosques, schools, estates, bus stops. That came to 12,578 places.

Lagos is reasonably well mapped. A random sample read like a Lagosian giving directions: Lekki Market, Addax Jetty, Igode, the Celestial Church of God.

Cleaning, and the fear of losing places

Before importing, we planned to drop house numbers with no street, and duplicates where OpenStreetMap splits one long street into many pieces. That raised a fair worry: does trimming data mean some rider can't find their street?

So the import wrote every dropped row to a file, with the reason, for a person to read. In the end it dropped 2 of 12,578, both exact copies of a place already kept. The rule worth having was the file of drops: a worry answered by evidence instead of argument.

Telling two streets apart

Lagos has more than one Isaac John Street. A list that says "Isaac John Street" twice helps nobody, so each place takes the name of the nearest suburb, village or neighbourhood within three kilometres, also from OpenStreetMap. Typing "Isaac" now offers:

Isaac Okomo Street, Ogudu
Isaac John Street, Igbobi
Isaac Solame Street, Ologode
Isaac John Street, Ikeja G.R.A

Matching what people actually type

People type the start of a word, and they make typos. The obvious tool for "how close are two strings" is Levenshtein distance, the number of single-letter edits from one to the other. It is the wrong tool here, and the examples below show why. They are written for this post, and checked on every build with mdx.

let levenshtein a b =
  let row = Array.init (String.length b + 1) Fun.id in
  String.iteri
    (fun i ca ->
      let diagonal = ref row.(0) in
      row.(0) <- i + 1;
      String.iteri
        (fun j cb ->
          let above = row.(j + 1) in
          row.(j + 1) <-
            min (min (above + 1) (row.(j) + 1))
              (!diagonal + if ca = cb then 0 else 1);
          diagonal := above)
        b)
    a;
  row.(String.length b)

A rider who wants Admiralty Way types "admir". Levenshtein compares whole strings, so the unfinished word is far from the long name, and closer to a short unrelated one, Aorta Interchange:

# levenshtein "admir" "admiralty way";;
- : int = 8
# levenshtein "admir" "aorta";;
- : int = 4

It is also slow: it has to compare the input against every place, and no index can help.

PostgreSQL's free pg_trgm extension takes another view. A trigram is three letters in a row. Two strings are similar when they share many trigrams:

let trigrams word =
  List.init (max 0 (String.length word - 2)) (fun i -> String.sub word i 3)

let shared a b =
  let theirs = trigrams b in
  List.length (List.filter (fun t -> List.mem t theirs) (trigrams a))
# trigrams "admir";;
- : string list = ["adm"; "dmi"; "mir"]
# shared "admir" "admiralty way";;
- : int = 3
# shared "admir" "adeola odeku";;
- : int = 0
# shared "admirality" "admiralty way";;
- : int = 5

Every trigram of "admir" is in "admiralty way", so a half-typed word matches well, and the typo "admirality" still shares most of its pieces. PostgreSQL does this properly, padding each word so matches at its start rank higher, and comparing against the best-matching part of a long name. It also keeps an index from each trigram to the places containing it, the way a book's index lists pages, so a search looks up a handful of candidates instead of reading every row.

The whole search is one query:

SELECT label FROM places
WHERE $1 <% search
ORDER BY word_similarity($1, search) DESC, length(label)
LIMIT $2

In the form

The pickup field is an ordinary HTML input with a <datalist>. After three characters and a short pause, a few lines of JavaScript ask Chaufr for up to eight suggestions, and the browser shows them. Free text still works: in Lagos people often describe a pickup by landmark, and every booking accepts whatever the rider types.

Nothing a rider types leaves Chaufr. There is no map company in the middle, which also keeps our privacy policy simple.

What it cost

No fees and no new service to run: data from OpenStreetMap, the PostgreSQL we already had, and a small table of 12,576 rows. One obligation comes with the data, and it is a fair one: OpenStreetMap's licence asks for credit, so every page of Chaufr now says "Map data © OpenStreetMap contributors". When riders find a street missing, the fix is to add it to the map, and it reaches Chaufr at the next import.

Thank you, Munachiso, for the idea. If you are building something and want free software doing the heavy lifting, we would like to hear about it.

— Chukwuma

Found this useful? Share it: Discuss on Hacker News | Submit to Lobsters