[Prime-cvs] CVS update: prime/lib

Back to archive index

Hiroyuki Komatsu komat****@users*****
2004年 12月 20日 (月) 17:45:37 JST


Index: prime/lib/session.rb
diff -u prime/lib/session.rb:1.4.4.12 prime/lib/session.rb:1.4.4.13
--- prime/lib/session.rb:1.4.4.12	Fri Dec 17 23:11:32 2004
+++ prime/lib/session.rb	Mon Dec 20 17:45:37 2004
@@ -1,5 +1,5 @@
 # session.rb
-# $Id: session.rb,v 1.4.4.12 2004/12/17 14:11:32 komatsu Exp $
+# $Id: session.rb,v 1.4.4.13 2004/12/20 08:45:37 komatsu Exp $
 #
 # Copyright (C) 2001 Satoru Takabayashi <sator****@namaz*****>
 # Copyright (C) 2002, 2003, 2004 Hiroyuki Komatsu <komat****@taiya*****>
@@ -314,24 +314,30 @@
     add_command(:conv_commit,  [:SESSION],
                 "commit the current conversion.")
 
-#     add_command(:modify_start, [:SESSION],
-#                 "")
-#     add_command(:modify_cursor_left, [:SESSION],
-#                 "")
-#     add_command(:modify_cursor_right, [:SESSION],
-#                 "")
-#     add_command(:modify_cursor_left_edge, [:SESSION],
-#                 "")
-#     add_command(:modify_cursor_right_edge, [:SESSION],
-#                 "")
+    add_command(:modify_start, [:SESSION],
+                "Start a modification of the selected conversion.")
+    add_command(:modify_select, [:SESSION, :INDEX],
+                "")
+    add_command(:modify_reconvert, [:SESSION],
+                "")
+    add_command(:modify_commit, [:SESSION],
+                "")
+    add_command(:modify_get_candidates, [:SESSION],
+                "")
+    add_command(:modify_get_conversion, [:SESSION],
+                "")
+    add_command(:modify_cursor_left, [:SESSION],
+                "")
+    add_command(:modify_cursor_right, [:SESSION],
+                "")
+    add_command(:modify_cursor_left_edge, [:SESSION],
+                "")
+    add_command(:modify_cursor_right_edge, [:SESSION],
+                "")
 #     add_command(:modify_cursor_expand, [:SESSION],
 #                 "")
 #     add_command(:modify_cursor_shrink, [:SESSION],
 #                 "")
-#     add_command(:modify_select, [:SESSION, INDEX],
-#                 "")
-#     add_command(:modify_commit, [:SESSION],
-#                 "")
 
   end
 
@@ -428,8 +434,8 @@
   end
 
   def conv_predict (session, method = nil)
-    candidates =****@prime*****_predict(session)
-    return reply_successful(candidates.to_text)
+    conversions =****@prime*****_predict(session)
+    return reply_successful( conversions.to_text() )
   end
 
   def conv_select (session, index)
@@ -441,6 +447,58 @@
     @prime.conv_commit(session)
     return reply_successful()
   end
+
+  ##
+  ## Conversion methods
+  ##
+  def modify_start (session)
+    @prime.modify_start(session)
+    return modify_get_conversion(session)
+  end
+
+  def modify_select (session, index_no)
+    @prime.modify_select(session, index_no.to_i)
+    return modify_get_conversion(session)
+  end
+
+  def modify_reconvert (session)
+    @prime.modify_reconvert(session)
+    return modify_get_candidates(session)
+  end
+
+  def modify_commit (session)
+  end
+
+  def modify_get_candidates (session)
+    segment =****@prime*****_get_segment(session).to_text_candidates()
+    return reply_successful(segment)
+  end
+
+  def modify_get_conversion (session)
+    conversion =****@prime*****_get_conversion(session).join("\t")
+    return reply_successful(conversion)
+  end
+
+  def modify_cursor_left (session)
+    @prime.modify_cursor_left(session)
+    return modify_get_conversion(session)
+  end
+  def modify_cursor_right (session)
+    @prime.modify_cursor_right(session)
+    return modify_get_conversion(session)
+  end
+  def modify_cursor_left_edge (session)
+    @prime.modify_cursor_left_edge(session)
+    return modify_get_conversion(session)
+  end
+  def modify_cursor_right_edge (session)
+    @prime.modify_cursor_right_edge(session)
+    return modify_get_conversion(session)
+  end
+  def modify_cursor_expand (session)
+  end
+  def modify_cursor_shrink (session)
+  end
 end
 
 # ----
Index: prime/lib/prime2.rb
diff -u prime/lib/prime2.rb:1.1.2.4 prime/lib/prime2.rb:1.1.2.5
--- prime/lib/prime2.rb:1.1.2.4	Mon Dec 20 00:03:48 2004
+++ prime/lib/prime2.rb	Mon Dec 20 17:45:37 2004
@@ -1,5 +1,5 @@
 # prime2.rb: Module for PRIME2 protocol.
-# $Id: prime2.rb,v 1.1.2.4 2004/12/19 15:03:48 komatsu Exp $
+# $Id: prime2.rb,v 1.1.2.5 2004/12/20 08:45:37 komatsu Exp $
 #
 # Copyright (C) 2004 Hiroyuki Komatsu <komat****@taiya*****>
 #     All rights reserved.
@@ -121,6 +121,10 @@
     session_set_context(session, "")
   end
 
+  def context_set_previous_segment (session, segment)
+    context_set_previous_word(session, segment.get_base() )
+  end
+
   def context_set_previous_word (session, word)
     session_set_context(session, word)
   end
@@ -139,11 +143,11 @@
       composer = session_get_composer(session)
       context  = session_get_context(session)
 
-      conversions_compact  = convert_compact(composer, context)
+      conversions_compact  = convert_compact( composer, context)
       conversions_japanese = convert_japanese(composer, context)
-      conversions_overall  = convert_overall(composer, context)
+      conversions_overall  = convert_overall( composer, context)
 
-      ## FIXME: Shink verbose candidates.
+      ## FIXME: Shrink verbose candidates.
       ## FIXME: (2004-12-19) <Hiro>
       conversions = PrimeConversionList.new( conversions_compact |
                                                conversions_japanese |
@@ -154,6 +158,47 @@
     return conversions
   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)
+
+      conversions = convert_compact(composer, context)
+    end
+
+    session_set_conversions(session, conversions)
+    return conversions
+  end
+
+  def conv_select (session, index)
+    selected_candidate = session_set_selection(session, index)
+    return selected_candidate
+  end
+
+  def conv_commit (session)
+    conversion = session_get_selection(session)
+
+    conversion.segments.each { | segment |
+      learn_segment( segment, session_get_context(session) )
+
+      context_reset(session)
+      context_set_previous_segment(session, segment)
+    }
+  end
+
+  def learn_segment (segment, context)
+    base         = segment.get_base()
+    base_reading = segment.get_base_reading()
+    pos          = segment.get_pos()
+    adjunct      = segment.get_adjunct()
+    rest         = ""
+    @engines.command(:learn_word,
+                     base_reading, base, pos, context, adjunct, rest)
+  end
+
   def _adhoc_wordlist_to_conversionlist (wordlist)
     conversion_list = []
     wordlist.length.times { | index |
@@ -168,43 +213,60 @@
     return PrimeConversionList.new(conversion_list)
   end
 
-  def conv_predict (session, method = nil)
-    query_string = edit_get_query_string(session)
+  ##
+  ## Modification methods
+  ##
+  def modify_start (session)
+    conversion = session_get_selection(session)
+    conversion.position_right_edge()
+  end
 
-    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)
+  def modify_select (session, index_no)
+    conversion = session_get_selection(session)
+    conversion.candidate_select(index_no)
+  end
 
-      words_compact = convert_compact(composer, context)
-      candidates = PrimeWordList::merge_with_label(@context, words_compact)
-    end
+  def modify_commit (session)
+  end
+
+  def modify_reconvert (session)
+    conversion = session_get_selection(session)
+    segment    = conversion.get_segment()
 
-    conversions = PrimeConversionList.new( PrimeConversion.new(candidates) )
+    segment_reconvert(segment)
 
-    session_set_candidates(session, candidates)
-    return candidates
+    return modify_get_segment(session)
   end
 
-  def conv_select (session, index)
-    selected_candidate = session_set_selection(session, index)
-    return selected_candidate
+  def modify_get_segment (session)
+    conversion = session_get_selection(session)
+    return conversion.get_segment()
   end
 
-  def conv_commit (session)
-    selected_candidate = session_get_selection(session)
-    reading = selected_candidate.pron
-    literal = selected_candidate.literal
-    pos     = selected_candidate.pos
-    ending  = selected_candidate.conjugation 
-    rest    = selected_candidate.rest 
-
-    context = session_get_context(session)
-    learn_word(reading, literal, pos, context, ending, rest)
-    word = literal + ending + rest
-    context_set_previous_word(session, word)
+  def modify_get_conversion (session)
+    conversion = session_get_selection(session)
+    return conversion.get_conversion()
+  end
+
+  def modify_cursor_left (session)
+    conversion = session_get_selection(session)
+    conversion.position_left()
+  end
+  def modify_cursor_right (session)
+    conversion = session_get_selection(session)
+    conversion.position_right()
+  end
+  def modify_cursor_left_edge (session)
+    conversion = session_get_selection(session)
+    conversion.position_left_edge()
+  end
+  def modify_cursor_right_edge (session)
+    conversion = session_get_selection(session)
+    conversion.position_right_edge()
+  end
+  def modify_cursor_expand (session)
+  end
+  def modify_cursor_shrink (session)
   end
 
   private
@@ -216,9 +278,9 @@
     return composer
   end
 
-  def session_set_candidates (session, wordlist)
-    @sessions[session].set_candidates(wordlist)
-  end
+#   def session_set_candidates (session, wordlist)
+#     @sessions[session].set_candidates(wordlist)
+#   end
 
   def session_set_conversions (session, conversions)
     @sessions[session].set_conversions(conversions)
@@ -228,7 +290,7 @@
   end
 
   def session_set_selection (session, index)
-    @sessions[session].set_selection(index)
+    return @sessions[session].set_selection(index)
   end
   def session_get_selection (session)
     return @sessions[session].get_selection()
@@ -304,8 +366,6 @@
       conversion = conversion_japanese
     end
 
-    return PrimeConversionList.new( [conversion] )
-
     ## Predict a next segment of the conversion.
     next_segment = predict_next_segment( conversion )
     if next_segment.nil? then
@@ -317,7 +377,6 @@
     return PrimeConversionList.new( [conversion, conversion2] )
   end
 
-
   ## This predicts candidate words as a next word for the specified conversion
   ## data.  For example when the specified conversion means "どうもありがとう",
   ## one of the results would contain "ございます".
@@ -346,17 +405,18 @@
 
     ## Create a PrimeSegment from the first value of a PrimeWord.
     next_words = next_words[0,1]
+
     next_word  = next_words.first
-    next_word.prefix = Prime::get_prefix(context, word.literal)
+    next_word.prefix = Prime::get_prefix(context, next_word.literal)
 
-    reading     = next_word.to_text_pron()
-    base        = ""
-    pos         = nil
-    adjunct     = ""
-    pos_adjunct = nil
+    reading      = next_word.to_text_pron()
+    base_reading = ""
+    pos          = nil
+    adjunct      = ""
+    pos_adjunct  = nil
 
-    next_segment =
-      PrimeSegment.new(reading, base, pos, adjunct, pos_adjunct, context)
+    next_segment = PrimeSegment.new(reading, base_reading, pos,
+                                    adjunct, pos_adjunct, context)
     next_segment.set_candidates(next_words, 0)
     return next_segment
   end
@@ -372,7 +432,7 @@
         conversions += convert_from_segment( segments.first() )
       else
         segments.each { | segment |
-          query = PrimeQuery.new( [segment.base], segment.pos )
+          query = PrimeQuery.new( [segment.base_reading], segment.pos )
           words = search(query)
           words.each { | word |
             word.conjugation     = segment.adjunct
@@ -405,7 +465,7 @@
   ## FIXME: (2004-12-19) <Hiro>.
   def convert_from_segment (segment)
     conversions = PrimeConversionList.new()
-    query = PrimeQuery.new( [segment.base], segment.pos )
+    query = PrimeQuery.new( [segment.base_reading], segment.pos )
     words = search(query)
 
     words.length.times { | index |
@@ -421,6 +481,18 @@
   end
   private :convert_from_segment
 
+  def segment_reconvert (segment, context = nil)
+    reading = segment.reading
+    words1 = search_raw(reading)
+    words2 = search_overall(reading)
+    words3 = search_japanese_uniclause(reading)
+    words  = PrimeWordList::merge(words1, words2, words3)
+
+    candidates      = segment.get_candidates()
+    candidate_index = segment.get_candidate_index()
+    segment.set_candidates( ( candidates | words ), candidate_index )
+  end
+
   def convert_japanese_process_segments_list (composer, threshold = 4)
     string = composer.edit_get_surface_string()
     (depth, segments_list) = guess_clauses_internal(string, 1, nil, threshold)
@@ -434,9 +506,10 @@
         min_length = segments.length
       elsif segments.length == min_length then
         prime_segments = []
-        segments.each { | (base, pos, adjunct, pos_adjunct, engines) |
-          prime_segment =
-            PrimeSegment.new(base + adjunct, base, pos, adjunct, pos_adjunct)
+        segments.each { | (base_reading, pos, adjunct, pos_adjunct, engines) |
+          prime_segment = PrimeSegment.new(base_reading + adjunct,
+                                           base_reading, pos,
+                                           adjunct, pos_adjunct)
           prime_segments.push(prime_segment)
         }
         conversions.push(prime_segments)
@@ -473,8 +546,7 @@
   class PrimeSession
     def initialize ()
       @composer    = initialize_composer()
-      @candidates  = PrimeWordList.new()
-      @conversions = []
+      @conversions = PrimeConversionList.new()
       @target      = ""
       @context     = ""
     end
@@ -529,21 +601,12 @@
       return @conversions
     end
 
-    ## Obsolete
-    def set_candidates (wordlist)
-      @candidates = wordlist
-    end
-    ## Obsolete
-    def get_candidates ()
-      return @candidates
-    end
-
     def set_selection (index_no)
-      @target = @candidates[index_no]
-      return @target
+      @conversions.set_conversion_index(index_no)
+      return @conversions[index_no]
     end
     def get_selection ()
-      return @target
+      retur****@conve*****_conversion()
     end
 
     def set_context (context)
@@ -556,9 +619,28 @@
 end
 
 class PrimeConversionList < Array
+  def initialize (*arguments)
+    super(*arguments)
+    @index_no = -1
+  end
+
+  def set_conversion_index (index_no)
+    @index_no = index_no
+  end
+  def get_conversion ()
+    if @index_no == -1 then
+      return nil
+    else
+      return self[@index_no]
+    end
+  end
+
   def to_text ()
-    texts = self.map { | conversion | conversion.to_text() }
-    return texts.join("\n")
+    text = @index_no.to_s
+    self.each { | conversion |
+      text += "\n" + conversion.to_text()
+    }
+    return text
   end
 
   def to_text_debug ()
@@ -570,23 +652,73 @@
 end
 
 class PrimeConversion
-  attr_reader :score
+  attr_reader :score, :segments
   def initialize (segments, score = 0, position = -1)
-    @segments = segments
-    @position = position
+    @segments = segments  # Array of PrimeSegment
+    @position = position  # position of segments, -1 means non-selection.
     @score    = score
   end
 
+  def dup ()
+    return PrimeConversion.new(@segments.dup, @score, @position)
+  end
+
+  def get_segment ()
+    return @segments[@position]
+  end
+
+  def get_conversion ()
+    if @position == -1 then
+      return [ get_literal(), "", "" ]
+    end
+
+    left  = ""
+    right = ""
+
+    @segments[0, @position].each { | segment |
+      left += segment.get_literal()
+    }
+    cursor = @segments[@position].get_literal()
+    @segments[(@position + 1)..-1].each { | segment |
+      right += segment.get_literal()
+    }
+    return [left, cursor, right]
+  end
+
   def get_literal()
     literal = ""
     @segments.each { | segment | literal += segment.get_literal() }
     return literal
   end
+  
+  def reconvert ()
+    segment = get_segment()
+    segment.reconvert()
+  end
 
   def segment_insert (segment)
     ## FIXME: Condiser @position.
     ## FIXME: (2004-12-19) <Hiro>
-    @segments.push(segments)
+    @segments.push(segment)
+  end
+
+  def candidate_select (index_no)
+    @segments[@position].set_candidate_index(index_no)
+  end
+
+  def position_right ()
+    @position += 1
+    @position %=****@segme*****()
+  end
+  def position_right_edge ()
+    @position =****@segme*****() - 1
+  end
+  def position_left ()
+    @position -= 1
+    @position %=****@segme*****()
+  end
+  def position_left_edge ()
+    @position = 0
   end
 
   def to_text_debug ()
@@ -611,26 +743,29 @@
 
 
 class PrimeSegment
-  attr_reader :reading, :base, :pos, :adjunct, :pos_adjunct, :context
+  attr_reader :reading, :base_reading, :pos, :adjunct, :pos_adjunct, :context
   def initialize (reading,
-                  base = nil, pos = nil, adjunct = "", pos_adjunct = nil,
+                  base_reading = nil,
+                  pos = nil,
+                  adjunct = "",
+                  pos_adjunct = nil,
                   context = nil)
     @reading = reading
 
     ## The following data is a query guideline.
-    @base        = base
-    @pos         = pos
-    @adjunct     = adjunct
-    @pos_adjunct = pos_adjunct
-    @context     = context
+    @base_reading = base_reading
+    @pos          = pos
+    @adjunct      = adjunct
+    @pos_adjunct  = pos_adjunct
+    @context      = context
 
     @candidates      = PrimeWordList.new()
     @candidate_index = -1
   end
 
   def dup ()
-    segment = 
-      PrimeSegment.new(@reading, @base, @pos, @adjunct, @pos_adjunct, @context)
+    segment = PrimeSegment.new(@reading, @base_reading, @pos,
+                               @adjunct, @pos_adjunct, @context)
     segment.set_candidates(@candidates, @candidate_index)
     return segment
   end
@@ -639,10 +774,26 @@
     @candidates      = candidates
     @candidate_index = candidate_index
   end
+  def get_candidates ()
+    return @candidates
+  end
+  def get_candidate_index ()
+    return @candidate_index
+  end
   def set_candidate_index (candidate_index)
+    if candidate_index > @candidates.length or candidate_index < 0 then
+      @candidate_index = -1
+    end
     @candidate_index = candidate_index
   end
 
+  def get_reading ()
+    if @candidate_index == -1 then
+      return @reading
+    else
+      return @candidates[@candidate_index].to_text_pron()
+    end
+  end
   def get_literal ()
     if @candidate_index == -1 then
       return @reading
@@ -650,6 +801,39 @@
       return @candidates[@candidate_index].to_text_literal()
     end
   end
+
+  def get_base_reading ()
+    if @candidate_index == -1 then
+      return (@base_reading or @reading)
+    else
+      return @candidates[@candidate_index].pron
+    end
+  end
+  def get_base ()
+    if @candidate_index == -1 then
+      return (@base_reading or @reading)
+    else
+      return @candidates[@candidate_index].literal
+    end
+  end
+  def get_pos ()
+    if @candidate_index == -1 then
+      return (@pos or "")
+    else
+      return @candidates[@candidate_index].pos
+    end
+  end
+  def get_adjunct ()
+    if @candidate_index == -1 then
+      if @base_reading.nil? then
+        return ""  # @base_reading == @reading
+      else
+        return @adjunct
+      end
+    else
+      return @candidates[@candidate_index].conjugation
+    end
+  end
   def get_score ()
     if @candidate_index == -1 then
       ## FIXME: Remove the music number.
@@ -660,6 +844,16 @@
     end
   end
 
+  def to_text_candidates ()
+    if @candidate_index == -1 then
+      return "-1"
+    end
+
+    text = @candidate_index.to_s() + "\n"
+    text +=****@candi***** { | candidate | candidate.to_text2() }.join("\n")
+    return text
+  end
+
   def to_text_data ()
     if @candidate_index == -1 then
       text = @reading
@@ -673,10 +867,3 @@
     return get_literal()
   end
 end
-
-class PrimeConvertMultiSegments
-  ## This returns a PrimeConversionList
-  def PrimeConvertMultiSegments::convert (composer)
-    string = composer.edit_get_surface_string()
-  end
-end


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