harper icon indicating copy to clipboard operation
harper copied to clipboard

Use FST as dictionary?

Open PSeitz opened this issue 2 years ago • 7 comments

Just wanted to ask if you considered using FST as the dictionary. It allows efficient fuzzy queries with levenshtein distance for spelling correction. Look ups are also quite fast, and it provides pretty good compression. Iteration would be slower than a vec, but not sure why you would need that with the features it provides.

FST dicitonary https://docs.rs/fst/latest/fst/ FST fuzzy queries https://docs.rs/levenshtein_automata/latest/levenshtein_automata/

PSeitz avatar Sep 05 '24 05:09 PSeitz

I did not consider using an FST, although it looks exceptionally cool. I'll toy around with it as soon as I have time. It should be pretty trivial to get it integrated into Harper (and hopefully speed up those spellcheck times).

Thanks for bringing it up!

elijah-potter avatar Sep 09 '24 00:09 elijah-potter

This would be very cool, but I started looking into this, and it doesn't look like a drop-in replacement to me. The biggest differences are these:

  1. Once constructed, a Map can never be modified.
  2. Maps must be constructed with lexicographically ordered byte sequences. There is no restricting on the ordering of values.

The existence of MergedDictionary might make this feasible.

Fuzzy finding would be nice, but the documentation has this to say:

Warning: Levenshtein automatons use a lot of memory

The construction of Levenshtein automatons should be consider “proof of concept” quality. Namely, they do just enough to be correct. But they haven’t had any effort put into them to be memory conscious.

Note that an error will be returned if a Levenshtein automaton gets too big (tens of MB in heap usage).

I'm not 100% sure how big of an issue this would be, but my understanding is that one of Harper's motivations is to have a small memory footprint (compared to LanguageTool).

grantlemons avatar Oct 06 '24 04:10 grantlemons

I'm going to do a proof of concept implementation regardless, and just see how everything works out.

grantlemons avatar Oct 06 '24 04:10 grantlemons

You probably should use https://crates.io/crates/levenshtein_automata for fuzzy search, as the fst one is broken. I don't think memory consumption will be an issue.

PSeitz avatar Oct 06 '24 05:10 PSeitz

Can you elaborate on what you mean by broken?

grantlemons avatar Oct 06 '24 16:10 grantlemons

https://github.com/BurntSushi/fst/issues/38

PSeitz avatar Oct 07 '24 00:10 PSeitz

From what I read there, it looks like it is only broken for japanese characters (or maybe complex unicode in general), but considering Harper only supports english for the forseable future, I'll just make a note of it and continue with Fst in my proof of concept.

grantlemons avatar Oct 07 '24 16:10 grantlemons