Fuzzy Search and Q-Grams
Error-tolerant (prefix) search over a dictionary with edit distance and prefix edit distance, made fast by filtering candidates with the q-gram lemma and a q-gram index.
Concepts#
- Fuzzy Searchthe problem and the filter-then-verify algorithm start here
- Edit Distancerelevance model for whole words, DP
- Prefix Edit Distancerelevance model for search-as-you-type, minimum of the last DP row
- Q-Gramlength- substrings, with padding
- Q-Gram Lemmalower bound on common q-grams from ED / PED
- Q-Gram Indexinverted lists from q-grams to word ids, yields all common counts
How it fits together#
Two challenges, as for keyword search: relevance (what matches) and an index (find it fast).
- Relevance: ED for fuzzy search, PED for fuzzy prefix search; PED is computed from the same table as ED.
- Speed-up: ED/PED for all of is a linear scan. The Q-Gram Lemma gives a necessary condition on the number of common q-grams, and the Q-Gram Index computes these counts for all words by merging lists. Only words passing the bound are verified.
- Padding must match the distance, otherwise the bound or the distance changes.
| Fuzzy search (ED) | Fuzzy prefix search (PED) | |
|---|---|---|
| Matches | ||
| Padding | $ on both sides | $ on the left only |
| Filter | ||
| Verify | DP, bottom-right entry | DP, min of last row, first columns |
| Index size |
Typical traps: PED is not symmetric and has no length lower bound; right padding breaks PED; no padding stays correct but filters less.
Key Formulas#
- Recurrence and DP of Edit Distance; .
- (Q-Gram).
- and its corollaries (Q-Gram Lemma).
Exercises & Exam Questions#
SuccessSolution
3.1 Padded: $abba (1), $bab (2), $aba (3).
| 2-gram | Inverted list |
|---|---|
$a | 1, 3 |
$b | 2 |
ab | 1, 2, 3 |
ba | 1, 2, 3 |
bb | 1 |
3.2 $aaab, 2-grams $a, aa, aa, ab. Lookups: $a → {1, 3}, aa → none, ab → {1, 2, 3}. Counts: abba 2, bab 1, aba 2. Threshold , so candidates abba and aba. and : no word matches.
SuccessSolution
: replace f → p, insert h. , . Common: (3). Check with the lemma: .
SuccessSolution
- True. is a prefix of itself.
- False. , but .
- True. Padding only affects filter efficiency: more false candidates pass, so more PED computations.
- False. Right padding changes PED: .
Exam occurrences#
| Semester | Task | Points | Subtopics |
|---|---|---|---|
| DBIS Exam WS1314 (not published) | Task 2 | 20 | PED, comm2, proof for PED=1 |
| DBIS Exam WS1516 (not published) | Task 6 | 5 | 3-gram padding purpose |
| DBIS Exam WS1617 (not published) | Task 6 | 5 | 3-gram inverted list formula |
| DBIS Exam WS1718 (not published) | Task 3 | 20 | ED, PED, q-gram bound, valid_utf8_char |
| DBIS Exam WS1819 (not published) | Task 3 | 20 | 2-gram index, PED filtering, fuzzy_prefix_search |
| DBIS Exam WS2122 (not published) | Task 3 | 25 | SPARQL, PED function, PED proof |
| DBIS Exam WS2223 (not published) | Task 2 | 20 | ED(alfa,alpha), q-gram common count, one_delete |
| DBIS Exam WS2324 (not published) | Task 4 | 20 | 3-gram analysis, PED true/false, HTML forms |
| DBIS Exam WS2324-2 (not published) | Task 4 | 20 | 2-gram index with counts, PED true/false |
Lectures#
- DBIS_VL08 — fuzzy search, ED, PED, q-grams, q-gram lemma, q-gram index
- DBIS_VL13 — exam example task on q-grams and edit distance (WS 22/23, Task 2.2)
Related Topics#
- SPARQL and Knowledge Graphs — entity autocompletion in SPARQL editors is the motivating application; ES8 searches Wikidata entities
- Web Applications — ES9/ES10 put fuzzy prefix search behind a (search-as-you-type) web app
- Inverted Index and Ranking — the q-gram index is an inverted index over q-grams