[Prime-cvs] CVS update: prime/lib

Back to archive index

Hiroyuki Komatsu komat****@users*****
2005年 1月 18日 (火) 11:04:32 JST


Index: prime/lib/session.rb
diff -u prime/lib/session.rb:1.4.4.17 prime/lib/session.rb:1.4.4.18
--- prime/lib/session.rb:1.4.4.17	Sun Jan 16 04:47:15 2005
+++ prime/lib/session.rb	Tue Jan 18 11:04:32 2005
@@ -1,5 +1,5 @@
 # session.rb
-# $Id: session.rb,v 1.4.4.17 2005/01/15 19:47:15 komatsu Exp $
+# $Id: session.rb,v 1.4.4.18 2005/01/18 02:04:32 komatsu Exp $
 #
 # Copyright (C) 2001 Satoru Takabayashi <sator****@namaz*****>
 # Copyright (C) 2002, 2003, 2004 Hiroyuki Komatsu <komat****@taiya*****>
@@ -265,13 +265,15 @@
 class SessionPrime2 < SessionPrime
   def initialize (prime, version)
     super(prime, version)
-    @sessions = {}
-    @session_no = 0
 
     add_command(:session_start, [],
                 "start a session and return the session id.")
     add_command(:session_end, [:SESSION],
                 "close the session specified with the session id.")
+    add_command(:session_language_set, [:SESSION, :LANGUAGE],
+                "Set the target language (Japanese or English).")
+    add_command(:session_language_get, [:SESSION],
+                "Get the current target language.")
     add_command(:edit_insert, [:SESSION, :STRING],
                 "insert this string into the preediting string.")
     add_command(:edit_delete, [:SESSION],
@@ -312,18 +314,18 @@
     add_command(:conv_predict, [:SESSION, :METHOD],
                 "predict candidate words with the method", 1)
     add_command(:conv_select,  [:SESSION, :INDEX],
-                "select an index word to a conversion.")
+                "select an indexed word to a conversion.")
     add_command(:conv_commit,  [:SESSION],
                 "commit the current conversion.")
 
     add_command(:modify_start, [:SESSION],
                 "Start a modification of the selected conversion.")
     add_command(:segment_select, [:SESSION, :INDEX],
-                "")
+                "set an indexed word to the current segment.")
     add_command(:segment_reconvert, [:SESSION],
-                "")
+                "convert the current segment again.")
     add_command(:segment_commit, [:SESSION],
-                "")
+                "fix up the selected word in the current segment.")
     add_command(:modify_get_candidates, [:SESSION],
                 "")
     add_command(:modify_get_conversion, [:SESSION],
@@ -352,7 +354,17 @@
   end
 
   def session_end (session)
-    @sessions[session] = nil
+    @prime.session_end()
+    return reply_successful()
+  end
+
+  def session_language_get (session)
+    language =****@prime*****_language_get(session)
+    return reply_successful(language)
+  end
+
+  def session_language_set (session, language)
+    @prime.session_language_set(session, language)
     return reply_successful()
   end
 
Index: prime/lib/prime2.rb
diff -u prime/lib/prime2.rb:1.1.2.10 prime/lib/prime2.rb:1.1.2.11
--- prime/lib/prime2.rb:1.1.2.10	Sun Jan 16 04:47:15 2005
+++ prime/lib/prime2.rb	Tue Jan 18 11:04:32 2005
@@ -1,5 +1,5 @@
 # prime2.rb: Module for PRIME2 protocol.
-# $Id: prime2.rb,v 1.1.2.10 2005/01/15 19:47:15 komatsu Exp $
+# $Id: prime2.rb,v 1.1.2.11 2005/01/18 02:04:32 komatsu Exp $
 #
 # Copyright (C) 2004 Hiroyuki Komatsu <komat****@taiya*****>
 #     All rights reserved.
@@ -10,6 +10,14 @@
 
 require 'suikyo/suikyo-composer'
 
+require 'prime/engine/engine-basic'
+require 'prime/engine/engine-english'
+require 'prime/engine/engine-userdict2'
+require 'prime/engine/engine-userdict2-en'
+require 'prime/engine/engine-personaldict'
+require 'prime/engine/engine-alphabet'
+require 'prime/engine/engine-number'
+
 module Prime2
   def initialize_prime2 ()
     @sessions   = {}
@@ -33,6 +41,17 @@
     return true
   end
 
+  ## This sets the language to the current session.
+  ## Now Japanese and English are avairable.
+  def session_language_set (session, language)
+    @sessions[session].set_language(language)
+  end
+
+  ## This returns the current language on the session.
+  def session_language_get (session)
+    return @sessions[session].get_language()
+  end
+
   ## 
   ## Composition methods
   ##
@@ -163,42 +182,52 @@
 
   ## This returns a PrimeConversionList.
   def conv_convert (session, method = nil)
-    if PRIME_ENV['typing_method'] == 'tcode' or 
-        PRIME_ENV['typing_method'] == 'handwrite' then
-      candidates = lookup_direct_all(string)
-    else
-      composer = session_get_composer(session)
-      context  = session_get_context(session)
-
-      conversions_compact  = convert_compact( composer, context)
-      ## FIXME: Delete the magic number.
-      ## FIXME: (2004-12-22) <Hiro>
-      conversions_compact.add_score( 50000 )
-      conversions_japanese = convert_japanese(composer, context)
-      conversions_overall  = convert_overall( composer, context)
-
-      conversions = PrimeConversionList::merge( conversions_compact,
-                                                conversions_japanese,
-                                                conversions_overall )
-    end
+    case session_language_get(session)
+    when "Japanese" then
+      if PRIME_ENV['typing_method'] == 'tcode' or 
+          PRIME_ENV['typing_method'] == 'handwrite' then
+        candidates = lookup_direct_all(string)
+      else
+        conversions_compact  = convert_compact(session)
+        ## FIXME: Delete the magic number.
+        ## FIXME: (2004-12-22) <Hiro>
+        conversions_compact.add_score( 50000 )
+        conversions_japanese = convert_japanese(session)
+        conversions_overall  = convert_overall(session)
+
+        conversions = PrimeConversionList::merge( conversions_compact,
+                                                  conversions_japanese,
+                                                  conversions_overall )
+      end
 
-    session_set_conversions(session, conversions)
-    return conversions
+      session_set_conversions(session, conversions)
+      return conversions
+
+    when "English" then
+      conversions = convert_prefix(session)
+      session_set_conversions(session, conversions)
+      return conversions
+    end
   end
 
   def conv_predict (session, method = nil)
-    if PRIME_ENV['typing_method'] == 'tcode' or 
-        PRIME_ENV['typing_method'] == 'handwrite' then
-      candidates = lookup_direct(string)
-    else
-      composer = session_get_composer(session)
-      context  = session_get_context(session)
+    case session_language_get(session)
+    when "Japanese" then
+      if PRIME_ENV['typing_method'] == 'tcode' or 
+          PRIME_ENV['typing_method'] == 'handwrite' then
+        candidates = lookup_direct(string)
+      else
+        conversions = convert_compact(session)
+      end
 
-      conversions = convert_compact(composer, context)
-    end
+      session_set_conversions(session, conversions)
+      return conversions
 
-    session_set_conversions(session, conversions)
-    return conversions
+    when "English" then
+      conversions = convert_prefix(session)
+      session_set_conversions(session, conversions)
+      return conversions
+    end
   end
 
   def conv_select (session, index)
@@ -321,6 +350,10 @@
 #     @sessions[session].set_candidates(wordlist)
 #   end
 
+  def session_get_engines (session)
+    return @sessions[session].get_engines()
+  end
+
   def session_set_conversions (session, conversions)
     @sessions[session].set_conversions(conversions)
   end
@@ -348,44 +381,44 @@
 
   ## This is a wrapper for convert_*.  This converts query to
   ## a PrimeConversionList insted of PrimeWordList and returns it.
-  def convert (query)
-    wordlist = search(query)
+  def convert (session, query)
+    query.input.uniq!()
+    words_list = session_get_engines(session).command(:search, query)
+    wordlist = PrimeWordList::merge(words_list)
+
     PrimeWordList::attach_prefix(@context, wordlist)
     return _adhoc_wordlist_to_conversionlist( wordlist )
   end
   private :convert
 
-  def convert_prefix (composer, context)
+  def convert_prefix (session)
     # 「よ→予測」
+    composer = session_get_composer(session)
+    context  = session_get_context(session)
     expansion = composer.edit_get_expansion()
     query = PrimeQuery.new(expansion, nil, :prefix, context)
-    return convert(query)
+    return convert(session, query)
   end
 
-  def convert_exact (composer, context)
+  def convert_exact (session)
     # 「よそく→予測」
+    composer = session_get_composer(session)
+    context  = session_get_context(session)
     conversion = composer.edit_get_conversion()
     query = PrimeQuery.new(conversion, nil, :exact, context)
-    return convert(query)
-  end
-
-  def convert_raw (composer, context)
-    ## FIXME: This method is an ad-hoc routine for search_japanese.
-    ## FIXME: <komat****@taiya*****> (2004-02-28)
-    raw_input = composer.edit_get_raw_input()
-    query = PrimeQuery.new(raw_input, nil, :exact, @context)
-    return convert(query)
+    return convert(session, query)
   end
 
-  def convert_overall (composer, context)
+  def convert_overall (session)
     # 「1+1=→2」, 「aiueo→アイウエオ」
+    composer = session_get_composer(session)
     raw_input = composer.edit_get_raw_input()
     query = PrimeQuery.new(raw_input, nil, :overall)
-    return convert(query)
+    return convert(session, query)
   end
 
-  def convert_compact (composer, context)
-    conversion_prefix = convert_prefix(composer, context).first()
+  def convert_compact (session)
+    conversion_prefix = convert_prefix(session).first()
 
     ## If the result of search_prefix is empty, this method stops the following
     ## search_japanese_uniclause for the quickness.
@@ -396,7 +429,7 @@
     ## If the result of convert_japanese_uniclause exists and the score of it
     ## is greater than the result of convert_prefix, the return conversion
     ## becomes the convert_japanese_uniclause's one.
-    conversion_japanese = convert_japanese_uniclause(composer, context).first()
+    conversion_japanese = convert_japanese_uniclause(session).first()
     if conversion_japanese.nil?() then
       conversion = conversion_prefix
     elsif conversion_japanese.score < conversion_prefix.score then
@@ -462,7 +495,8 @@
   private :predict_next_segment
 
   ## This returns a PrimeConversionList.
-  def convert_japanese (composer, context)
+  def convert_japanese (session)
+    composer = session_get_composer(session)
     segments_list = convert_japanese_process_segments_list(composer)
     conversions = []
 
@@ -489,7 +523,8 @@
 
   ## This returns a PrimeWordList.
   ## FIXME: Change the method name.
-  def convert_japanese_uniclause (composer, context)
+  def convert_japanese_uniclause (session)
+    composer = session_get_composer(session)
     segments_list = convert_japanese_process_segments_list(composer, 1)
     conversions = PrimeConversionList.new()
 
@@ -584,15 +619,30 @@
   end
   private :convert_japanese_get_score
 
+
   class PrimeSession
     def initialize ()
-      @composer    = initialize_composer()
-      @conversions = PrimeConversionList.new()
-      @target      = ""
-      @context     = ""
+      @language     = "Japanese"
+      @composers = {}
+      initialize_composer(@language)
+      @engines_list = {}
+      initialize_engines(@language)
+      @conversions  = PrimeConversionList.new()
+      @target       = ""
+      @context      = ""
+    end
+
+    def initialize_composer (language)
+      case language
+      when "Japanese" then 
+        @composers[language] = initialize_composer_japanese()
+      when "English" then
+        @composers[language] = initialize_composer_english()
+      end
+      return @composers
     end
 
-    def initialize_composer ()
+    def initialize_composer_japanese ()
       composer = SuikyoComposer.new()
       composer.set_table(PRIME_ENV['suikyo_tables'])
       composer.set_reverse_table(PRIME_ENV['suikyo_reverse_tables'])
@@ -631,8 +681,75 @@
       return composer
     end
 
+    def initialize_composer_english ()
+      composer = SuikyoComposer.new()
+      return composer
+    end
+
+    def initialize_engines (language)
+      case language
+      when "Japanese" then 
+        @engines_list[language] = initialize_engines_japanese()
+      when "English" then
+        @engines_list[language] = initialize_engines_english()
+      end
+      return @engines_list
+    end
+
+    def initialize_engines_japanese ()
+      engine_classes = [
+        PrimeEngineBasic,
+        PrimeEngineUserdict2,
+        PrimeEnginePersonalDict,
+        PrimeEngineAlphabet,
+        PrimeEngineNumber,
+      ]
+      return initialize_engines_internal(engine_classes)
+    end
+
+    def initialize_engines_english ()
+      engine_classes = [
+        PrimeEngineEnglish,
+        PrimeEngineUserdict2English,
+        PrimeEnginePersonalDict,
+        PrimeEngineAlphabet,
+        PrimeEngineNumber,
+      ]
+      return initialize_engines_internal(engine_classes)
+    end
+
+    def initialize_engines_internal (engine_classes)
+      engines = engine_classes.map { |engine_class| engine_class.new() }
+
+      def engines.command (method, *args)
+        self.map {|engine|
+          engine.send(method, *args)
+        }
+      end
+
+      return engines
+    end
+
+
     def get_composer ()
-      return @composer
+      return @composers[@language]
+    end
+    def get_engines ()
+      return @engines_list[@language]
+    end
+
+    def set_language (language)
+      @language = language
+      unles****@compo*****_key?(language) then
+        initialize_composer(language)
+      end
+      unless @engines_list.has_key?(language) then
+        initialize_engines(language)
+      end
+      return @language
+    end
+    def get_language ()
+      return @language
     end
 
     def set_conversions (conversions)


Prime-cvs メーリングリストの案内
Back to archive index