Created
May 31, 2014 08:49
-
-
Save sugamasao/0d91c70e594ef46e429d to your computer and use it in GitHub Desktop.
表記ゆれチェッカーもどき
This file contains 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
# encoding:utf-8 | |
require 'natto' | |
require 'tempfile' | |
# これは何 | |
# 表記ゆれチェック用のツール(になったらいいなぁ)です | |
# 名詞を抜き出してソートすることで、末尾の表記ゆれを確認しやすくなるかな、と思います!! | |
# 使い方 | |
# gem の natto と homebrew的な何かでMeCabをインストールしてると使えるよ!! | |
# ここにメモがある http://d.hatena.ne.jp/seiunsky/20120108/1326008165 | |
# 辞書をipa以外の賢いヤツにするともっと捗るのかしら | |
class NotConsistentCheck | |
def file_collect(dir) | |
return Dir.glob(File.join(dir, "*.txt")) | |
end | |
def parse_text(files) | |
list = files.each_with_object([]) do |file, list| | |
File.readlines(file).each do |line| | |
next if line.strip.chomp.empty? | |
Natto::MeCab.new.parse(line).split("\n").delete_if{|n| n == "EOS"}.map do |m| | |
list << m.split("\t")[0] if m.split("\t")[1].include?("名詞") | |
end | |
end | |
end | |
return list.sort | |
end | |
def count_word(word_list) | |
word_list.chunk{|word| word} | |
end | |
def run(dir) | |
list = parse_text(file_collect(dir)) | |
puts "%5s|%-s" % ['count', 'word'] | |
count_word(list).each do |word, word_list| | |
puts "%5s|%-s" % [word_list.length, word] | |
end | |
end | |
end | |
if(File.directory?(ARGV[0].to_s)) | |
NotConsistentCheck.new.run(ARGV[0]) | |
else | |
puts "usage % #{__FILE__} /path/to/build_text/directory" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment