A tiny lightweight library for Fuzzy Search.
I made this library as a result of learning about Levenshtein Distance algorithm to calculate minimum number of single-character edits (insertions, deletions or substitutions) required to transform one word to another by Vladimir Levenshtein.
Note
Note: The library is at a very early stage, if you want to help improve it, please open an issue.
with yarn
yarn add fuzzify
with npm
npm install fuzzify
import Fuzzy from "fuzzify";
const countries = [
"Australia",
"France",
"Germany",
"Hungary",
"Iceland",
"India",
"Israel",
"Italy",
"Japan",
"Malawi",
"Malaysia",
"Maldives",
];
const fuzzy = new Fuzzy(countries);
const query = "ala";
const results = fuzzy.search(query);
console.log("RESULTS", results);
The search
API gives approximate matched strings with the passed query in the below format.
Attributes | Description |
---|---|
text | The target string against which query is matched |
distance | The minimum number of edits (Insertion / Deletion / Substitutions) required to transform the query to target text. |
[
{
text: "Malawi",
distance: 3,
},
{
text: "Malaysia",
distance: 5,
},
{
text: "Australia",
distance: 6,
},
{
text: "Italy",
distance: 3,
},
{
text: "Japan",
distance: 3,
},
{
text: "Iceland",
distance: 5,
},
{
text: "Maldives",
distance: 6,
},
{
text: "Israel",
distance: 5,
},
{
text: "India",
distance: 4,
},
{
text: "France",
distance: 5,
},
{
text: "Germany",
distance: 6,
},
{
text: "Hungary",
distance: 6,
},
];
includeMatches
- Determines whether the indices
at which characters matche should be returned in the response. Each match
element consists of two indices -
- The index of query string where match is found.
- The index of target string where a match is found.
Example π
query = "ala", target string = "Australia"
matches: [
[0, 5],
[1, 6],
[2, 8],
],
In the above example π matches are found
- character
a
at0th
index inala
matches with characatera
at5th
index inAustralia
- character
l
at1st
index inala
matches with characatera
at6th
index inAustralia
- character
a
at2nd
index inala
matches with characatera
at8th
index inAustralia
The complete response would be π
[
{
text: "Malawi",
distance: 3,
matches: [
[0, 1],
[1, 2],
[2, 3],
],
},
{
text: "Malaysia",
distance: 5,
matches: [
[0, 1],
[1, 2],
[2, 7],
],
},
{
text: "Australia",
distance: 6,
matches: [
[0, 5],
[1, 6],
[2, 8],
],
},
{
text: "Italy",
distance: 3,
matches: [
[0, 2],
[1, 3],
],
},
{
text: "Japan",
distance: 3,
matches: [
[0, 1],
[2, 3],
],
},
{
text: "Iceland",
distance: 5,
matches: [
[1, 3],
[2, 4],
],
},
{
text: "Maldives",
distance: 6,
matches: [
[0, 1],
[1, 2],
],
},
{
text: "Israel",
distance: 5,
matches: [
[0, 3],
[1, 5],
],
},
{
text: "India",
distance: 4,
matches: [[2, 4]],
},
{
text: "France",
distance: 5,
matches: [[2, 2]],
},
{
text: "Germany",
distance: 6,
matches: [[2, 4]],
},
{
text: "Hungary",
distance: 6,
matches: [[2, 4]],
},
];
You can check the demo here.
Install packages:
yarn
Start development playground:
yarn start
Build command:
yarn build
Please open an issue so we can start discussing. Any help to improve the library is most welcome :).