cal-phonetic
Version:
CAL code to Latin phonetic transliteration
117 lines (106 loc) • 3.27 kB
JavaScript
/**
* @file CAL code to Latin phonetic transliteration
* @version 1.0.3
* @author Greg Borota
* @copyright (c) 2017 Greg Borota.
* @license MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// https://peshitta.github.io
// https://sedra.bethmardutho.org/about/fonts
// http://cal1.cn.huc.edu/searching/fullbrowser.html
import { Mapper, Writing } from 'aramaic-mapper';
import { allConsonants, isDiacritic, vowels } from 'cal-code-util';
import { begadkepatMap, commonVowels, consonants, easternVowels } from 'phonetic-code-util';
/** @module calPhonetic */
/**
* @private
* CAL source writing
* @const
* @type { Writing }
*/
var calWriting = new Writing(allConsonants, vowels);
/**
* @private
* Latin phonetic destination writing
* @const
* @type { Writing }
*/
var phoneticWriting = new Writing(
Object.freeze(consonants.concat('p', 'ś')), // + Palestinian P and Hebrew Shin
Object.freeze(commonVowels.concat(easternVowels))
);
/**
* @private
* Customized mapping callback
* @param { string } word input word
* @param { number } i current index in the word
* @param { Object.<string, string> } fromTo mapping dictionary
* @returns { string } Latin mapped char
*/
var callback = function (word, i, fromTo) {
var c = word.charAt(i);
// Alap is silent always in present day pronunciation.
// Diacritics were processed already in prior iteration.
if (c === ')' || isDiacritic(c)) {
return '';
}
var n = word.charAt(i + 1);
var m;
switch (n) {
case ',':
m = begadkepatMap[c];
if (m) {
return m;
}
break;
case '_':
return '';
case 'i':
case 'e':
if (c === 'y') {
return '';
}
break;
case 'u':
case 'O':
if (c === 'w') {
return '';
}
break;
default:
break;
}
return fromTo[c] || c;
};
/**
* Phonetic Mapper
* @const
* @type { Mapper }
*/
var mapper = new Mapper(calWriting, phoneticWriting, callback);
/**
* Convert from CAL to Phonetic Latin
* @static
* @param {string} word input word in CAL code transliteration
* @returns {string} the input word converted to phonetic Latin
*/
var toPhonetic = function (word) { return mapper.map(word); };
export { mapper, toPhonetic };