# coding: UTF-8
module Fluent
  class KuromojiOutput < Output
    Fluent::Plugin.register_output('kuromoji', self)

    # Define `router` method of v0.12 to support v0.10 or earlier
    unless method_defined?(:router)
      define_method("router") { Fluent::Engine }
    end

    config_param :target_key, :string
    config_param :add_tag_prefix, :string
    config_param :dictionary_path, :string

    def initialize
      super
      require 'kuromoji'
    end

    def configure(conf)
      super
      @core = Kuromoji::Core.new(@dictionary_path)
    end

    def emit(tag, es, chain)
      es.each do |time, record|
        tokens = @core.tokenize_with_hash(record[@target_key])
        tokens.each do |token|
          router.emit(@add_tag_prefix + '.' + tag, time, token)
        end
      end
      chain.next
    end
  end
end