Skip to content

Instantly share code, notes, and snippets.

@naoty
Created December 4, 2013 11:11
Show Gist options
  • Select an option

  • Save naoty/7785916 to your computer and use it in GitHub Desktop.

Select an option

Save naoty/7785916 to your computer and use it in GitHub Desktop.
mecabのユーザー辞書を作成する書き捨てスクリプト
require "csv"
class String
# 参考: http://d.hatena.ne.jp/hirokan55/20100210/p1
def cost
[-36000, -400 * (self.length ** 1.5)].max.to_i
end
end
class UserdicSourceGenerator
def self.from(source_path)
new(source_path)
end
def initialize(source_path)
@source = CSV.table(source_path)
end
def to(output_path, opts = {})
CSV.open(output_path, "wb") do |csv|
@source.each do |source_row|
row = []
row << source_row[0] # 表層形
row << 0 # 左文脈ID
row << 0 # 右文脈ID
row << source_row[0].cost # コスト
row << "名詞" # 品詞
row << "固有名詞" # 品詞細分類1
row << "*" # 品詞細分類2
row << "*" # 品詞細分類3
row << "*" # 活用形
row << "*" # 活用型
row << source_row[1] # 原形
row << "*" # 読み
row << "*" # 発音
row << opts[:tag] # 登録タグ
csv << row
end
end
end
end
UserdicSourceGenerator.from("input.csv").to("output.csv", tag: "my_dictionary")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment