UNPKG

cmpstr

Version:

CmpStr is a lightweight, fast and well performing package for calculating string similarity

113 lines (109 loc) 3.06 kB
// CmpStr v3.0.1 dev-052fa0c-250614 by Paul Köhler @komed3 / MIT License 'use strict'; var Phonetic = require('./Phonetic.cjs'); /** * Cologne Phonetic Algorithm * src/phonetic/Cologne.ts * * @see https://en.wikipedia.org/wiki/Cologne_phonetics * * Cologne phonetics, also known as `Kölner Phonetik` or the `Cologne process`, * is a phonetic algorithm that assigns a sequence of digits, referred to as the * phonetic code, to words. The purpose of this method is to ensure that words * with identical sounds receive the same code. This algorithm can facilitate a * similarity search among words. * * Cologne phonetics is associated with the well-known Soundex phonetic algorithm, * yet it is specifically optimized for the German language. This algorithm was * introduced by Hans Joachim Postel in 1969. * * The Cologne phonetic algorithm works by mapping letters to digits, ignoring * certain letters, and applying specific rules to handle character combinations. * * @module Phonetic/Cologne * @author Paul Köhler (komed3) * @license MIT */ /** * Cologne class extends the Phonetic class to implement the Cologne phonetic algorithm. */ class Cologne extends Phonetic.Phonetic { // Default options for the Cologne phonetic algorithm static default = { map: 'default', delimiter: ' ', length: -1, dedupe: true }; /** * Constructor for the Cologne class. * * Initializes the Cologne phonetic algorithm with the mapping and options. * * @param {PhoneticOptions} [opt] - Options for the Cologne phonetic algorithm */ constructor(opt = {}) { super('cologne', opt); } /** * Adjusts the phonetic code by removing all '0's except the first character. * * @param {string} code - The phonetic code to adjust * @returns {string} - The adjusted phonetic code */ adjustCode(code) { return code.slice(0, 1) + code.slice(1).replaceAll('0', ''); } } // Register the Cologne algorithm in the phonetic registry Phonetic.PhoneticRegistry.add('cologne', Cologne); // Register the Cologne phonetic mapping Phonetic.PhoneticMappingRegistry.add('cologne', 'default', { map: { a: '0', ä: '0', e: '0', i: '0', j: '0', o: '0', ö: '0', u: '0', ü: '0', y: '0', b: '1', p: '1', d: '2', t: '2', f: '3', v: '3', w: '3', g: '4', k: '4', q: '4', l: '5', m: '6', n: '6', r: '7', c: '8', s: '8', ß: '8', z: '8', x: '48' }, ignore: ['h'], ruleset: [ { char: 'p', next: ['h'], code: '3' }, { char: 'c', position: 'start', next: ['a', 'h', 'k', 'l', 'o', 'q', 'r', 'u', 'x'], code: '4' }, { char: 'c', next: ['a', 'h', 'k', 'o', 'q', 'u', 'x'], prevNot: ['s', 'z'], code: '4' }, { char: 'd', next: ['c', 's', 'z'], code: '8' }, { char: 't', next: ['c', 's', 'z'], code: '8' }, { char: 'x', prev: ['c', 'k', 'q'], code: '8' } ] }); exports.Cologne = Cologne; //# sourceMappingURL=Cologne.cjs.map