[logaling-commit] logaling/logaling-command [improve-lookup] collect duplicated process to method

Back to archive index

null+****@clear***** null+****@clear*****
Wed Feb 22 19:06:02 JST 2012


SUZUKI Miho	2012-02-22 19:06:02 +0900 (Wed, 22 Feb 2012)

  New Revision: 94f083a12468b2f8ca12a4e4dd3f395068a6720a

  Log:
    collect duplicated process to method

  Modified files:
    lib/logaling/glossary_db.rb

  Modified: lib/logaling/glossary_db.rb (+33 -58)
===================================================================
--- lib/logaling/glossary_db.rb    2012-02-22 17:54:29 +0900 (bca8857)
+++ lib/logaling/glossary_db.rb    2012-02-22 19:06:02 +0900 (1b31eff)
@@ -108,26 +108,14 @@ module Logaling
                  :html_escape => true,
                  :normalize => true}
       snippet = records_selected.expression.snippet(["<snippet>", "</snippet>"], options)
-
-      snipped_source_term = []
-      records.map do |record|
-        term = record.key
-        snipped_text = snippet.execute(term.source_term).join
-        {:glossary_name => term.glossary.key,
-         :source_language => term.source_language,
-         :target_language => term.target_language,
-         :source_term => term.source_term,
-         :snipped_source_term => struct_snipped_text(snipped_text),
-         :target_term => term.target_term,
-         :snipped_target_term => [],
-         :note => term.note || ''}
-      end
+      struct_result(records, snippet)
     ensure
       snippet.close if snippet
       records_selected.expression.close if records_selected
       specified_glossary.expression.close if specified_glossary
     end
 
+
     def lookup_dictionary(search_word)
       records_selected_source = Groonga["translations"].select do |record|
         target = record.match_target do |match_record|
@@ -157,20 +145,7 @@ module Logaling
                  :normalize => true}
       snippet = records_selected.expression.snippet(["<snippet>", "</snippet>"], options)
 
-      snipped_source_term = []
-      records.map do |record|
-        term = record.key
-        snipped_source_term = snippet.execute(term.source_term).join
-        snipped_target_term = snippet.execute(term.target_term).join
-        {:glossary_name => term.glossary.key,
-         :source_language => term.source_language,
-         :target_language => term.target_language,
-         :source_term => term.source_term,
-         :snipped_source_term => struct_snipped_text(snipped_source_term),
-         :target_term => term.target_term,
-         :snipped_target_term => struct_snipped_text(snipped_target_term),
-         :note => term.note || ''}
-      end
+      struct_result(records, snippet)
     ensure
       snippet.close if snippet
       records_selected.expression.close if records_selected
@@ -189,16 +164,7 @@ module Logaling
         {:key=>"source_term", :order=>'ascending'},
         {:key=>"target_term", :order=>'ascending'}])
 
-      records.map do |record|
-        term = record.key
-
-        {:glossary_name => term.glossary.key,
-         :source_language => term.source_language,
-         :target_language => term.target_language,
-         :source_term => term.source_term,
-         :target_term => term.target_term,
-         :note => term.note || ''}
-      end
+      struct_result(records)
     ensure
       records_raw.expression.close
     end
@@ -212,16 +178,7 @@ module Logaling
         ]
       end
 
-      records.map do |record|
-        term = record.key
-
-        {:glossary_name => term.glossary,
-         :source_language => term.source_language,
-         :target_language => term.target_language,
-         :source_term => term.source_term,
-         :target_term => term.target_term,
-         :note => term.note || ''}
-      end
+      struct_result(records)
     ensure
       records.expression.close
     end
@@ -236,16 +193,7 @@ module Logaling
         ]
       end
 
-      records.map do |record|
-        term = record.key
-
-        {:glossary_name => term.glossary,
-         :source_language => term.source_language,
-         :target_language => term.target_language,
-         :source_term => term.source_term,
-         :target_term => term.target_term,
-         :note => term.note || ''}
-      end
+      struct_result(records)
     ensure
       records.expression.close
     end
@@ -392,7 +340,24 @@ module Logaling
       @database.nil? or****@datab*****?
     end
 
+    def struct_result(records, snippet=nil)
+      records.map do |record|
+        term = record.key
+        snipped_source_term = snippet ? snip_source_term(term, snippet) : []
+        snipped_target_term = snippet ? snip_target_term(term, snippet) : []
+        {:glossary_name => term.glossary.key,
+         :source_language => term.source_language,
+         :target_language => term.target_language,
+         :source_term => term.source_term,
+         :snipped_source_term => snipped_source_term,
+         :target_term => term.target_term,
+         :snipped_target_term => snipped_target_term,
+         :note => term.note || ''}
+      end
+    end
+
     def struct_snipped_text(snipped_text)
+      return [] if snipped_text.empty?
       word_list = snipped_text.split(/(<snippet>[^<]*<\/snippet>)/)
       structed_source_term = word_list.map{|word|
         replaced_word = word.sub(/<snippet>([^<]*)<\/snippet>/){|match| $1}
@@ -405,6 +370,16 @@ module Logaling
       structed_source_term
     end
 
+    def snip_source_term(term, snippet)
+        snipped_text = snippet.execute(term.source_term).join
+        struct_snipped_text(snipped_text)
+    end
+
+    def snip_target_term(term, snippet)
+        snipped_text = snippet.execute(term.target_term).join
+        struct_snipped_text(snipped_text)
+    end
+
     def get_config(conf_key)
       records = Groonga["configurations"].select do |record|
         record.conf_key == conf_key




More information about the logaling-commit mailing list
Back to archive index