# prime/engine/engine-alphabet.rb
# $Id: engine-alphabet.rb,v 1.11 2005/03/07 07:51:32 komatsu Exp $
#
# Copyright (C) 2002, 2003, 2004 Hiroyuki Komatsu <komatsu@taiyaki.org>
#     All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
# You can redistribute it and/or modify it under the terms of 
# the GNU General Public License version 2.
#

require 'prime/engine/engine'
$engine_class_name = 'PrimeEngineAlphabet'

class PrimeEngineAlphabet < PrimeEngine
  @@annotations = {
    "-"  => "半角「マイナス」",
    "−" => "全角「マイナス」",
    "ー" => "長音",
    "─" => "罫線",
    "一" => "漢数「いち」",
    "―" => "記号「ダッシュ」",
    "‐" => "記号「ハイフン」",
    "x"  => "半角「エックス」",
    "X"  => "半角「エックス」",
    "ｘ" => "全角「エックス」",
    "Ｘ" => "全角「エックス」",
    "×" => "記号「かける」",
    "o"  => "半角「オー」",
    "O"  => "半角「オー」",
    "0"  => "半角「ゼロ」",
    "ｏ" => "全角「オー」",
    "Ｏ" => "全角「オー」",
    "０" => "全角「ゼロ」",
    "〇" => "漢数「ゼロ」",
    "○" => "記号「まる」",
    "◯" => "記号「まる2」",
    "ニ" => "全角カタカナ",
    "二" => "漢数「に」",
    "へ" => "ひらがな",
    "ヘ" => "カタカナ",
    "ぺ" => "ひらがな",
    "ペ" => "カタカナ",
    "べ" => "カタカナ",
    "ベ" => "ひらがな",
    "り" => "ひらがな",
    "リ" => "カタカナ",
    "|"  => "半角",
    "｜" => "全角",
    "."  => "半角",
    "．" => "全角",
    ","  => "半角",
    "，" => "全角",
  }

  def initialize
    super

    @name = "Alphabet engine"
    @id   = "alphabet"
    @description = "Alphabet engine"
  end

  def search(query)
    if query.input == [""] then
      return PrimeWordList.new()
    end

    case query.method
    when :overall then
      results = search_overall(query)
    else
      results = PrimeWordList.new()
    end
    if query.pos == '数詞' then
      query.input.each {|input|
        numeral = PrimeWord.new(input, input, query.pos, 10000)
        results.push(numeral)
      }
    end
    return results
  end

  def get_pos_data (string)
    pos_data = {}
    if string =~ /^[0-9.,+-]+/ then
      matched_string = $&
      pos_data.list_push(matched_string, '数詞')
    end
    return pos_data
  end

  private
  def search_overall(query)
    results = PrimeWordList.new()
    query.input.each {|string|
      if PRIME_ENV['typing_method'] == 'tcode' then
        if string.length == 1 then 
          results << PrimeWord.new(string.downcase, string,
                                   "特殊", 20000) # T-Code での 1文字入力
        end
      end
      if string == string.upcase and string != string.downcase then
        results << PrimeWord.new(string.downcase, string,
                                 "未知語", 17000) # 英単語(大文字)
      else
        results << PrimeWord.new(string.downcase, string,
                                 "未知語", 8000) # 英単語
      end
      ### FIXME: 違うエンジンに変える.
      (hiragana, pending) = PrimeTypeConv::convert(string)
      if pending.empty? then
        results << PrimeWord.new(hiragana.downcase, hiragana.katakana,
                                 "未知語", 8000) # カタカナ
        results << PrimeWord.new(hiragana.downcase, hiragana,
                                 "未知語", 8000) # ひらがななど
      end
    }

    results.each { | word |
      if @@annotations.has_key?( word.literal ) and
          (not word.data.has_key?('form')) then
        word.data['form'] = @@annotations[ word.literal ]
      end
    }
    return results
  end
end
