Module: Yanagi::Romaji

Defined in:
lib/yanagi/romaji.rb

Class Method Summary collapse

Class Method Details

.call(input) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yanagi/romaji.rb', line 8

def self.call(input)
  moras = input.is_a?(Array) ? input : Tokenizer.tokenize(input)
  mora_map = Rules.mora_map
  res = []

  moras.each_with_index do |mora, idx|
    case mora.kind
    when :sokuon
      # Find next non-sokuon/chouonpu mora
      next_mora = moras[(idx + 1)..].find { |m| m.syllable? || m.moraic_n? }
      if next_mora
        next_rom = mora_map.dig(next_mora.kana.to_sym, :romaji)&.to_s || next_mora.kana.to_s
        consonant = next_rom.start_with?("ch") ? "t" : next_rom[0]
        res << consonant if consonant
      end
    when :chouonpu
      prev_char = res.last&.chars&.last
      res << prev_char if prev_char
    when :moraic_n
      res << "n"
    when :syllable
      rom = mora_map.dig(mora.kana.to_sym, :romaji)&.to_s || mora.kana.to_s
      res << rom
    else
      res << mora.kana.to_s
    end
  end

  res.join
end

.matches?(reading, romaji) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/yanagi/romaji.rb', line 38

def self.matches?(reading, romaji)
  expected = call(reading)
  return true if romaji == expected

  norm_actual = romaji.to_s.gsub(/\d+$/, "").gsub(/([aeiou])-/, '\1\1').gsub("oo", "ou").gsub("dewa", "deha")
  norm_exp = expected.gsub("oo", "ou")
  norm_actual == norm_exp
end