Created
February 29, 2012 12:28
-
-
Save kimoto/1940479 to your computer and use it in GitHub Desktop.
日本語をローマ字に変換するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
# encoding: utf-8 | |
# Author: kimoto | |
require 'MeCab' | |
require 'kconv' | |
require 'romankana' | |
require 'moji' | |
def nihongo_to_roma(japanese, join_word="_") | |
m = MeCab::Tagger.new("-Ochasen") | |
node = m.parseToNode(japanese) | |
elements = [] | |
while node | |
item = node.feature.toutf8.split(",")[-2] | |
if item != "*" | |
elements << item | |
end | |
node = node.next | |
end | |
Moji.zen_to_han(elements.map(&:katakana_to_roman).join(join_word)) | |
end | |
p japanese = "日本語をローマ字のみにするテストです" | |
p nihongo_to_roma(japanese) | |
# => "nihongo_o_roーmaji_nomi_ni_suru_tesuto_desu" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment