#! /usr/bin/env ruby # # PRIME: PRedictive Input Method Editor # $Id: prime,v 1.3 2003/05/16 19:46:26 komatsu Exp $ # # Copyright (C) 2001 Satoru Takabayashi # Copyright (C) 2002, 2003 Hiroyuki Komatsu # 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. # $LOAD_PATH.push(File::dirname($0)) require 'getoptlong' require 'prime/prime' require 'prime/session' require "jcode" include PogemoServer PRIME_VERSION = "0.5.1" class Session < SessionCore def initialize (pogemo, inpurt, outport) super @name = "prime" @id = "prime" add_command(:l, [:PATTERN], "look up PATTERN with hybrid matching") add_command(:lookup_hybrid, [:PATTERN], "look up PATTERN with hybrid matching") add_command(:lookup_prefix, [:PATTERN], "look up PATTERN with prefix matching") add_command(:lookup_exact, [:PATTERN], "look up PATTERN with exact matching") add_command(:learn_word, [:KEY, :VALUE, :PART, :CONTEXT, :SUFFIX, :REST], "learn and record a word to the user dictionary", 2) add_command(:reset_context, [], "reset context") add_command(:set_context, [:CONTEXT], "set context to CONTEXT") end def learn_word (key, value, part = nil, context = nil, suffix = nil, rest = nil) reply_successful { @pogemo.learn_word(key, value, part, context, suffix, rest) } end def l (pattern) reply_successful { @out.puts @pogemo.lookup_hybrid(pattern).to_text } end def set_context (context) reply_successful { @pogemo.set_context(context) } end def reset_context () reply_successful { @pogemo.set_context(nil) } end def lookup_hybrid (pattern) reply_successful { @out.puts @pogemo.lookup_hybrid(pattern).to_text } end def lookup_prefix (pattern) reply_successful { @out.puts @pogemo.lookup_prefix(pattern).to_text } end def lookup_exact (pattern) reply_successful { @out.puts @pogemo.lookup_exact(pattern).to_text } end end def show_usage puts "\ Usage: #{command_name} -u, --unix-socket=PATH run as Unix socket server with socket PATH -s, --tcp-server=PORT run as TCP server with port PORT -v, --version show the version and exit -h, --help show this help and exit -d, --debug run under debug mode " end def command_name $0.split('/').last end def parse_options options = Hash.new parser = GetoptLong.new parser.set_options(['--help', '-h', GetoptLong::NO_ARGUMENT], ['--version','-v', GetoptLong::NO_ARGUMENT], ['--unix-socket', '-u', GetoptLong::REQUIRED_ARGUMENT], ['--tcp-server', '-s', GetoptLong::REQUIRED_ARGUMENT], ['--debug', '-d', GetoptLong::NO_ARGUMENT]) parser.each_option do |name, arg| options[name.sub(/^--/, "")] = arg end if options['version'] puts "#{command_name} #{PRIME_VERSION}" exit 1 end if options['help'] show_usage exit 1 end return options end def main options = parse_options @prime = Prime.new if options['unix-socket'] socket_path = options['unix-socket'] server = UnixSocketServer.new(@prime, socket_path) elsif options['tcp-server'] tcp_port = options['tcp-server'] server = TaiyakiTCPServer.new(@prime, tcp_port) else server = StdioServer.new(@prime) end server.debug = true if options['debug'] || ENV['POGEMODEBUG'] server.accept end main