@mrizki/natural
Version:
General natural language (tokenizing, stemming (English, Russian, Spanish), part-of-speech tagging, sentiment analysis, classification, inflection, phonetics, tfidf, WordNet, jaro-winkler, Levenshtein distance, Dice's Coefficient) facilities for node.
625 lines (579 loc) • 16.3 kB
JavaScript
/*
Copyright (c) 2012, Guillaume Marty
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.
*/
/**
* Normalize Japanese inputs and expose function to perform several conversions.
*
* Note: The space character is treated like a roman character as it usually
* has the same width as them in Japanese texts.
*
* \@todo Replace characters range from ㈠ to ㉃, ㊀ to ㊰ and ㇰ to ㇿ.
* \@todo Lazy initializations of conversionTables and converters.
* \@todo Would fixHalfwidthKana be useful?
*
* Descriptions of functions exposed:
* normalizeJapanese 「全角」英字・数字を「半角」、「半角」記・カタカナを「全角」に変換
* converters.fullwidthToHalfwidth.alphabet 「全角」英字を「半角」に変換
* converters.halfwidthToFullwidth.alphabet 「半角」英字を「全角」に変換
* converters.fullwidthToHalfwidth.numbers 「全角」数字を「半角」に変換
* converters.halfwidthToFullwidth.numbers 「半角」数字を「全角」に変換 「全角」スペースを「半角」
* converters.fullwidthToHalfwidth.punctuation 「全角」記号を「半角」に変換 「半角」スペースを「全角」
* converters.halfwidthToFullwidth.punctuation 「半角」記号を「全角」に変換
* converters.fullwidthToHalfwidth.katakana 「全角カタカナ」を「半角カタカナ」に変換
* converters.halfwidthToFullwidth.katakana 「半角カタカナ」を「全角カタカナ」に変換
* converters.hiraganaToKatakana 「カタカナ」を「ひらがな」に変換
* converters.katakanaToHiragana 「ひらがな」を「カタカナ」に変換
*/
var flip = require('../util/utils.js').flip;
var merge = require('../util/utils.js').merge;
var replacer = require('../util/utils').replacer;
// From http://fernweh.jp/b/mb_convert_kana_js/
var conversionTables = {
fullwidthToHalfwidth: {
alphabet: {
'a': 'a',
'b': 'b',
'c': 'c',
'd': 'd',
'e': 'e',
'f': 'f',
'g': 'g',
'h': 'h',
'i': 'i',
'j': 'j',
'k': 'k',
'l': 'l',
'm': 'm',
'n': 'n',
'o': 'o',
'p': 'p',
'q': 'q',
'r': 'r',
's': 's',
't': 't',
'u': 'u',
'v': 'v',
'w': 'w',
'x': 'x',
'y': 'y',
'z': 'z',
'A': 'A',
'B': 'B',
'C': 'C',
'D': 'D',
'E': 'E',
'F': 'F',
'G': 'G',
'H': 'H',
'I': 'I',
'J': 'J',
'K': 'K',
'L': 'L',
'M': 'M',
'N': 'N',
'O': 'O',
'P': 'P',
'Q': 'Q',
'R': 'R',
'S': 'S',
'T': 'T',
'U': 'U',
'V': 'V',
'W': 'W',
'X': 'X',
'Y': 'Y',
'Z': 'Z',
' ': ' ' // Fullwidth space
},
numbers: {
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9'
},
symbol: {
'_': '_',
'-': '-',
',': ',',
';': ';',
':': ':',
'!': '!',
'?': '?',
'.': '.',
'(': '(',
')': ')',
'[': '[',
']': ']',
'{': '{',
'}': '}',
'@': '@',
'*': '*',
'\': '\\',
'/': '/',
'&': '&',
'#': '#',
'%': '%',
'`': '`',
'^': '^',
'+': '+',
'<': '<',
'=': '=',
'>': '>',
'|': '|',
// Never converted: '~': '~',
'≪': '«',
'≫': '»',
'─': '-',
'$': '$',
'"': '"'
},
purePunctuation: {
'、': '、',
'。': '。',
'・': '・',
'「': '「',
'」': '」'
},
punctuation: {},
katakana: {
'゛': '゙',
'゜': '゚',
'ー': 'ー',
'ヴ': 'ヴ',
'ガ': 'ガ',
'ギ': 'ギ',
'グ': 'グ',
'ゲ': 'ゲ',
'ゴ': 'ゴ',
'ザ': 'ザ',
'ジ': 'ジ',
'ズ': 'ズ',
'ゼ': 'ゼ',
'ゾ': 'ゾ',
'ダ': 'ダ',
'ヂ': 'ヂ',
'ヅ': 'ヅ',
'デ': 'デ',
'ド': 'ド',
'バ': 'バ',
'パ': 'パ',
'ビ': 'ビ',
'ピ': 'ピ',
'ブ': 'ブ',
'プ': 'プ',
'ベ': 'ベ',
'ペ': 'ペ',
'ボ': 'ボ',
'ポ': 'ポ',
'ァ': 'ァ',
'ア': 'ア',
'ィ': 'ィ',
'イ': 'イ',
'ゥ': 'ゥ',
'ウ': 'ウ',
'ェ': 'ェ',
'エ': 'エ',
'ォ': 'ォ',
'オ': 'オ',
'カ': 'カ',
'キ': 'キ',
'ク': 'ク',
'ケ': 'ケ',
'コ': 'コ',
'サ': 'サ',
'シ': 'シ',
'ス': 'ス',
'セ': 'セ',
'ソ': 'ソ',
'タ': 'タ',
'チ': 'チ',
'ッ': 'ッ',
'ツ': 'ツ',
'テ': 'テ',
'ト': 'ト',
'ナ': 'ナ',
'ニ': 'ニ',
'ヌ': 'ヌ',
'ネ': 'ネ',
'ノ': 'ノ',
'ハ': 'ハ',
'ヒ': 'ヒ',
'フ': 'フ',
'ヘ': 'ヘ',
'ホ': 'ホ',
'マ': 'マ',
'ミ': 'ミ',
'ム': 'ム',
'メ': 'メ',
'モ': 'モ',
'ャ': 'ャ',
'ヤ': 'ヤ',
'ュ': 'ュ',
'ユ': 'ユ',
'ョ': 'ョ',
'ヨ': 'ヨ',
'ラ': 'ラ',
'リ': 'リ',
'ル': 'ル',
'レ': 'レ',
'ロ': 'ロ',
'ワ': 'ワ',
'ヲ': 'ヲ',
'ン': 'ン'
}
},
halfwidthToFullwidth: {}
};
var fixFullwidthKana = {
'ゝ゛': 'ゞ',
'ヽ゛': 'ヾ',
'う゛': 'ゔ',
'か゛': 'が',
'き゛': 'ぎ',
'く゛': 'ぐ',
'け゛': 'げ',
'こ゛': 'ご',
'さ゛': 'ざ',
'し゛': 'じ',
'す゛': 'ず',
'せ゛': 'ぜ',
'そ゛': 'ぞ',
'た゛': 'だ',
'ち゛': 'ぢ',
'つ゛': 'づ',
'て゛': 'で',
'と゛': 'ど',
'は゛': 'ば',
'は゜': 'ぱ',
'ひ゛': 'び',
'ひ゜': 'ぴ',
'ふ゛': 'ぶ',
'ふ゜': 'ぷ',
'へ゛': 'べ',
'へ゜': 'ぺ',
'ほ゛': 'ぼ',
'ほ゜': 'ぽ',
'っな': 'んな',
'っに': 'んに',
'っぬ': 'んぬ',
'っね': 'んね',
'っの': 'んの',
'ウ゛': 'ヴ',
'カ゛': 'ガ',
'キ゛': 'ギ',
'ク゛': 'グ',
'ケ゛': 'ゲ',
'コ゛': 'ゴ',
'サ゛': 'ザ',
'シ゛': 'ジ',
'ス゛': 'ズ',
'セ゛': 'ゼ',
'ソ゛': 'ゾ',
'タ゛': 'ダ',
'チ゛': 'ヂ',
'ツ゛': 'ヅ',
'テ゛': 'デ',
'ト゛': 'ド',
'ハ゛': 'バ',
'ハ゜': 'パ',
'ヒ゛': 'ビ',
'ヒ゜': 'ピ',
'フ゛': 'ブ',
'フ゜': 'プ',
'ヘ゛': 'ベ',
'ヘ゜': 'ペ',
'ホ゛': 'ボ',
'ホ゜': 'ポ',
'ッナ': 'ンナ',
'ッニ': 'ンニ',
'ッヌ': 'ンヌ',
'ッネ': 'ンネ',
'ッノ': 'ンノ'
};
var fixCompositeSymbolsTable = {
'㋀': '1月',
'㋁': '2月',
'㋂': '3月',
'㋃': '4月',
'㋄': '5月',
'㋅': '6月',
'㋆': '7月',
'㋇': '8月',
'㋈': '9月',
'㋉': '10月',
'㋊': '11月',
'㋋': '12月',
'㏠': '1日',
'㏡': '2日',
'㏢': '3日',
'㏣': '4日',
'㏤': '5日',
'㏥': '6日',
'㏦': '7日',
'㏧': '8日',
'㏨': '9日',
'㏩': '10日',
'㏪': '11日',
'㏫': '12日',
'㏬': '13日',
'㏭': '14日',
'㏮': '15日',
'㏯': '16日',
'㏰': '17日',
'㏱': '18日',
'㏲': '19日',
'㏳': '20日',
'㏴': '21日',
'㏵': '22日',
'㏶': '23日',
'㏷': '24日',
'㏸': '25日',
'㏹': '26日',
'㏺': '27日',
'㏻': '28日',
'㏼': '29日',
'㏽': '30日',
'㏾': '31日',
'㍘': '0点',
'㍙': '1点',
'㍚': '2点',
'㍛': '3点',
'㍜': '4点',
'㍝': '5点',
'㍞': '6点',
'㍟': '7点',
'㍠': '8点',
'㍡': '9点',
'㍢': '10点',
'㍣': '11点',
'㍤': '12点',
'㍥': '13点',
'㍦': '14点',
'㍧': '15点',
'㍨': '16点',
'㍩': '17点',
'㍪': '18点',
'㍫': '19点',
'㍬': '20点',
'㍭': '21点',
'㍮': '22点',
'㍯': '23点',
'㍰': '24点',
'㍻': '平成',
'㍼': '昭和',
'㍽': '大正',
'㍾': '明治',
'㍿': '株式会社',
'㌀': 'アパート',
'㌁': 'アルファ',
'㌂': 'アンペア',
'㌃': 'アール',
'㌄': 'イニング',
'㌅': 'インチ',
'㌆': 'ウオン',
'㌇': 'エスクード',
'㌈': 'エーカー',
'㌉': 'オンス',
'㌊': 'オーム',
'㌋': 'カイリ', //海里
'㌌': 'カラット',
'㌍': 'カロリー',
'㌎': 'ガロン',
'㌏': 'ガンマ',
'㌐': 'ギガ',
'㌑': 'ギニー',
'㌒': 'キュリー',
'㌓': 'ギルダー',
'㌔': 'キロ',
'㌕': 'キログラム',
'㌖': 'キロメートル',
'㌗': 'キロワット',
'㌘': 'グラム',
'㌙': 'グラムトン',
'㌚': 'クルゼイロ',
'㌛': 'クローネ',
'㌜': 'ケース',
'㌝': 'コルナ',
'㌞': 'コーポ',
'㌟': 'サイクル',
'㌠': 'サンチーム',
'㌡': 'シリング',
'㌢': 'センチ',
'㌣': 'セント',
'㌤': 'ダース',
'㌥': 'デシ',
'㌦': 'ドル',
'㌧': 'トン',
'㌨': 'ナノ',
'㌩': 'ノット',
'㌪': 'ハイツ',
'㌫': 'パーセント',
'㌬': 'パーツ',
'㌭': 'バーレル',
'㌮': 'ピアストル',
'㌯': 'ピクル',
'㌰': 'ピコ',
'㌱': 'ビル',
'㌲': 'ファラッド',
'㌳': 'フィート',
'㌴': 'ブッシェル',
'㌵': 'フラン',
'㌶': 'ヘクタール',
'㌷': 'ペソ',
'㌸': 'ペニヒ',
'㌹': 'ヘルツ',
'㌺': 'ペンス',
'㌻': 'ページ',
'㌼': 'ベータ',
'㌽': 'ポイント',
'㌾': 'ボルト',
'㌿': 'ホン',
'㍀': 'ポンド',
'㍁': 'ホール',
'㍂': 'ホーン',
'㍃': 'マイクロ',
'㍄': 'マイル',
'㍅': 'マッハ',
'㍆': 'マルク',
'㍇': 'マンション',
'㍈': 'ミクロン',
'㍉': 'ミリ',
'㍊': 'ミリバール',
'㍋': 'メガ',
'㍌': 'メガトン',
'㍍': 'メートル',
'㍎': 'ヤード',
'㍏': 'ヤール',
'㍐': 'ユアン',
'㍑': 'リットル',
'㍒': 'リラ',
'㍓': 'ルピー',
'㍔': 'ルーブル',
'㍕': 'レム',
'㍖': 'レントゲン',
'㍗': 'ワット'
};
// punctuation is pure_punctuation
conversionTables.fullwidthToHalfwidth.punctuation = merge(
conversionTables.fullwidthToHalfwidth.symbol,
conversionTables.fullwidthToHalfwidth.purePunctuation
)
// Fill in the conversion tables with the flipped tables.
conversionTables.halfwidthToFullwidth.alphabet = flip(conversionTables.fullwidthToHalfwidth.alphabet);
conversionTables.halfwidthToFullwidth.numbers = flip(conversionTables.fullwidthToHalfwidth.numbers);
conversionTables.halfwidthToFullwidth.symbol = flip(conversionTables.fullwidthToHalfwidth.symbol);
conversionTables.halfwidthToFullwidth.purePunctuation = flip(conversionTables.fullwidthToHalfwidth.purePunctuation);
conversionTables.halfwidthToFullwidth.punctuation = flip(conversionTables.fullwidthToHalfwidth.punctuation);
conversionTables.halfwidthToFullwidth.katakana = flip(conversionTables.fullwidthToHalfwidth.katakana);
// Build the normalization table.
conversionTables.normalize = merge(
conversionTables.fullwidthToHalfwidth.alphabet,
conversionTables.fullwidthToHalfwidth.numbers,
conversionTables.fullwidthToHalfwidth.symbol,
conversionTables.halfwidthToFullwidth.purePunctuation,
conversionTables.halfwidthToFullwidth.katakana
);
var converters = {
fullwidthToHalfwidth: {
alphabet: replacer(conversionTables.fullwidthToHalfwidth.alphabet),
numbers: replacer(conversionTables.fullwidthToHalfwidth.numbers),
symbol: replacer(conversionTables.fullwidthToHalfwidth.symbol),
purePunctuation: replacer(conversionTables.fullwidthToHalfwidth.purePunctuation),
punctuation: replacer(conversionTables.fullwidthToHalfwidth.punctuation),
katakana: replacer(conversionTables.fullwidthToHalfwidth.katakana)
},
halfwidthToFullwidth: {
alphabet: replacer(conversionTables.halfwidthToFullwidth.alphabet),
numbers: replacer(conversionTables.halfwidthToFullwidth.numbers),
symbol: replacer(conversionTables.halfwidthToFullwidth.symbol),
purePunctuation: replacer(conversionTables.halfwidthToFullwidth.purePunctuation),
punctuation: replacer(conversionTables.halfwidthToFullwidth.punctuation),
katakana: replacer(conversionTables.halfwidthToFullwidth.katakana)
},
fixFullwidthKana: replacer(fixFullwidthKana),
normalize: replacer(conversionTables.normalize)
};
var fixCompositeSymbols = replacer(fixCompositeSymbolsTable);
/**
* Convert hiragana to fullwidth katakana.
* According to http://jsperf.com/converting-japanese, these implementations are
* faster than using lookup tables.
*
* @param {string} str A string.
* @return {string} A string not containing hiragana.
*/
converters.hiraganaToKatakana = function(str) {
str = converters.halfwidthToFullwidth.katakana(str);
str = converters.fixFullwidthKana(str);
str = str.replace(/ゝ/g, 'ヽ');
str = str.replace(/ゞ/g, 'ヾ');
//str = str.replace(/?/g, '𛀀'); // Letter archaic E
str = str.replace(/[ぁ-ゖ]/g, function(str) {
return String.fromCharCode(str.charCodeAt(0) + 96);
});
return str;
};
/**
* Convert katakana to hiragana.
*
* @param {string} str A string.
* @return {string} A string not containing katakana.
*/
converters.katakanaToHiragana = function(str) {
str = converters.halfwidthToFullwidth.katakana(str);
str = converters.fixFullwidthKana(str);
str = str.replace(/ヽ/g, 'ゝ');
str = str.replace(/ヾ/g, 'ゞ');
//str = str.replace(/?/g, '𛀁'); // Letter archaic E
str = str.replace(/[ァ-ヶ]/g, function(str) {
return String.fromCharCode(str.charCodeAt(0) - 96);
});
return str;
};
/**
* Fix kana and apply the following processes;
* * Replace repeat characters
* * Alphabet to halfwidth
* * Numbers to halfwidth
* * Punctuation to fullwidth
* * Katakana to fullwidth
* * Fix fullwidth kana
* * Replace composite symbols
*
* @param {string} str
* @return {string}
*/
var normalize_ja = function(str) {
// Replace repeat characters.
str = str
.replace(/(..)々々/g, '$1$1')
.replace(/(.)々/g, '$1$1');
str = converters.normalize(str);
str = converters.fixFullwidthKana(str);
// Replace composite symbols.
str = fixCompositeSymbols(str);
return str;
};
exports.normalize_ja = normalize_ja;
exports.converters = converters;