I can make a mistake once! How to build a neural network for correcting typos in search

Anna Vlasova8 Jul 2024

Problem

Users of a grocery delivery app often mistype search queries: wrong keys, wrong keyboard layout, unknown spelling. Product cards cannot list every misspelling, so such a query usually returns nothing, and seven of ten items at Kuper are added to the cart from search. The production corrector, SymSpell, is very fast but not accurate enough. A better one needs training pairs, yet the logs hold only raw queries with no record of past corrections.

Example from the article: for the typo «kurinye dapy» SymSpell returns «kurinye sady», while the neural model returns «kurinye lapy».

Solution in brief

Train a sequence-to-sequence model (bidirectional GRU encoder, GRU decoder with attention) that rewrites a misspelled query into a correct one, as in machine translation. Training pairs come from synthetic typos made from product names and verified correct queries, and from real pairs mined in user sessions. Real pairs gave the biggest gain.

Questions this case answers
  • How to train a query-correction model when the logs hold only raw queries and no record of past corrections
  • How to correct typos in grocery search queries better than a dictionary-based corrector?
  • How to build training pairs for a typo corrector when only the users' original queries are logged?
  • How to mine real typo and correction pairs from search sessions?

Tools and methods

  • GRU encoder-decoder with attention — maps a typo query to the corrected one
  • BPE tokenization — splits queries into subword tokens
  • Synthetic typo generation — builds pairs from product names and correct queries
  • Session-based pair mining — finds real typo and correction pairs in logs
  • Masked language modeling — pre-trains on full product names
  • Teacher forcing — speeds up training
  • SymSpell — the production baseline
  • Elasticsearch — runs the original and the corrected query
Continue reading — it's free

Google or a one-time email code. No password.