Nodejs String Soundex soundexRefined()

Here you can find the source of soundexRefined()

Method Source Code

/**//from   ww w .j av a2s.c  om
 * 18 May 2008
 * Soundex is a phonetic algorithm for indexing names by sound, as pronounced in English.
 * 
 * Refined Sounder profiles the entire string (not just the first 4 constants) so is a
 * litle better geared towards spellchecking than phoneme matching.
 *  
 * This code has been ported to ECMAScript from the Apache Commons Codec project
 * and is redistributed under the terms of that license.
 *  - http://commons.apache.org/codec/
 *  - trunk/src/java/org/apache/commons/codec/language/RefinedSoundex.java (r651573)
 *  - http://www.apache.org/licenses/LICENSE-2.0
 *   
 * Please assume any errors found in the below code are translation errors
 * inserted by myself and not those of the original authors.
 * 
 * @author Matt Chadburn <matt@commuterjoy.co.uk> 
 */

// Mapping
// - 01360240043788015936020505
// - ABCDEFGHIJKLMNOPQRSTUVWXYZ

// array: emulate toCharArray
var US_ENGLISH_MAPPING = "01360240043788015936020505".split("");
var lookup = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");

/**
 * @return {String} A Refined Soundex coded string.
 */ 
String.prototype.soundexRefined = function(){

        if (this == null){ 
            return null;
        }

        var str = this.toUpperCase().replace(/[^A-Z]/g, "");

        if (this.length == 0) {
            return str;
        }

        var sBuf = new String();
        sBuf += (str.charAt(0));
      
      
        var last, current;
        last = '*';

        for (var i = 0; i < str.length; i++) {
         
         current = getMappingCode(str.charAt(i));
            
            if (current == last) {
                continue;
            } //else if (current != 0) { /// ?? TODO. I took this out as it was preventing the zeroes being output.
                sBuf += current;
            //}

            last = current;
        }

        return sBuf.toString();
    }


function getMappingCode(c) {

   for(var j = 0; j < lookup.length; j++){
      if ( lookup[j] == c ){
         return US_ENGLISH_MAPPING[j];
         }
      }
   return 0;
    }

Related

  1. soundex( )
    String.prototype.soundex = function( ){
      var i, j, r;
      var keyLength = 4; 
      var map = { B:1,F:1,P:1,V:1,C:2,G:2,J:2,K:2,Q:2,S:2,X:2,Z:2,D:3,T:3,L:4,M:5,N:5,R:6 };
      var r = (s = this.toUpperCase().replace(/[^A-Z]/g, "").split("")).splice(0, 1);
      var key = {
          items : [],
          _check: function(e) {
          var m = map[e]; 
    ...
    
  2. soundex()
    String.prototype.soundex = function()
      return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase()
        .replace(/\w/g, function($0)
          return String.prototype.soundex.table[$0];
        })
        .replace(/(.)\1+/g, '$1')
        .slice(0, 3)
    ...
    
  3. soundex(p)
    String.prototype.soundex = function(p){
      var i, j, l, r, p = isNaN(p) ? 4 : p > 10 ? 10 : p < 4 ? 4 : p,
      m = {BFPV: 1, CGJKQSXZ: 2, DT: 3, L: 4, MN: 5, R: 6},
      r = (s = this.toUpperCase().replace(/[^A-Z]/g, "").split("")).splice(0, 1);
      for(i = -1, l = s.length; ++i < l;)
        for(j in m)
          if(j.indexOf(s[i]) + 1 && r[r.length-1] != m[j] && r.push(m[j]))
            break;
      return r.length > p && (r.length = p), r.join("") + (new Array(p - r.length + 1)).join("0");
    ...
    
  4. soundex(p)
    String.prototype.soundex = function(p){ 
        var i, j, r, p = isNaN(p) ? 4 : p > 10 ? 10 : p < 4 ? 4 : p,
        m = {BFPV: 1, CGJKQSXZ: 2, DT: 3, L: 4, MN: 5, R: 6},
        r = (s = this.toUpperCase().replace(/[^A-Z]/g, "").split("")).splice(0, 1);
        for(i in s)
            for(j in m)
                if(j.indexOf(s[i]) + 1 && r[r.length-1] != m[j] && r.push(m[j]))
                    break;
        return r.length > p && (r.length = p), r.join("") + (new Array(p - r.length + 1)).join("0");
    ...
    
  5. soundex.init(table)
    String.prototype.soundex.init = function(table)
      String.prototype.soundex.table = {};
      table = table || {
        'bfpv': '1', 
        'cgjkqsxz': '2', 
        'dt': '3', 
        'l': '4', 
        'mn': '5', 
    ...