Class: Ke2daira::Ke2dairanizer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tagger: ::Suika::Tagger.new, separator: " ") ⇒ Ke2dairanizer

Returns a new instance of Ke2dairanizer.



7
8
9
10
# File 'lib/ke2daira/ke2dairanizer.rb', line 7

def initialize(tagger: ::Suika::Tagger.new, separator: " ")
  @tagger = tagger
  @separator = separator
end

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



12
13
14
# File 'lib/ke2daira/ke2dairanizer.rb', line 12

def separator
  @separator
end

#taggerObject (readonly)

Returns the value of attribute tagger.



12
13
14
# File 'lib/ke2daira/ke2dairanizer.rb', line 12

def tagger
  @tagger
end

Instance Method Details

#ke2dairanize(fullname) ⇒ String

Ke2dairanize the givin name

Parameters:

  • fullname (String)

Returns:

  • (String)

    ke2dairanized name



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ke2daira/ke2dairanizer.rb', line 17

def ke2dairanize(fullname) # rubocop:disable Metrics/AbcSize
  names = fullname.strip.split(separator)
  yomis = names.map { |name| to_yomi(name) }

  return yomis[0] if yomis.length == 1

  first_word_moras = Kana2Mora.katakana2mora(yomis[0])
  first_word_head = first_word_moras[0]
  first_word_tail = first_word_moras[1..]

  last_word_moras = Kana2Mora.katakana2mora(yomis[-1])
  last_word_head = last_word_moras[0]
  last_word_tail = last_word_moras[1..]

  new_first_word = last_word_head + first_word_tail.join
  new_last_word = first_word_head + last_word_tail.join

  yomis[0] = new_first_word
  yomis[-1] = new_last_word
  yomis.join(separator)
end