Class: LyricsJapanese::LyricsJapanese

Inherits:
Object
  • Object
show all
Defined in:
lib/lyrics_japanese.rb

Overview

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

Constant Summary collapse

@@natto =

rubocop:disable Style/ClassVars

Natto::MeCab.new

Instance Method Summary collapse

Instance Method Details

#to_lyricruby(text, delim = ' ') ⇒ Object

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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