Skip to content

Instantly share code, notes, and snippets.

@mrk21
Last active February 1, 2023 06:09
Show Gist options
  • Save mrk21/adc8714e2c7fec90f82f37540611f7e5 to your computer and use it in GitHub Desktop.
Save mrk21/adc8714e2c7fec90f82f37540611f7e5 to your computer and use it in GitHub Desktop.
Sort array by Kanji using MeCab on Ruby.
# $ brew install mecab
# $ brew install mecab-ipadic
# $ gem install natto
# $ ruby mecab_sort.rb
# "シュジンコウ"
# "ジョウシャ"
# "ジョウシャ"
# "コウムイン"
# "シュジンコウ"
# "コウムイン"
# ["主人公", "乗車", "公務員"]
# ["公務員", "主人公", "乗車"]
require 'natto'
natto = Natto::MeCab.new
arr1 = [
'主人公',
'乗車',
'公務員',
]
arr2 = arr1.sort do |a,b|
aa = natto.parse(a).split(',')[7]
bb = natto.parse(b).split(',')[7]
pp aa, bb
aa <=> bb
end
pp arr1
pp arr2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment