Module: Mojicon::Converter

Included in:
String
Defined in:
lib/mojicon/converter.rb

Instance Method Summary collapse

Instance Method Details

#arabic_to_kanji(zero: true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mojicon/converter.rb', line 57

def arabic_to_kanji(zero: true)
  arabic_pattern = /[\d0-9]+/
  arabic_numbers = zen_to_han.scan(arabic_pattern).map(&:to_i)
  arabic_numbers = gsub(arabic_pattern).with_index do |_match, index|
    if zero
      YaKansuji.to_kan(arabic_numbers[index], :judic_v)
    else
      YaKansuji.to_kan(arabic_numbers[index], :simple)
    end
  end
  arabic_numbers.han_to_zen
end

#candidateObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/mojicon/converter.rb', line 82

def candidate
  [
    trim_space,
    trim_space.zen_to_han,
    trim_space.han_to_zen,
    trim_space.kana_to_hira,
    trim_space.hira_to_kana,
    trim_space.upper_to_down,
    trim_space.down_to_upper,
    trim_space.kanji_to_arabic,
    trim_space.arabic_to_kanji,
    trim_space.arabic_to_kanji(zero: false),
    trim_space.to_new_moji,
    trim_space.hira_to_kana.zen_to_han,
    trim_space.kanji_to_arabic.han_to_zen,
    trim_space.arabic_to_kanji.zen_to_han,
    trim_space.arabic_to_kanji(zero: false).zen_to_han,
    trim_space.han_to_zen.kana_to_hira
  ].uniq
end

#down_to_upperObject



43
44
45
46
# File 'lib/mojicon/converter.rb', line 43

def down_to_upper
  upcased = upcase
  upcased.tr("ぁぃぅぇぉっゃゅょァィゥェォッャュョァィゥェォッャュョ", "あいうえおつやゆよアイウエオツヤユヨアイウエオツヤユヨ")
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/mojicon/converter.rb', line 74

def equal?(other)
  return false if other.nil?
  return false unless other.is_a?(String)
  return true if self == other

  candidate.include?(other.trim_space)
end

#han_to_zenObject



21
22
23
24
25
26
# File 'lib/mojicon/converter.rb', line 21

def han_to_zen
  converted = tr("A-Za-z0-9", "A-Za-z0-9")
  space_converted = converted.tr(" ", " ")
  nkf_converted = NKF.nkf("-w -X", space_converted)
  nkf_converted.tr("\s!#$%&\'\"()⦅⦆⟨⟩*+,-./:;<=>?@[]^_`−{|}~\\", " !#$%&'"()⦅⦆〈〉*+,-./:;<=>?@[]^_`-{|}~\")
end

#hira_to_kanaObject



33
34
35
36
# File 'lib/mojicon/converter.rb', line 33

def hira_to_kana
  vu = tr("", "")
  vu.tr("ぁ-ん", "ァ-ヶ")
end

#kana_to_hiraObject



28
29
30
31
# File 'lib/mojicon/converter.rb', line 28

def kana_to_hira
  vu = tr("", "")
  vu.tr("ァ-ヶ", "ぁ-ん")
end

#kanji_to_arabicObject



48
49
50
51
52
53
54
55
# File 'lib/mojicon/converter.rb', line 48

def kanji_to_arabic
  kanji_pattern = /[一二三四五六七八九〇十百千万億兆]+/
  kanji_numbers = scan(kanji_pattern).map(&:to_i)
  replaced_str = gsub(kanji_pattern).with_index do |_match, index|
    kanji_numbers[index].to_s
  end
  replaced_str.zen_to_han
end

#to_new_mojiObject



70
71
72
# File 'lib/mojicon/converter.rb', line 70

def to_new_moji
  Itaiji::Converter.new.seijitai(self)
end

#trim_spaceObject



9
10
11
# File 'lib/mojicon/converter.rb', line 9

def trim_space
  delete(" ")&.delete(" ")
end

#upper_to_downObject



38
39
40
41
# File 'lib/mojicon/converter.rb', line 38

def upper_to_down
  downcased = downcase
  downcased.tr("あいうえおつやゆよアイウエオツヤユヨアイウエオツヤユヨ", "ぁぃぅぇぉっゃゅょァィゥェォッャュョァィゥェォッャュョ")
end

#zen_to_hanObject



13
14
15
16
17
18
19
# File 'lib/mojicon/converter.rb', line 13

def zen_to_han
  return self unless match(/[^ -~。-゚]/)

  space_trimmed_word = tr(" ", " ")
  nkf_converted = NKF.nkf("-Z4 -w", space_trimmed_word)
  nkf_converted.tr("⦅⦆", "⦅⦆")
end