← writing

Matching power cuts by geography, not by name

4 min readdraft

This is a draft. The wording isn't final.

tog answers one question for people in Ulaanbaatar: is my power going out today? The data to answer it is public. The electricity distributor publishes a monthly PDF of planned maintenance, and it runs a live feed of faults. The hard part isn't getting the data. It's connecting the data to a person.

This post is about the decision that made that work, and the two rewrites it took to get there.

Two vocabularies

A maintenance notice names a khoroo, a sub-district, and then a few buildings or landmarks inside it. A person typing into a search box names their building, or their apartment complex, or the name everyone uses for that part of town. In principle these should meet. In practice they barely do.

The utility's wording changes from month to month. The same area appears under different phrasing, with different landmarks, in a different order. And people don't use the utility's names at all. They use the names on the side of their building, or a bus stop, or a shop that closed years ago but still names the corner.

My first version tried to bridge this with string matching: normalise the text, strip punctuation and suffixes, compare tokens, allow some fuzziness. It worked on the examples I'd looked at and failed on everything else. That wasn't because the matching was badly tuned. It's because two vocabularies that barely intersect can't be joined by comparing their words more cleverly. There's nothing in the strings to match.

"And their surroundings"

The second problem made string matching impossible even in principle. 85% of notices end with the phrase «болон эдгээрийн ойр орчмоор», which means "and their surroundings".

That phrase isn't decoration. It's the utility saying the outage extends past the listed buildings to the area around them. Which means a notice isn't a list you can test membership against. "Is my building on the list?" is the wrong question when the list says, in effect, "these, and things near these". The correct question is about distance.

So the answer had to come from a map.

Resolving both sides to places

The approach that worked was to stop comparing text to text, and resolve both sides to geography first.

On the notice side, the pipeline extracts the khoroo and the named landmarks from each row of the PDF, and resolves them to places with known locations. On the person's side, their search is resolved to a place too. Then the comparison happens between places: is this person's place inside an affected khoroo, and is it near the landmarks the notice lists?

In outline:

notice row  →  khoroo + landmarks  →  places on the map  ─┐
                                                          ├─→  verdict
search text →  gazetteer lookup    →  a place, a khoroo  ─┘

This needs a gazetteer: a list of place names with locations, covering the names people actually use. That turned out to be its own problem.

No single map knows the names

I started with OpenStreetMap, which is the obvious source. It's good, but it doesn't hold the names people search for in Ulaanbaatar, particularly the names of housing complexes. So I added Overture Maps as a second source, pulled out with DuckDB.

I expected Overture to mostly duplicate OpenStreetMap with some extra coverage. It didn't. 96% of what Overture holds for Ulaanbaatar is absent from OpenStreetMap under the same name, and that includes most of the housing complexes. Neither source is a superset of the other. The gazetteer now has 28,020 places drawn from both, and each one is resolved to a khoroo at build time, so the app never has to do that work in the browser.

The effect is that a search for a name the utility would never use still lands somewhere useful. You type the name of your street or complex and the app finds the khoroo for you. It never asks you for a khoroo number you might not know.

Never a confident wrong answer

Resolving by geography fixes the matching, but it doesn't make the data complete. The schedule covers planned work only, not faults. The khoroo boundary data is incomplete. And the sources disagree with each other in places.

The rule I settled on is that tog never gives a confident wrong answer. Where it's unsure, it says so. Where it knows nothing, it shows МЭДЭГДЭХГҮЙ, "not known", instead of a clear day. The known limits are stated in the interface, not only in the README. A tool like this is only useful if a green answer can be trusted, and that's only true if it's willing to show something other than green.

Keeping it honest month to month

Because the utility's wording changes, the pipeline has to notice when it changes. Each run prints a health report: the distribution of coverage, the kinds of landmarks it found, and the unclassified head words by frequency. When next month's PDF introduces new phrasing, it shows up as a new word near the top of that list rather than as rows that quietly fail to match.

The unit suite has 208 assertions, all passing. The monthly refresh is: drop the new PDF in, run the build, run the checks.

What I'd take from it

When two datasets don't share a vocabulary, matching their words harder won't help. Find a third thing both of them can be resolved to. Here it was geography. Once the problem became "are these two places near each other", the string problem mostly went away, and the remaining uncertainty became something the app could state honestly instead of hide.