Mac Mecab installed
#Hatena Keyword
curl -L http://d.hatena.ne.jp/images/keyword/keywordlist_furigana.csv | iconv -f euc-jp -t utf-8 > keywordlist_furigana.csv
# Wikipedia
curl -L http://dumps.wikimedia.org/jawiki/latest/jawiki-latest-all-titles-in-ns0.gz | gunzip > jawiki-latest-all-titles-in-ns0
sample.rb
require 'csv'
original_data = {
wikipedia: 'jawiki-latest-all-titles-in-ns0',
hatena: 'keywordlist_furigana.csv'
}
CSV.open("custom.csv", 'w') do |csv|
original_data.each do |type, filename|
next unless File.file? filename
open(filename).each do |title|
title.strip!
next if title =~ %r(^[+-.$()?*/&%!"'_,]+)
next if title =~ /^[-.0-9]+$/
next if title =~ /Ambiguity avoidance/
next if title =~ /_\(/
next if title =~ /^PJ:/
next if title =~ /Characters/
next if title =~ /List/
title_length = title.length
if title_length > 3
score = [-36000.0, -400 * (title_length ** 1.5)].max.to_i
csv << [title, nil, nil, score, 'noun', 'General', '*', '*', '*', '*', title, '*', '*', type]
end
end
end
end
After that, run sample.rb
ruby sample.rb
Create a user dictionary custom.dic with the mecab-dict-index command based on the CSV file created in this way.
/usr/local/libexec/mecab/mecab-dict-index -d /usr/local/lib/mecab/dic/ipadic -u custom.dic -f utf-8 -t utf-8 custom.csv
Make sure you have custom.dic here.
After that, in the terminal, go to / usr / local / lib / mecab / dic / ipadic and
$ sudo vi dicrc
And
Finally, create a custom.dic directory.
userdic ="Location of the created dictionary directory"
Put in.
Let's implement the following code.
sample01.py
#coding:utf-8
import MeCab
tagger = MeCab.Tagger("-Ochasen")
result = tagger.parse("Cloud")
print result
At first, when you do not add a dictionary, "cloud" is
Kura Kura Kura Noun-Proper noun-General
Udo Udo noun-General
Whereas it was
Cloud cloud noun-General
became.
If you can do this, you're done. Thank you for your hard work.
Recommended Posts