Module: Mojicon::Converter
- Included in:
- String
- Defined in:
- lib/mojicon/converter.rb
Instance Method Summary collapse
- #arabic_to_kanji(zero: true) ⇒ Object
- #candidate ⇒ Object
- #down_to_upper ⇒ Object
- #equal?(other) ⇒ Boolean
- #han_to_zen ⇒ Object
- #hira_to_kana ⇒ Object
- #kana_to_hira ⇒ Object
- #kanji_to_arabic ⇒ Object
- #to_new_moji ⇒ Object
- #trim_space ⇒ Object
- #upper_to_down ⇒ Object
- #zen_to_han ⇒ Object
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 |
#candidate ⇒ Object
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_upper ⇒ Object
43 44 45 46 |
# File 'lib/mojicon/converter.rb', line 43 def down_to_upper upcased = upcase upcased.tr("ぁぃぅぇぉっゃゅょァィゥェォッャュョァィゥェォッャュョ", "あいうえおつやゆよアイウエオツヤユヨアイウエオツヤユヨ") end |
#equal?(other) ⇒ 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_zen ⇒ Object
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_kana ⇒ Object
33 34 35 36 |
# File 'lib/mojicon/converter.rb', line 33 def hira_to_kana vu = tr("ゔ", "ヴ") vu.tr("ぁ-ん", "ァ-ヶ") end |
#kana_to_hira ⇒ Object
28 29 30 31 |
# File 'lib/mojicon/converter.rb', line 28 def kana_to_hira vu = tr("ヴ", "ゔ") vu.tr("ァ-ヶ", "ぁ-ん") end |
#kanji_to_arabic ⇒ Object
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_moji ⇒ Object
70 71 72 |
# File 'lib/mojicon/converter.rb', line 70 def to_new_moji Itaiji::Converter.new.seijitai(self) end |
#trim_space ⇒ Object
9 10 11 |
# File 'lib/mojicon/converter.rb', line 9 def trim_space delete(" ")&.delete(" ") end |
#upper_to_down ⇒ Object
38 39 40 41 |
# File 'lib/mojicon/converter.rb', line 38 def upper_to_down downcased = downcase downcased.tr("あいうえおつやゆよアイウエオツヤユヨアイウエオツヤユヨ", "ぁぃぅぇぉっゃゅょァィゥェォッャュョァィゥェォッャュョ") end |
#zen_to_han ⇒ Object
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 |