Skip to content

Instantly share code, notes, and snippets.

@pnlybubbles
Last active January 4, 2016 09:21
Show Gist options
  • Save pnlybubbles/5ced601bff328356ec04 to your computer and use it in GitHub Desktop.
Save pnlybubbles/5ced601bff328356ec04 to your computer and use it in GitHub Desktop.
漢字混じりの文字列をローマ字列に変換する。input.txt(改行区切り)を読み込んでoutput.txtに吐き出す。YahooAPIを利用。
# A sample Gemfile
source "https://rubygems.org"
# gem "rails"
gem "yahoo-japanese-analysis"
gem "romaji"
require 'yahoo-japanese-analysis'
require 'romaji'
require 'pp'
data = File.read('./sample.txt').split("\n");
YahooJA.configure do |config|
config.app_key = 'dj0zaiZpPTlhNTluRTM1MGFZeSZzPWNvbnN1bWVyc2VjcmV0Jng9NWY-'
end
result = []
puts "requesting..."
data.each { |str|
puts str
res = YahooJA.furigana(str)
pp res
next if res.nil?
kana = [res[:Result][:WordList][:Word]].flatten.inject('') { |r, v_|
add = v_[:Furigana] ? v_[:Furigana] : v_[:Surface]
r + add
}
puts kana
romaji = Romaji.kana2romaji(kana)
puts romaji
result << romaji
}
File.write('output.txt', result.join("\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment