Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lyra): compute levenshtein metric within a given tolerance #131

Merged
merged 2 commits into from
Sep 12, 2022

Conversation

jkomyno
Copy link
Contributor

@jkomyno jkomyno commented Sep 11, 2022

As discussed privately, I have noticed that the levenshtein distance is currently only used as a comparison metric to check whether two strings have edit distance less than a given tolerance threshold.

Computing the levenshtein metric in the general case takes time O(max(m, n)) and space O(max(m, n)), where m and n is the length of the two input strings. When we're only interested in retrieving the exact edit distance when it's below or equal to a given tolerance threshold, more time-efficient methods exist.

Given

let term: string;
let word: string;
let tolerance: number; // small, non-negative integer

this PR turns the comparison

if (levenshtein(term, word) <= tolerance) {
  // ...
}

into the following:

if (boundedLevenshtein(term, word, tolerance).isBounded) {
  // ...
}

The core algorithm this PR takes inspiration from has already been developed in the talisman project, which is MIT licensed.

It'd be interested to see how the changes introduced by this PR will influence the benchmarks. I expect a marginal performance increase as the size of the inputs grows.

Alternative not considered in this PR

As the size of the input scales, computing an exact comparison metric may not be needed. Luckily, the literature offers a plethora of polylogarithmic approximation algorithms to choose from. Should you be interested in this slightly less precise alternative, I'd suggest to take a look at Andoni et al.'s method.

@micheleriva micheleriva added the benchmark This label will trigger the Benchmark Comparisson workflow label Sep 12, 2022
@micheleriva
Copy link
Member

Hi @jkomyno,
Thank you so much for this PR, this is brilliant!

About the benchmarks, I forgot we removed the benchmark script from this repo, so I'll run them manually and update you on the results

@micheleriva micheleriva removed the benchmark This label will trigger the Benchmark Comparisson workflow label Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants