2

I have a string akstr = My name is khan and I want to know that does akstr contains My name which I can do easily but if I want to check does akstr contains My nama with little mistake in spelling and I want True as output. Could it be done using javascript?

rs4
  • 81
  • 7

2 Answers2

1

Provided you use node.js, you can use the npm package natural. It is used for natural language processing applications. It has a set of methods for calculating String Distances. Meaning that My name is 94% equal to My nama. You can create your fuzzy algorithm based on that. An example:

const natural = require('natural');
let distance = natural.JaroWinklerDistance("My name", "My nama");
console.log(distance);

prints 0.9428571428571428

You may find other intersting stuff in it too, like spell checking and Approximate String Matching.

With just javascript I wrote a simple fuzzy contains method with three inputs. The first is the full string, the second, the substring and the third the allowed error. In this case with an error 2, you allow 2 characters to be different for the substring. With 0 you get the normal contains method. You can also change the way the error is calculated (maybe a percentage based on the substring length). I used the code for the levenstein method from here: https://gist.github.com/andrei-m/982927

function levenstein(a, b) {
    var m = [], i, j, min = Math.min;

    if (!(a && b)) return (b || a).length;

    for (i = 0; i <= b.length; m[i] = [i++]);
    for (j = 0; j <= a.length; m[0][j] = j++);

    for (i = 1; i <= b.length; i++) {
        for (j = 1; j <= a.length; j++) {
            m[i][j] = b.charAt(i - 1) == a.charAt(j - 1)
                ? m[i - 1][j - 1]
                : m[i][j] = min(
                    m[i - 1][j - 1] + 1, 
                    min(m[i][j - 1] + 1, m[i - 1 ][j] + 1))
        }
    }

    return m[b.length][a.length];
}
function fuzzyContains(a, b, error) {
    var matchLength = a.length - b.length;
    var distanceToMatch = levenstein(a, b) - matchLength;
    if(distanceToMatch - error > 0) {
      return false;
    } else {
      return true;
    }
}
console.log(fuzzyContains("hello world entire", "worlf", 1))
Giannis Mp
  • 1,291
  • 6
  • 13
  • I'm not using node.js – rs4 Jul 10 '18 at 10:41
  • https://github.com/thsig/jaro-winkler-JS check this if you want, it provides the same string distance functionality. There are other implementation in the internet too. – Giannis Mp Jul 10 '18 at 10:44
  • This is for comparing two strings. I want to check if a string contains a substring with fuzziness. – rs4 Jul 10 '18 at 10:53
  • You said that you can do the contain part easily and your problem is in spelling mistakes – Giannis Mp Jul 10 '18 at 11:27
  • I said because in javascript I can use `includes` to check if string contains a substring but if there is a spelling mistake I will not work since `includes` checks letter by letter. – rs4 Jul 10 '18 at 11:46
0

You can compare the String, like

My Name My Nama

is 90% matches so you can return true.

You will get more idea from the following link

Compare Strings Javascript Return %of Likely

Parshav Shah
  • 203
  • 2
  • 9
  • And how would I know that with what substring I would compare small string. Instead of `My nama` it could `iss khana` or `name ik` or `my name khan`? – rs4 Jul 10 '18 at 10:41
  • 1st param can be Your database value and second can you input value – Parshav Shah Jul 11 '18 at 16:51