Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
한글 초성 분리기
# 삼성전자 => ㅅㅅㅈㅈ, 유안타제3호스팩 => ㅇㅇㅌㅈ3ㅎㅅㅍ
def extract_korean_initials(keyword)
initials = ['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ']
# hangul_range = '가'..'힣'
hangul_first = 44032 # '가'.ord
size = 588 # '까'.ord - '가'.ord
keyword.split('').collect do |k|
k_char_code = k.ord
initials[(k_char_code - hangul_first) / size] || k
end
.join('')
end
@fallroot

This comment has been minimized.

Copy link

@fallroot fallroot commented Jul 20, 2018

require 'active_support/all'

def extract_korean_initials(keyword)
  keyword.mb_chars.chars.map do |char|
      char.mb_chars.decompose.first
  end.join
end

RoR의 ActiveSupport::Multibyte::Chars를 이용하면 좀 수월할 듯. 에전에 잠깐 만져본 경험이 있어서 ㅎㅎ

@elegantcoder

This comment has been minimized.

Copy link
Owner Author

@elegantcoder elegantcoder commented Jul 22, 2018

@fallroot 이건 대단히, 너무 간단한걸요!?

감사합니다 ㅎㅎㅎ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment