Study Vault Search notes, aliases and full text
Graph
ConceptDBISAI-written · not yet proofread

Q-Gram

Also Q-Grams

The q-grams of a string x are its substrings of length q; Q_q(x) denotes the multiset of all of them, which has |x| − q + 1 elements.

Formula#

∣Qq(x)∣=∣x∣−q+1(∣x∣≥q).|Q_q(x)| = |x| - q + 1 \qquad (|x| \ge q).

With q−1q - 1 padding characters on the left only: ∣Qq(x′′)∣=∣x∣|Q_q(x'')| = |x|. With q−1q - 1 on both sides: ∣Qq(x′)∣=∣x∣+q−1|Q_q(x')| = |x| + q - 1.

Properties#

  • Q-grams are a cheap proxy for string similarity: strings with a small Edit Distance agree “at many places” and therefore share many q-grams. The Q-Gram Lemma makes this precise (each edit operation affects at most qq q-grams).
  • Intersections and differences are taken as multisets, e.g. Q3(ababa)={aba,bab,aba}Q_3(\text{ababa}) = \{\text{aba}, \text{bab}, \text{aba}\} contains aba twice.
  • commq(x,y):=∣Qq(x)∩Qq(y)∣\mathrm{comm}_q(x, y) := |Q_q(x) \cap Q_q(y)| denotes the number of common q-grams.

Padding#

Before extracting q-grams, the strings are padded with q−1q - 1 copies of a special character $ that does not occur in the strings. Padding yields more q-grams, including distinctive ones for the word boundaries, so the q-gram filter rules out more non-matches; the filter result stays correct.

PaddingExample (q=3q = 3)UseWhy it is correct
q−1q-1 $ on both sides$$freiburg$$ED searchfor padded x′,y′x', y': ED(x,y)=ED(x′,y′)\mathrm{ED}(x, y) = \mathrm{ED}(x', y')
q−1q-1 $ on the left only$$freiburgPED (prefix) searchfor padded x′′,y′′x'', y'': PED(x′′,y′′)=PED(x,y)\mathrm{PED}(x'', y'') = \mathrm{PED}(x, y)
nonefreiburgbothstill correct, but fewer distinctive q-grams, weaker filter
right side (alone or with left)freiburg$$—invalid for PED: changes PED values

Example#

Q3(freiburg)={fre,rei,eib,ibu,bur,urg}Q_3(\text{freiburg}) = \{\text{fre}, \text{rei}, \text{eib}, \text{ibu}, \text{bur}, \text{urg}\}, 8−3+1=68 - 3 + 1 = 6 elements.

Left-padded (q=3q = 3): Q3($$freiburg)={$$f,$fr,fre,rei,eib,ibu,bur,urg}Q_3(\texttt{\$\$freiburg}) = \{\texttt{\$\$f}, \texttt{\$fr}, \text{fre}, \text{rei}, \text{eib}, \text{ibu}, \text{bur}, \text{urg}\}, 8=∣freiburg∣8 = |\text{freiburg}| elements.

Both sides (q=3q = 3, exam WS 22/23, Task 2.1): Q3($$alfa$$)={$$a,$al,alf,lfa,fa$,a$$}Q_3(\texttt{\$\$alfa\$\$}) = \{\texttt{\$\$a}, \texttt{\$al}, \text{alf}, \text{lfa}, \texttt{fa\$}, \texttt{a\$\$}\} and Q3($$alpha$$)={$$a,$al,alp,lph,pha,ha$,a$$}Q_3(\texttt{\$\$alpha\$\$}) = \{\texttt{\$\$a}, \texttt{\$al}, \text{alp}, \text{lph}, \text{pha}, \texttt{ha\$}, \texttt{a\$\$}\}, with 3 common q-grams {$$a,$al,a$$}\{\texttt{\$\$a}, \texttt{\$al}, \texttt{a\$\$}\}.

Sources#

Search

Type In
↑ ↓ move ↵ open ⇥ next section Ctrl↵ results page Prefix # to search text only