cmpstr
Version:
CmpStr is a lightweight, fast and well performing package for calculating string similarity
100 lines (96 loc) • 2.79 kB
JavaScript
// CmpStr v3.2.2 build-bb61120-260311 by Paul Köhler @komed3 / MIT License
'use strict';
var Phonetic = require('./Phonetic.cjs');
class Metaphone extends Phonetic.Phonetic {
static REGEX = { adjacent: /([A-BD-Z])\1+/gi, vowel: /[AEIOU]/g };
static default = {
map: 'en90',
delimiter: ' ',
length: -1,
pad: '',
dedupe: false
};
constructor(opt = {}) {
super('metaphone', opt);
}
encode(word) {
word = word.replace(Metaphone.REGEX.adjacent, (m, c) =>
c === 'C' ? m : c
);
return super.encode(word);
}
adjustCode(code) {
return code.slice(0, 1) + code.slice(1).replace(Metaphone.REGEX.vowel, '');
}
}
Phonetic.PhoneticRegistry.add('metaphone', Metaphone);
Phonetic.PhoneticMappingRegistry.add('metaphone', 'en90', {
map: {
a: 'A',
b: 'B',
c: 'K',
d: 'T',
e: 'E',
f: 'F',
g: 'K',
h: 'H',
i: 'I',
j: 'J',
k: 'K',
l: 'L',
m: 'M',
n: 'N',
o: 'O',
p: 'P',
q: 'K',
r: 'R',
s: 'S',
t: 'T',
u: 'U',
v: 'F',
w: 'W',
x: 'KS',
y: 'Y',
z: 'S'
},
ruleset: [
{ char: 'a', position: 'start', next: ['e'], code: '' },
{ char: 'g', position: 'start', next: ['n'], code: '' },
{ char: 'k', position: 'start', next: ['n'], code: '' },
{ char: 'p', position: 'start', next: ['n'], code: '' },
{ char: 'w', position: 'start', next: ['r'], code: '' },
{ char: 'b', position: 'end', prev: ['m'], code: '' },
{ char: 'c', next: ['h'], prevNot: ['s'], code: 'X' },
{ char: 'c', next: ['i'], next2: ['a'], code: 'X' },
{ char: 'c', next: ['e', 'i', 'y'], code: 'S' },
{ char: 'd', next: ['g'], next2: ['e', 'i', 'y'], code: 'J' },
{
char: 'g',
next: ['h'],
next2Not: ['', 'a', 'e', 'i', 'o', 'u'],
code: ''
},
{ char: 'g', trailing: 'n', code: '' },
{ char: 'g', trailing: 'ned', code: '' },
{ char: 'g', next: ['e', 'i', 'y'], prevNot: ['g'], code: 'J' },
{
char: 'h',
prev: ['a', 'e', 'i', 'o', 'u'],
nextNot: ['a', 'e', 'i', 'o', 'u'],
code: ''
},
{ char: 'h', prev: ['c', 'g', 'p', 's', 't'], code: '' },
{ char: 'k', prev: ['c'], code: '' },
{ char: 'p', next: ['h'], code: 'F' },
{ char: 's', next: ['h'], code: 'X' },
{ char: 's', next: ['i'], next2: ['a', 'o'], code: 'X' },
{ char: 't', next: ['i'], next2: ['a', 'o'], code: 'X' },
{ char: 't', next: ['h'], code: '0' },
{ char: 't', next: ['c'], next2: ['h'], code: '' },
{ char: 'w', nextNot: ['a', 'e', 'i', 'o', 'u'], code: '' },
{ char: 'h', leading: 'w', code: '' },
{ char: 'x', position: 'start', code: 'S' },
{ char: 'y', nextNot: ['a', 'e', 'i', 'o', 'u'], code: '' }
]
});
exports.Metaphone = Metaphone;