User:Jeblad/Detect text segments with a convolving hash

Detecting text segments with a convolving hash is an attempt to describe an algorithm for text synchronization.

[Should add something about the hashing at each index leading to invariance on editing.]

It is easier to see whats going on in the basic algorithm than the more convolved approaches.

Basic algorithm
Assume there is a text represented as a vector of chars (or codepoints) $$\mathbf{a}$$ or a heystack string, and a shorter segment $$\mathbf{b}$$ or a needle string. Also assume the wanted needle digest comes from the smaller segment and is $$\mathbf{\hat{b}}$$, and the heystack has the digest $$\mathbf{\hat{a}}$$. These digests are vectors, but can be reprocessed into binary numbers. For simplicity call the unprocessed digests for a needle vector and a heystack vector, and likewise the truncated version for a needle digest and a heystack digest. It is easiest to assume that we have some function that truncates the elements in the vector into a binary digest, thus we can forget the distinction.

Calculate the needle digest as

\begin{array}{lcl} \mathbf{foreach}~\mbox{char}~i~\mbox{in}~\mathbf{b}~\mathbf{do} \\ \quad j \leftarrow \operatorname{hash} \left ( \mathbf{b} \left [ i-\delta, i+\delta \right ] \right ) \\ \quad \hat{b}_{j} \leftarrow \hat{b}_{j} + 1 \end{array} $$

Out of this there will be a needle vector $$\mathbf{\hat{b}}$$

In the previous there is a hash function $$\operatorname{hash}(\cdot)$$ that takes a short string of chars (or codepoints) and folds it into an alternate range. Often something like Pearson hashing is used as speed is crucial, while ideal properties are unimportant, and then a precomputed lookup table can be used instead of a modulus operations. The alternate range is the length of the wanted digest, or a vector in this case.

Calculate the heystack digest for a window as

\mathbf{foreach}~\mbox{char}~i+k~\mbox{in}~A~\mathbf{do}~Y_{\operatorname{h}(A_{i+k})} \leftarrow Y_{\operatorname{h}(A_{i+k})} + 1 $$

Compare $$\mathbf{X}$$ and $$\mathbf{Y}$$ with some distance metric and sum the distances

\mathbf{foreach}~\mbox{element}~j~\mbox{in}~\mathbf{X}~\mathbf{do}~z_{k} \leftarrow \operatorname{d} \left ( X_{j} - Y_{j} \right ) $$

Each of the elements in $$\mathbf{z}$$ will now hold the accumulated distance for a specific offset $$k$$ into the text $$A$$.

Adjust the distance metric
There are a lot of kernels for doing correlation, and as the digest should be a binary string it could simply sum elements from the heystack $$\mathbf{Y}$$ where the sign is inverted for zeroes in the needle $$\mathbf{X}$$

\mathbf{foreach}~\mbox{element}~j~\mbox{in}~\mathbf{X}~\mathbf{do}~z_{k} \leftarrow z_{k} + \begin{cases} Y_{j}, & \mbox{if}~X_{j} == 1 \\ -Y_{j}, & \mbox{if}~X_{j} == 0 \end{cases} $$

This can be calculated a lot more efficient as a summation over two masked vectors, although more efficient solutions exists.

Avoid repeating calculations
During the convolution a lot of hashing operations are redone again and again. This creates a lot of unnecessary load. Instead the hashed values can be updated in place. To do this there is a leading action and a trailing action. Those do in place updates according to the needle digest

\begin{array}{lcl} \mathbf{foreach}~\mbox{char}~i~\mbox{in}~B~\mathbf{do} \\ \quad j \leftarrow \operatorname{h}(B_{i}) \\ \quad z_{j} \leftarrow z_{j} + \begin{cases} +1, & \mbox{if}~X_{j} == 1 \\ -1, & \mbox{if}~X_{j} == 0 \end{cases} \\ \quad j \leftarrow \operatorname{h}(B_{i+l}) \\ \quad z_{j} \leftarrow z_{j} + \begin{cases} +1, & \mbox{if}~X_{j} == 0 \\ -1, & \mbox{if}~X_{j} == 1 \end{cases} \end{array} $$

This assumes the $$\mathbf{x}$$ digest is precomputed.

Out of this there will be a vector with correlation coefficients for each code point in the text.