class LyricsJapanese::LyricsJapanese

日本語表記を音符の下に書く表記に変換

Public Instance Methods

to_lyricruby(text, delim = ' ') click to toggle source

メインメソッド mecabを使って読みがなに変換。 読みがなにしても文字数が変化しないなら漢字を使う カタカナ語とアルファベットはそのまま出力。

# File lib/lyrics_japanese.rb, line 47
def to_lyricruby(text, delim = ' ') # rubocop:disable Metrics/AbcSize
  lines = []
  text.each do |sentence|
    words = []
    @@natto.parse(sentence.gsub(/[  ]+/,' ').strip) do |n|
      unless n.is_eos?
        # puts "#{n.surface}\t#{n.feature}"
        rubi = n.surface
        if n.feature.split(',').length > 8 && !n.surface.katakana?
          w7 = n.feature.split(',')[7]
          rubi = w7.to_hiragana if w7.length != n.surface.length
        end
        words << rubi.separate_kana(delim).join_phoneme
      end
    end
    lines << words.join(delim)
  end
  lines
end