Fuzzy search lib for ionic

Hello,

Anybody know a fuzzy search library like Fuse.js compatible with ionic2 ?

Thx

This is an old thread, but its a useful one to have a solution for. I just had the same need.

Anyone using ā€œspeech recognitionā€ in their app to match to known tags will likely need to use both ionic/cordovaā€™s speech recognition plugin followed by fuzzy match. Iā€™ve often found the first match returned by the speech recognition service is not the right one Iā€™d expect and a match lower in the returned array is ā€˜closerā€™ to my speech, but programatically there is no way for me to select one over the other unless I do a fuzzy match.

The latest fuse.js (well, since Dec 2016, it seems) is compatible with typescript and includes typescript bindings.

Usage:
I have an array of markers that ā€˜markā€™ specific events. I just added speech to text using Ionic SpeechRecognition - folks can speak out the markers instead of tapping buttons. As it turns out, fuzzy search added much more accuracy to just plain speech to text

Marker array:

markers = [
    //  good markers
    { name: 'turn', val: 'turn', color: 'light' , speech: 'turn'},
    { name: 'brake', val: 'brake', color: 'light', speech: 'brake' },
    { name: 'unknown', val: 'unknown', color: 'light', speech: 'unknown' },
    // bad markers
    { name: 'brake', val: 'hard-brake', color: 'alert', speech: 'hard brake' },
    { name: 'distract', val: 'distract', color: 'alert', speech: 'distract' },
    { name: 'speedup', val: 'speedup', color: 'alert', speech: 'speed up' },
    { name: 'turn', val: 'sharp-turn', color: 'alert' , speech: 'sharp turn'},
  ]

So letā€™s say I need to match what the user says to the values in the ā€˜speechā€™ key of the marker array. Here is how I use fuse.js with the returned speech text:

npm install fuse.js --save

In index.html:

  <script src="node_modules/fuse.js/dist/fuse.min.js"> </script>

In the .ts file I need to implement fuzzy search:

import * as Fuse from 'fuse.js';
 myFuseOptions: Fuse.FuseOptions = {
    caseSensitive: false,
    keys: ['speech'],
    shouldSort: true,
};
 myFuse = new Fuse(this.markers, this.myFuseOptions);

After you get your speech converted to text:

let fuzzy = this.myFuse.search(s); // s = speech to text string
if (!fuzzy.length) {
      console.log ("Fuzzy search failed, going with manual sanitization");
     // whatever I need to do
    }
    else {
      s = fuzzy[0]["val"];
      console.log ("Fuzzy returned: "+s);
    }

3 Likes