MySQLに登録してからミス発見!「かなをローマ字表記に変換する」
自称「全国版住所検索システム」ですが、試験的に12万4000件のデータもMySQLに登録。
事業所も12000件、登録が終了しました。
検索のためのwebアプリもほぼ完成に近づいて、遊んでいたところ
カナの長音記号「−」が半角の「-」に変換されていないことに気づきました!
げぇ〜〜〜〜〜〜〜って感じです。
SQLのコマンドから登録すれば、ものの数秒で終わるのですが、そこはほれ!
Javascriptでやるから価値がある(?)と信じきっている俺なので、スクリプトでやってやる!
MySQLの登録にも1時間半かかったのに、すべてやり直し?!
めげる。
そこで、プログラムを書き換えて、長音記号にも対応するようにした。
これでまた、2日くらい無駄にするだろうなぁ〜
ご利用の際はDMonkeyを利用してください。
ぐぐって検索でひっかかるのかは不明だけど、
「かなをローマ字表記に変換する」って文字を付け加えておこうか。
var dialog = new Dialog(); var fname = dialog.openFile('変換するデータファイル', 'ZIPJIS9B.k3'); class kanaToRoman { chop = false; //ローマ字文字列をアルファベット1文字ごとに分解するかどうか vowel = ['a','i','u','e','o']; child = ['a','k','s','t','n','h','m','y','r','w','g','z','d','b','p','x','y','t']; symbol = "!?-',-!?-',"; zsymbol = "!?−’,ー!?-',"; number = '0123456789'; znumber = '0123456789'; cols_H = { 'A': 'あかさたなはまやらわがざだばぱぁゃ', 'I': 'いきしちにひみ@り@ぎじぢびぴぃ', 'U': 'うくすつぬふむゆるんぐずづぶぷぅゅっ', 'E': 'えけせてねへめ@れ@げぜでべぺぇ', 'O': 'おこそとのほもよろをごぞどぼぽぉょ' }; mode_Krows = "k"; //か・く・こ mode_XArows = "x"; //小文字ぁ行と「っ」 mode_TYrows = "ty"; //ち行+小文字や行 mode_SYrows = "sh"; //し行+小文字や行 mode_JYrows = "zy"; //じ行+小文字や行 mode_Sstr = "s"; //し mode_Jstr = "j"; //じ mode_TUstr = "t"; //つ mode_FUstr = "h"; //ふ mode_TIstr = "t"; //ち mode_Nstr = "n"; //ん function convert ( txt ) { var stack = this._TextSlice( txt + '' ), out = [ ]; for( var i = 0; i < stack.length; i++ ) out.push( stack[i].length == 1 ? this._baseOne( stack[i] ): this._baseTwo( stack[i] ) ); return out.join(''); } function _TextSlice ( txt ) { var max = txt.length; var array = [ ]; for( i = 0; i < max; i++ ) { var str = txt.charAt(i);//今の文字 var nxt = txt.charAt( i + 1);//次の文字 //隣接する1文字目が小文字や行なら if( nxt == 'ゃ' || nxt == 'ゅ' || nxt == 'ょ' ) { array.push( str + nxt ); i++; } else if( str == 'っ' && this.symbol.indexOf( nxt ) == -1 ) { if( this.number.indexOf( nxt ) == -1 ) { array.push( str + nxt ); i++; } else { array.push( str ); } } else { array.push( str ); } } return array; } function _baseTwo ( str ) { if( str.indexOf( 'っ' ) != -1 ) { if( str.length == 2 ) { var txt = this._baseOne( str.charAt(1) ); return txt.charAt(0) + txt; } else { return this._baseOne( str ); } } else { switch( str ) { case 'ちゃ': return this.mode_TYrows + this.vowel[0]; case 'ちゅ': return this.mode_TYrows + this.vowel[2]; case 'ちょ': return this.mode_TYrows + this.vowel[4]; case 'しゃ': return this.mode_SYrows + this.vowel[0]; case 'しゅ': return this.mode_SYrows + this.vowel[2]; case 'しょ': return this.mode_SYrows + this.vowel[4]; case 'じゃ': return this.mode_JYrows + this.vowel[0]; case 'じゅ': return this.mode_JYrows + this.vowel[2]; case 'じょ': return this.mode_JYrows + this.vowel[4]; default: var first = this._baseOne( str.charAt(0) ); var second = this._baseOne( str.charAt(1) ); return first.charAt(0) + second; } } } function _baseOne ( str ){ var tmp; if( (tmp = this.cols_H.A.indexOf(str)) != -1 ) return this._Change_A_Rows( tmp ); if( (tmp = this.cols_H.I.indexOf(str)) != -1 ) return this._Change_I_Rows( tmp ); if( (tmp = this.cols_H.U.indexOf(str)) != -1 ) return this._Change_U_Rows( tmp ); if( (tmp = this.cols_H.E.indexOf(str)) != -1 ) return this._Change_E_Rows( tmp ); if( (tmp = this.cols_H.O.indexOf(str)) != -1 ) return this._Change_O_Rows( tmp ); if( (tmp = this.znumber.indexOf(str)) != -1 ) return this.number.substring( tmp, tmp + 1 ); if( (tmp = this.zsymbol.indexOf(str)) != -1 ) return this.symbol.substring( tmp, tmp + 1 ); if( (tmp = this.number.indexOf(str)) != -1 ) return str; return null; } function _Change_A_Rows ( key ) { switch( key ) { case 1: return this.mode_Krows + this.vowel[0]; case 15: return this.mode_XArows + this.vowel[0]; case 0: return this.vowel[0]; default: return this.child[ key ] + this.vowel[0]; } } function _Change_I_Rows ( key ) { switch( key ) { case 0: return this.vowel[1]; case 15: return this.mode_XArows + this.vowel[1]; case 2: return this.mode_Sstr + this.vowel[1]; case 11: return this.mode_Jstr + this.vowel[1]; case 3: return this.mode_TIstr + this.vowel[1]; default: return this.child[ key ] + this.vowel[1]; } } function _Change_U_Rows ( key ) { switch( key ) { case 0: return this.vowel[2]; case 1: return this.mode_Krows + this.vowel[2]; case 15: return this.mode_XArows + this.vowel[2]; case 3: return this.mode_TUstr + this.vowel[2]; case 5: return this.mode_FUstr + this.vowel[2]; case 9: return this.mode_Nstr; case 17: return this.mode_XArows + this.mode_TUstr + this.vowel[2]; default: return this.child[ key ] + this.vowel[2]; } } function _Change_E_Rows ( key ) { switch( key ) { case 0: return this.vowel[3]; case 15: return this.mode_XArows + this.vowel[3]; default: return this.child[ key ] + this.vowel[3]; } } function _Change_O_Rows ( key ) { switch( key ) { case 0: return this.vowel[4]; case 1: return this.mode_Krows + this.vowel[4]; case 15: return this.mode_XArows + this.vowel[4]; default: return this.child[ key ] + this.vowel[4]; } } } Strings.prototype.toRoman = (function () { var toRoman = new kanaToRoman(); return function() { return toRoman.convert( this ); }; })(); var data = new Strings(); var count; var max; var rec; var tmp; if( (new File( fname )).exists() ) { data.loadFromFile( fname ); max = data.length; try { for( count = 0 ; count< max; count++ ) { if( tmp = data[ count ] ) { tmp = tmp.replace( /"|\s/g, '' ); rec = tmp.split( ',' ); rec[13] = (new Strings((rec[5] + rec[6] + rec[7] + rec[8]).replace( /(|、/g, ' ' ).replace( /)/g, '').replace( /〜/g, '-' ).toZenkaku().toHiragana())).toRoman(); data[ count ] = rec.join( "\t" ).toUTF8(); } } } catch (err) { data.saveToFile( 'zipzis9b.txt' ); alert( [ count, tmp ]); } data.saveToFile( 'zipzis9b.txt' ); } else { alert( 'ファイルがありません' ); } alert( '終了しました' );