Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created February 29, 2012 12:28
Show Gist options
  • Save kimoto/1940479 to your computer and use it in GitHub Desktop.
Save kimoto/1940479 to your computer and use it in GitHub Desktop.
日本語をローマ字に変換するやつ
#!/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