Fuzzysearch: Tiny, fast fuzzy searching for JavaScript
github.com
github.com
For instance, finding an "ok" image from this location only requires to type "ok": https://thefiletree.com/lib/.
(Note that it has scoring optimized for paths, which the library doesn't have; slashes have special meanings in paths.)
Thus:
M[0,j] = M[i,0] = 0
M[i,j] = max(0,
M[i-1,j-1] + (needle[i] == haystack[j]),
M[i-1,j] + 1,
M[i,j-1] + 1)
Additionally, store the highest value x and its position (i,j). When you've got the full matrix, return (x,i,j). In Bioinformatics, this is known as the Smith-Waterman algorithm [2] and the result would satisfy the requirements of fuzzy substring matching.[1] https://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93P... [2] https://en.wikipedia.org/wiki/Smith%E2%80%93Waterman_algorit...
> var nch = needle.charCodeAt(i);
> while (j < hlen) {
> if (haystack.charCodeAt(j++) === nch) {
> continue outer;
> }
> }
Oh my god, you can label loops?
break $n;
isn't valid since PHP 5.4
I wonder what else is in the JS syntax that I've been missing out on!
function fuzzysearch(needle, haystack) {
var hlen = haystack.length;
var nlen = needle.length;
if (nlen > hlen) return false;
if (nlen === hlen) return needle === haystack;
for (var i = 0, j = 0; i < nlen; ++i, ++j) {
while (j < hlen && needle.charCodeAt(i) !== haystack.charCodeAt(j)) ++j;
if (j === hlen) return false;
}
return true;
}To be honest reasoning about the loop with continue is actually easier than about one without.
Like this
Or rather outer: for (var i = 0, j = 0; i < nlen; i++) {
var nch = needle.charCodeAt(i);
while (j < hlen) {
if (haystack.charCodeAt(j++) === nch) {
continue outer;
}
}
return false;
}> function fuzzysearch(r,e){var n=e.length,t=r.length;if(t>n)return!1;if(t===n)return r===e;r:for(var f=0,u=0;t>f;f++){for(var a=r.charCodeAt(f);n>u;)if(e.charCodeAt(u++)===a)continue r;return!1}return!0}
I mean.. come on, guys. This is getting absurd..
I honestly wonder why you believe that.
Efficient algorithms are hard, even when their implementation is small. Having a good algorithm as a library lets you rely on it instead of writing your own.
Libraries shouldn't have the requirement to be bloated.
The popularity of my projects appear to be nearly inversely proportional to their amount of complexity and sophistication. Things I've been refining over 5 years like (https://github.com/kristopolous/EvDa) has a userbase of 1, while my occasionally evening hacks have significantly more traction.
At least for me, working long and hard on things I believe are of value and writing tests, dogfooding, and documentation basically means it's just going to be used by me ... bizarre but true.
Think about Linux: it's all about lots of small applications all doing one small thing each.
How is JS different?
This is simplicity itself to code up. Here's quick and dirty python one, for example, that would get you started. It's so simple that I'm pretty sure it works, even though I haven't tested it.
def get_suggestions(xs,str):
"""return list of elements in XS that match STR, the match string"""
suggestions=xs[:]
for part in str.split(): suggestions=[x for x in suggestions if part in x]
return suggestions
This is also (or so I think) better to use. As a user you get a lot more control over what you're finding, and you don't have to think very hard about what chars to add in to eliminate items you don't want. So it's very likely you'll be able to quickly winnow your list down to 1 item.And because it doesn't have any complicated workings inside it, it can be explained even to non-technical users, who can make good use of it.
http://www.blueskyonmars.com/2013/03/26/brackets-quick-open-...
[1] https://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance
[1] http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matchi... [2] https://github.com/seatgeek/fuzzywuzzy