Skip to content

Instantly share code, notes, and snippets.

@geckotang
Forked from kimoto/nihongo_to_roman.rb
Last active August 29, 2015 13:56
Show Gist options
  • Save geckotang/9246255 to your computer and use it in GitHub Desktop.
Save geckotang/9246255 to your computer and use it in GitHub Desktop.
source "https://rubygems.org/"
gem 'romankana'
gem 'moji'
#!/bin/env ruby
# encoding: utf-8
# Author: kimoto
require 'pp'
require 'MeCab'
require 'kconv'
require 'bundler'
Bundler.require
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]
pp item
if item != "*"
elements << item
else
elements << Moji.zen_to_han(node.surface)
end
node = node.next
end
Moji.zen_to_han(elements.map(&:katakana_to_roman).join(join_word))
end
p japanese = "天二郎V2「CSSはかんたんです。よゆう!」"
p nihongo_to_roma(japanese)
#" ten ni rou V 2 「 CSS ha kan ta n desu 。 yo yuu ! 」 "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment