Originally published on AssistApp Engineering.
People don't type search queries carefully. They type wiifi pasword with one thumb while standing at a friend's door. In AssistApp, our Android app for the notes, screenshots and documents you send yourself, a search that only matches exact words would fail those people exactly when they need it.
We didn't want a server-side search engine. The memories live encrypted on the phone, so matching has to happen on the phone too. This post shows the small piece of Kotlin that makes our word matching forgive typos: character trigrams, plus one cache that made it cheap enough to run over every memory on every question.
The idea: compare words by their letter triples
Split a word into overlapping three-letter pieces, with a space on each side so the start and the end count too:
"password" -> " pa", "pas", "ass", "ssw", "swo", "wor", "ord", "rd " "pasword" -> " pa", "pas", "asw", "swo", "wor", "ord", "rd " Two words are similar when they share many triples. We use the Jaccard index: shared triples divided by all distinct triples.
private fun trigrams(word: String): Set<String> { val padded = " $word " return (0..padded.length - 3).map { padded.substring(it, it + 3) }.toSet() } private fun jaccard(a: Set<String>, b: Set<String>): Float { val inter = a.count { it in b } val union = a.size + b.size - inter return if (union == 0) 0f else inter.toFloat() / union } Some real pairs:
| You typed | Saved word | Trigram similarity |
|---|---|---|
| pasword | password | 0.67 |
| pasport | passport | 0.67 |
| wiifi | wifi | 0.50 |
| insurence | insurance | 0.50 |
A threshold, and exact words first
Unrelated words still share a triple now and then, so a similarity only counts above a threshold. Ours is 0.45. An exact word always scores 1.0, and words shorter than three letters must match exactly (two letters give too few triples to compare fairly).
private const val MIN_TOKEN_SIM = 0.45f fun bestMatch(token: String, candidates: Set<String>): Float { if (token in candidates) return 1f if (token.length < 3) return 0f val qg = trigrams(token) var best = 0f for (c in candidates) { if (c.length < 3) continue val sim = jaccard(qg, trigrams(c)) if (sim > best) best = sim } return if (best >= MIN_TOKEN_SIM) best else 0f } From words to a score for the whole text
A question has several words, and a saved memory has many. We compute a "soft" word overlap: each query word contributes 1.0 if it appears exactly, or its best trigram similarity if it only appears misspelled. That soft intersection goes into a Jaccard over words. We add a bonus for an exact run of consecutive query words, because wifi password office matching in that order says more than the same three words scattered around:
val softInter = qTokens.sumOf { bestMatch(it, tTokens).toDouble() }.toFloat() val union = (qTokens.size + tTokens.size - softInter).coerceAtLeast(1f) val jaccard = softInter / union val cover = partialCover(qTokens.toList(), tTokens.toList()) // longest exact run of query words return (0.7f * jaccard + 0.3f * cover).coerceIn(0f, 1f) One bug we found in our own code: normalisation. An early version stripped everything that wasn't [a-z0-9]. That silently erased text in non-Latin scripts and split words with umlauts in half. Now letters and digits of any script are kept. If your app is used in many languages, check your normaliser first.
This score is one signal among several. The ranker also blends a meaning-based similarity (on-device embeddings) and how recent a memory is, so a fuzzy word match helps a memory up the list but never decides on its own.
Making it cheap: cache per question, not per memory
Measured on a phone, one question over 1,000 memories took about 1.5 seconds, and most of that time was spent right here, rebuilding the same triples over and over. The same words (the, card, 2026...) appear in hundreds of memories, and the query never changes during one search.
So we prepare the question once per search: its words, their triples, and a map that remembers every word-to-word similarity already computed in this pass.
class Prepared(val tokens: Set<String>) { private val grams = HashMap<String, Set<String>>() private val sims = HashMap<String, HashMap<String, Float>>() fun bestMatch(token: String, candidates: Set<String>): Float { if (token in candidates) return 1f if (token.length < 3) return 0f val qg = grams.getOrPut(token) { trigrams(token) } val known = sims.getOrPut(token) { HashMap() } var best = 0f for (c in candidates) { if (c.length < 3) continue val sim = known.getOrPut(c) { jaccard(qg, trigrams(c)) } if (sim > best) best = sim } return if (best >= MIN_TOKEN_SIM) best else 0f } } The arithmetic is unchanged, so every score is bit-for-bit identical to before. We have a test that compares the cached scorer with the old one on the same inputs, which made the speed-up safe to ship. One Prepared lives for one ranking pass and is never shared between threads.
What this approach doesn't do
- Synonyms. Trigrams know that
paswordis close topassword. They don't know thatloginis close topassword. That needs a meaning-based signal. - Very short words.
idvsitcan't be told apart reliably, so under three letters we require an exact match. - Wildly wrong spellings. Below 0.45 similarity we'd rather miss than return junk.
Try it
AssistApp is on Google Play. Save something, then look for it with a typo. If it fails, I'd like to hear the query; that's how the next threshold gets tuned.
More about the app: assistapp.net