Created
April 28, 2014 22:43
-
-
Save furandon-pig/11386243 to your computer and use it in GitHub Desktop.
ポケットミクでテキスト読み上げを行うRubyスクリプトです。
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
#!/usr/bin/env ruby193 | |
# -*- coding: utf-8 -*- | |
require 'pocket_miku' | |
char_map = Array.new | |
# この順番でサーチするため、HashではなくArrayにする | |
char_map.push('キャ') | |
char_map.push('キュ') | |
char_map.push('キョ') | |
char_map.push('ギャ') | |
char_map.push('ギュ') | |
char_map.push('ギョ') | |
char_map.push('スィ') | |
char_map.push('ズィ') | |
char_map.push('シャ') | |
char_map.push('シュ') | |
char_map.push('シェ') | |
char_map.push('ショ') | |
char_map.push('ジャ') | |
char_map.push('ジュ') | |
char_map.push('ジェ') | |
char_map.push('ジョ') | |
char_map.push('ティ') | |
char_map.push('トゥ') | |
char_map.push('ディ') | |
char_map.push('ドゥ') | |
char_map.push('テュ') | |
char_map.push('デュ') | |
char_map.push('チャ') | |
char_map.push('チュ') | |
char_map.push('チェ') | |
char_map.push('チョ') | |
char_map.push('ツァ') | |
char_map.push('ツィ') | |
char_map.push('ツェ') | |
char_map.push('ツォ') | |
char_map.push('ニャ') | |
char_map.push('ニュ') | |
char_map.push('ニョ') | |
char_map.push('ヒャ') | |
char_map.push('ヒュ') | |
char_map.push('ヒョ') | |
char_map.push('ビャ') | |
char_map.push('ビュ') | |
char_map.push('ビョ') | |
char_map.push('ピャ') | |
char_map.push('ピュ') | |
char_map.push('ピョ') | |
char_map.push('ファ') | |
char_map.push('フィ') | |
char_map.push('フュ') | |
char_map.push('フェ') | |
char_map.push('フォ') | |
char_map.push('ミャ') | |
char_map.push('ミュ') | |
char_map.push('ミョ') | |
char_map.push('リャ') | |
char_map.push('リュ') | |
char_map.push('リョ') | |
char_map.push('ウィ') | |
char_map.push('ウェ') | |
char_map.push('ウォ') | |
char_map.push('ア') | |
char_map.push('イ') | |
char_map.push('ウ') | |
char_map.push('エ') | |
char_map.push('オ') | |
char_map.push('カ') | |
char_map.push('キ') | |
char_map.push('ク') | |
char_map.push('ケ') | |
char_map.push('コ') | |
char_map.push('ガ') | |
char_map.push('ギ') | |
char_map.push('グ') | |
char_map.push('ゲ') | |
char_map.push('ゴ') | |
char_map.push('サ') | |
char_map.push('ス') | |
char_map.push('セ') | |
char_map.push('ソ') | |
char_map.push('ザ') | |
char_map.push('ズ') | |
char_map.push('ゼ') | |
char_map.push('ゾ') | |
char_map.push('シ') | |
char_map.push('ジ') | |
char_map.push('タ') | |
char_map.push('テ') | |
char_map.push('ト') | |
char_map.push('ダ') | |
char_map.push('デ') | |
char_map.push('ド') | |
char_map.push('チ') | |
char_map.push('ツ') | |
char_map.push('ナ') | |
char_map.push('ニ') | |
char_map.push('ヌ') | |
char_map.push('ネ') | |
char_map.push('ノ') | |
char_map.push('ハ') | |
char_map.push('ヒ') | |
char_map.push('フ') | |
char_map.push('ヘ') | |
char_map.push('ホ') | |
char_map.push('バ') | |
char_map.push('ビ') | |
char_map.push('ブ') | |
char_map.push('ベ') | |
char_map.push('ボ') | |
char_map.push('パ') | |
char_map.push('ピ') | |
char_map.push('プ') | |
char_map.push('ペ') | |
char_map.push('ポ') | |
char_map.push('マ') | |
char_map.push('ミ') | |
char_map.push('ム') | |
char_map.push('メ') | |
char_map.push('モ') | |
char_map.push('ヤ') | |
char_map.push('ユ') | |
char_map.push('ヨ') | |
char_map.push('ラ') | |
char_map.push('リ') | |
char_map.push('ル') | |
char_map.push('レ') | |
char_map.push('ロ') | |
char_map.push('ワ') | |
char_map.push('ン') | |
if ARGV.empty? | |
STDERR.print("Error: 形態素解析結果ファイルを指定してください。\n") | |
exit 1 | |
end | |
# 今日 キョウ 今日 名詞-副詞可能 | |
# ~~~~~~\ | |
# `--> "キョ,ウ"のような仮名毎のカンマ区切りに変換する | |
word_list = Array.new | |
buf = open(ARGV[0]).readlines | |
buf.each do |b| | |
break if b =~ /EOS/ | |
elm = b.chomp.split("\t") | |
while !(elm[1] =~ /,$/) | |
elm[1] = elm[1] + "," | |
char_map.length.times do |i| | |
if elm[1] =~ /(#{char_map[i]})/ | |
word = $1 | |
elm[1] = elm[1].sub(/#{word}/, "#{word},") | |
end | |
# "キ,ョ,ウ"のような区切りになる(「キョ」が「キ」で区切られる)ので | |
# 以下で"キ,ョ"->"キョ"に戻す(あまりスマートな処理ではない...) | |
if elm[1] =~ /,([ァィゥェォャュョ].)/ | |
m = $1 | |
elm[1] = elm[1].gsub(/,[ァィゥェォャュョ]/, m) | |
end | |
end | |
end | |
# カンマ区切りに変換した結果から、文字のみを抜き出す | |
# (上記の処理では"キョ,,,ウ,"のようなケースがある) | |
elm2 = elm[1].split(",") | |
elm2.length.times do |j| | |
word_list.push(elm2[j]) if elm2[j] != nil | |
end | |
end | |
PocketMiku.sing '/dev/rmidi1' do | |
tempo 240 | |
# 順次文字を読み上げるだけ | |
word_list.each do |w| | |
p w | |
case w | |
when 'キャ' then きゃ | |
when 'キュ' then きゅ | |
when 'キョ' then きょ | |
when 'ギャ' then ぎゃ | |
when 'ギュ' then ぎゅ | |
when 'ギョ' then ぎょ | |
when 'スィ' then すぃ | |
when 'ズィ' then ずぃ | |
when 'シャ' then しゃ | |
when 'シュ' then しゅ | |
when 'シェ' then しぇ | |
when 'ショ' then しょ | |
when 'ジャ' then じゃ | |
when 'ジュ' then じゅ | |
when 'ジェ' then じぇ | |
when 'ジョ' then じょ | |
when 'ティ' then てぃ | |
when 'トゥ' then とぅ | |
when 'ディ' then でぃ | |
when 'ドゥ' then どぅ | |
when 'テュ' then てゅ | |
when 'デュ' then でゅ | |
when 'チャ' then ちゃ | |
when 'チュ' then ちゅ | |
when 'チェ' then ちぇ | |
when 'チョ' then ちょ | |
when 'ツァ' then つぁ | |
when 'ツィ' then つぃ | |
when 'ツェ' then つぇ | |
when 'ツォ' then つぉ | |
when 'ニャ' then にゃ | |
when 'ニュ' then にゅ | |
when 'ニョ' then にょ | |
when 'ヒャ' then ひゃ | |
when 'ヒュ' then ひゅ | |
when 'ヒョ' then ひょ | |
when 'ビャ' then びゃ | |
when 'ビュ' then びゅ | |
when 'ビョ' then びょ | |
when 'ピャ' then ぴゃ | |
when 'ピュ' then ぴゅ | |
when 'ピョ' then ぴょ | |
when 'ファ' then ふぁ | |
when 'フィ' then ふぃ | |
when 'フュ' then ふゅ | |
when 'フェ' then ふぇ | |
when 'フォ' then ふぉ | |
when 'ミャ' then みゃ | |
when 'ミュ' then みゅ | |
when 'ミョ' then みょ | |
when 'リャ' then りゃ | |
when 'リュ' then りゅ | |
when 'リョ' then りょ | |
when 'ウィ' then うぃ | |
when 'ウェ' then うぇ | |
when 'ウォ' then うぉ | |
when 'ア' then あ | |
when 'イ' then い | |
when 'ウ' then う | |
when 'エ' then え | |
when 'オ' then お | |
when 'カ' then か | |
when 'キ' then き | |
when 'ク' then く | |
when 'ケ' then け | |
when 'コ' then こ | |
when 'ガ' then が | |
when 'ギ' then ぎ | |
when 'グ' then ぐ | |
when 'ゲ' then げ | |
when 'ゴ' then ご | |
when 'サ' then さ | |
when 'ス' then す | |
when 'セ' then せ | |
when 'ソ' then そ | |
when 'ザ' then ざ | |
when 'ズ' then ず | |
when 'ゼ' then ぜ | |
when 'ゾ' then ぞ | |
when 'シ' then し | |
when 'ジ' then じ | |
when 'タ' then た | |
when 'テ' then て | |
when 'ト' then と | |
when 'ダ' then だ | |
when 'デ' then で | |
when 'ド' then ど | |
when 'チ' then ち | |
when 'ツ' then つ | |
when 'ナ' then な | |
when 'ニ' then に | |
when 'ヌ' then ぬ | |
when 'ネ' then ね | |
when 'ノ' then の | |
when 'ハ' then は | |
when 'ヒ' then ひ | |
when 'フ' then ふ | |
when 'ヘ' then へ | |
when 'ホ' then ほ | |
when 'バ' then ば | |
when 'ビ' then び | |
when 'ブ' then ぶ | |
when 'ベ' then べ | |
when 'ボ' then ぼ | |
when 'パ' then ぱ | |
when 'ピ' then ぴ | |
when 'プ' then ぷ | |
when 'ペ' then ぺ | |
when 'ポ' then ぽ | |
when 'マ' then ま | |
when 'ミ' then み | |
when 'ム' then む | |
when 'メ' then め | |
when 'モ' then も | |
when 'ヤ' then や | |
when 'ユ' then ゆ | |
when 'ヨ' then よ | |
when 'ラ' then ら | |
when 'リ' then り | |
when 'ル' then る | |
when 'レ' then れ | |
when 'ロ' then ろ | |
when 'ワ' then わ | |
when 'ン' then ん | |
when 'ン' then ん | |
when 'ン' then ん | |
when 'ン' then ん | |
when 'ン' then ん | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment