Custom Function difference() relative to the soundex() value (node)

Following for my previous post regarding a custom soundex() function this custom function extends soundex() to allow flexible match levels

The soundex() function returns a 4 digit representation of the word input. The difference function returns a value from 0 - 4 showing the quality of match between two words

So the names “Smith” and “Smythe” both evaluate to a soundex() representation of S530

The difference function in this case would return 4 indicating a full character match where “Smith” and “Smitherby” would return S530 and S531 with a difference value of three. No match returns 0
This allows for searching on much looser matches by searching with reference to a difference value

The function

// JavaScript Document
exports.difference = function (word1, word2, precoded) {

    return difference(word1, word2, precoded);
};

function difference(word1, word2, precoded) {

    let ct = 0;

    if (precoded != true) {

        word1 = soundex(word1, false);
        word2 = soundex(word2, false);

    }

    for (let i = 0, l = word1.length; i < l; i++) {
        if (word1[i] == word2[i]) {
            ct++
        };


    }

    return ct;
}
function soundex(name, extend) {
    let slen = extend ? 4 : 3;
    let s = [];
    let si = 1;
    let c;

    //              ABCDEFGHIJKLMNOPQRSTUVWXYZ
    let mappings = "01230120022455012623010202";

    s[0] = name[0].toUpperCase();

    for (let i = 1, l = name.length; i < l; i++) {
        c = (name[i].toUpperCase()).charCodeAt(0) - 65;

        if (c >= 0 && c <= 25) {
            if (mappings[c] != '0') {
                if (mappings[c] != s[si - 1]) {
                    s[si] = mappings[c];
                    si++;
                }

                if (si > slen) {
                    break;
                }
            }
        }
    }

    if (si <= slen) {
        while (si <= slen) {
            s[si] = '0';
            si++;
        }
    }

    return s.join("");
}

The hjson


{
    type: 'method_difference',
    groupTitle : 'Custom Formatters',
    groupIcon : 'fa fa-lg fa-key',
    addTitle: 'Soundex difference',
    title : 'Soundex difference',
    icon : 'fa fa-lg fa-font',
    state : 'opened',
    help : 'Difference between Soundex of two stings',
    properties : [
    {
      group: 'difference Properties',
      variables: [
        { name: 'word2', optionName: 'word2', title: 'word2', 
          type: 'text', required: true, defaultValue: ''},
          {name: 'precoded', optionName: 'precoded', title: 'Input is Pre-soundex coded', 
          type: 'boolean', required: false, defaultValue: 'false'}
 
    
      ]
      
    }

The default is that the words passed are soundex() encoded before comparison.
A checkbox can be set to indicate that the inputs are already soundex() encoded and in the case are passed through to the difference() function without being passed for soundex() coding prior.

1 Like