5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

スプレッドシート上でのひらがなからローマ字への変換

Posted at

ひらがなからローマ字への変換

transversion.js
 function kana2romaji(string) {

    var arrayedString = string.split('');
    var value = '';

    var triTable = {
        'う゛ぁ': 'va','う゛ぃ' : 'vi', 'う゛ぅ' : 'vu','う゛ぇ' : 've', 'う゛ぉ' : 'vo'
    };

    var biTable = {
        'きゃ' : 'kya','きゅ' : 'kyu','きょ' : 'kyo',
        'ぎゃ' : 'gya','ぎゅ' : 'gyu','ぎょ' : 'gyo',
        'しゃ' : 'sha','しゅ' : 'shu','しょ' : 'sho',
        'じゃ' : 'ja','じゅ' : 'ju','じょ' : 'jo',
        'ちゃ' : 'cha','ちゅ' : 'chu','ちょ' : 'cho',
        'ぢゃ' : 'dya','ぢゅ' : 'dyu','ぢょ' : 'dyo',
        'でゃ' : 'dha','でゅ' : 'dhu','でょ' : 'dho',
        'にゃ' : 'nya','にゅ' : 'nyu','にょ' : 'nyo',
        'ひゃ' : 'hya','ひゅ' : 'hyu','ひょ' : 'hyo',
        'びゃ' : 'pya','びゅ' : 'byu','びょ' : 'byo',
        'みゃ' : 'mya','みゅ' : 'myu','みょ' : 'myo',
        'りゃ' : 'rya','りゅ' : 'ryu','りょ' : 'ryo',
        'てぁ' : 'tha','てぃ' : 'thi','てぇ' : 'tee',
        'う゛' : 'vu','あ゛' : 'a"',
        'っか' : 'kka','っき' : 'kki','っく' : 'kku','っけ' : 'kke','っこ' : 'kko',
        'っさ' : 'ssa','っし' : 'sshi','っす' : 'ssu','っせ' : 'sse','っそ' : 'sso',
        'った' : 'tta','っち' : 'cchi','っつ' : 'ttu','って' : 'tte','っと' : 'tto',
        'っな' : 'nna','っに' : 'nni','っぬ' : 'nnu','っね' : 'nne','っの' : 'nno',
        'っは' : 'hha','っひ' : 'hhi','っふ' : 'ffu','っへ' : 'hhe','っほ' : 'hho',
        'っま' : 'mma','っみ' : 'mmi','っむ' : 'mmu','っめ' : 'mme','っも' : 'mmo',
        'っや' : 'yya', 'っゆ' : 'yyu','っよ' : 'yyo',
        'っら' : 'rra','っり' : 'rri','っる' : 'rru','っれ' : 'rre','っろ' : 'rro',
        'っわ' : 'wwa',
        'っが' : 'gga','っぎ' : 'ggi','っぐ' : 'ggu','っげ' : 'gge','っご' : 'ggo',
        'っざ' : 'zza','っじ' : 'jji','っず' : 'zzu','っぜ' : 'zze','っぞ' : 'zzo',
        'っだ' : 'dda','っぢ' : 'ddi','っづ' : 'ddu','っで' : 'dde','っど' : 'ddo',
        'っば' : 'bba','っび' : 'bbi','っぶ' : 'bbu','っべ' : 'bbe','っぼ' : 'bbo',
        'っぱ' : 'ppa','っぴ' : 'ppi','っぷ' : 'ppu','っぺ' : 'ppe','っぽ' : 'ppo'

    };

    var uniTable = {
        '' : 'a','' : 'i','' : 'u','' : 'e','' : 'o',
        '' : 'ka','' : 'ki','' : 'ku','' : 'ke','' : 'ko',
        '' : 'sa','' : 'shi','' : 'su','' : 'se','' : 'so',
        '' : 'ta','' : 'chi','' : 'tsu','' : 'te','' : 'to',
        '' : 'na','' : 'ni','' : 'nu','' : 'ne','' : 'no',
        '' : 'ha','' : 'hi','' : 'fu','' : 'he','' : 'ho',
        '' : 'ma','' : 'mi','' : 'mu','' : 'me','' : 'mo',
        '' : 'ya','' : 'yu','' : 'yo',
        '' : 'ra','' : 'ri','' : 'ru','' : 're','' : 'ro',
        '' : 'wa','' : 'wo','' : 'n',
        '' : 'ga','' : 'gi','' : 'gu','' : 'ge','' : 'go',
        '' : 'za','' : 'ji','' : 'zu','' : 'ze','' : 'zo',
        '' : 'da','' : 'di','' : 'du','' : 'de','' : 'do',
        '' : 'ba','' : 'bi','' : 'bu','' : 'be','' : 'bo',
        '' : 'pa','' : 'pi','' : 'pu','' : 'pe','' : 'po',
        '' : 'xa','' : 'xi','' : 'xu','' : 'xe','' : 'xo',
        '' : 'xya','' : 'xyu','' : 'xyo','' : 'xtsu'
    };

    if(triTable[string] !== undefined){
        return triTable[string];
    } else if(biTable[string] !== undefined) {
        return biTable[string];
    }

    var biCheck = new Object();
    for (var k in biTable){
        var tmp = k.split('');
        biCheck[tmp[0]] = true;
    }

    var triCheck = new Object();
    for (var tk in triTable){
        var tmp = tk.split('');
        triCheck[tmp[0] + tmp[1]] = true;
        biCheck[tmp[0]] = true; 
    }


    var buf = '';
    for(var i = 0; i < arrayedString.length ; i++){
        var str = arrayedString[i];
        buf += str;
        if(buf.length == 3){
            if(triTable[buf] !== undefined){
                value += triTable[buf];
            } else {
                tmp = buf.split('');
                value += biTable[tmp[0] + tmp[1]];
                value += uniTable[tmp[2]] === undefined ? tmp[2] : uniTable[tmp[2]];

            }

        } else if(buf.length == 2) {
            if(triCheck[buf] !== undefined) { 
            } else if(biTable[buf] !== undefined) {
                    value += biTable[buf];
                    buf = '';
                } else {
                    tmp = buf.split('');
                    value += uniTable[tmp[0]]; 
                    value += uniTable[tmp[1]] === undefined ? tmp[1] : uniTable[tmp[1]]; 
                    buf = '';
                } 
        } else if(biCheck[buf] !== undefined){
        } else { 
                value += uniTable[str] === undefined ? str : uniTable[str];
                buf = '';
        }


        }

        value += buf !== '' ? uniTable[buf] : '';

    value = value.replace(/([aiueo])ー/gi,'$1');
    return value;
}
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?