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

Add fuzzy prefixing tree walking #23

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mish15
Copy link

@mish15 mish15 commented Nov 25, 2018

This allows trees to be walked with non-exact prefixes. The Levenshtein distance is used to determine how far the input can deviate, which is highly useful for tasks such as autocompletion or suggestions where the spelling is often not quite correct

Copy link
Member

@banks banks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really interesting idea! I’m impressed with how little code it needed but have a few questions from a quick look through:

How well does it work?

It looks like it will do a fuzzy match, but fuzzy matches in a non-trivial corpus quickly lead to a huge amount of noise which then needs at the very least a basic scoring mechanism to make sure the best (by some definition) are shown first.

As far as I can see (please correct if wrong) there is no concept of scoring here so if you fuzzy match with distance threshold 3 the results for “rabix” would deliver “racer” before “radix”.

It looks like since DistFn is pluggable and can modify the value returned you can custom score by emitting for example a tuple (value, score) and then the WalkFn could collect those into a container and sort by score. Is this the intended use? A doc example showing that would be a great addition.

Have you used this in a real case? Did it work well? What was corpus size like?

@@ -3,13 +3,19 @@ package iradix
import (
"bytes"
"sort"

"code.sajari.com/go-immutable-radix/pkg/levenshtein"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a goimports fail - shouldn’t it be importing the levenstein sub package also added here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"code.sajari.com/go-immutable-radix/pkg/levenshtein"
"github.com/hashicorp/go-immutable-radix/pkg/levenshtein"

Yeah this is my bad. I'd had a few beers and sent the PR a bit prematurely by accident.

})
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good to me at a glance but test cases to show the behaviour and edge cases seem important!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah i'm wondering how best to do this? The surface changes are pretty simple. Most of the complexity is in the levenshtein pkg, which is where i focused on the tests. Anyhow happy to add, let me know your thoughts?

@mish15
Copy link
Author

mish15 commented Dec 7, 2018

@banks yes you are correct. The distance function is pluggable as the leaves only act on the v interface{}, so the scoring methodology is up to the implementation, but still fits neatly into the existing api.

I have used this with around 250,000 items in the tree. For this the existing walk iterator takes ~250usec, the fuzzy walk is ~2msec, roughly 10x slower. Our scoring function is pretty complex though, we basically keep a top X based on a combination of the popularity of the key and the probability of the mistake/s made. There's lots of ways to do this, e.g. https://web.stanford.edu/class/cs124/lec/spelling.pdf but in practice even just using the probability of the key works pretty well, but it's better if you can properly penalise mistakes which is part of the reason to pass through the distance.

@hashicorp-cla
Copy link

hashicorp-cla commented Mar 12, 2022

CLA assistant check
All committers have signed the CLA.

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.

3 participants