Last active
February 1, 2023 06:09
-
-
Save mrk21/adc8714e2c7fec90f82f37540611f7e5 to your computer and use it in GitHub Desktop.
Sort array by Kanji using MeCab on Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# $ 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