[logaling-commit] logaling/logaling-command [master] improve import respond to tmx

Back to archive index

null+****@clear***** null+****@clear*****
Tue Apr 24 16:47:57 JST 2012


SUZUKI Miho	2012-04-24 16:47:57 +0900 (Tue, 24 Apr 2012)

  New Revision: 3205402f6292c1f3d6778fe4821b7678a9c91813

  Merged dc203a9: Merge pull request #58 from logaling/import-tmx

  Log:
    improve import respond to tmx

  Added files:
    lib/logaling/external_glossaries/tmx.rb
  Modified files:
    lib/logaling/command/application.rb
    lib/logaling/external_glossary.rb
    lib/logaling/repository.rb

  Modified: lib/logaling/command/application.rb (+21 -1)
===================================================================
--- lib/logaling/command/application.rb    2012-03-29 12:47:08 +0900 (d2b4557)
+++ lib/logaling/command/application.rb    2012-04-24 16:47:57 +0900 (7adfd60)
@@ -84,11 +84,16 @@ module Logaling::Command
 
     desc 'import', 'Import external glossary'
     method_option "list", type: :boolean, default: false
-    def import(external_glossary=nil)
+    def import(external_glossary=nil, *args)
       require "logaling/external_glossary"
       Logaling::ExternalGlossary.load
       if options["list"]
         Logaling::ExternalGlossary.list.each {|glossary| say "#{glossary.name.bright} : #{glossary.description}" }
+      elsif args.size > 0
+        glossary_info = initialize_import_parameter(args)
+        check_import_parameter(glossary_info)
+        @repository.import_tmx(Logaling::ExternalGlossary.get(external_glossary), glossary_info)
+        @repository.index
       else
         @repository.import(Logaling::ExternalGlossary.get(external_glossary))
         @repository.index
@@ -408,5 +413,20 @@ module Logaling::Command
         puts("\n]") if i == last-1
       end
     end
+
+    def check_import_parameter(glossary_info)
+      unless glossary_info[:name] && glossary_info[:url]
+        raise Logaling::CommandFailed, "Do loga import tmx <glossary name> <url or path>"
+      end
+    end
+
+    def initialize_import_parameter(arr)
+      glossary_info = {}
+      glossary_info[:name] = arr[0]
+      glossary_info[:url] = arr[1]
+      glossary_info[:source_language] = arr[2]
+      glossary_info[:target_language] = arr[3]
+      glossary_info
+    end
   end
 end

  Added: lib/logaling/external_glossaries/tmx.rb (+46 -0) 100644
===================================================================
--- /dev/null
+++ lib/logaling/external_glossaries/tmx.rb    2012-04-24 16:47:57 +0900 (0b6e3ed)
@@ -0,0 +1,46 @@
+# Copyright (C) 2012  Miho SUZUKI
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require 'open-uri'
+require 'nokogiri'
+module Logaling
+  class Tmx < ExternalGlossary
+    description     'TMX 1.4b formated glossary (http://www.gala-global.org/oscarStandards/tmx/)'
+    source_language 'en'
+    target_language 'ja'
+    output_format   'csv'
+
+    private
+    def convert_to_csv(csv, url)
+      doc = ::Nokogiri::XML(open(url, "r"))
+      tu_nodes = doc.xpath('//tu')
+      tu_nodes.each do |tu|
+        original = ""
+        translation = ""
+        tu.children.each do |child|
+          if child.name == "tuv"
+            lang = child["lang"].downcase.slice(0, 2)
+            if lang == self.class.source_language
+              original = child.text.strip
+            elsif lang == self.class.target_language
+              translation = child.text.strip
+            end
+          end
+        end
+        csv << [original, translation]
+      end
+    end
+  end
+end

  Modified: lib/logaling/external_glossary.rb (+19 -6)
===================================================================
--- lib/logaling/external_glossary.rb    2012-03-29 12:47:08 +0900 (0d9a0a7)
+++ lib/logaling/external_glossary.rb    2012-04-24 16:47:57 +0900 (a2d8556)
@@ -66,13 +66,17 @@ class Logaling::ExternalGlossary
     end
   end
 
-  def import
-    File.open(import_file_name, "w") do |output|
+  def import(glossary_info=nil)
+    File.open(import_file_name(glossary_info), "w") do |output|
       output_format = self.class.output_format
       output_format = output_format.to_s if output_format.is_a?(Symbol)
       case output_format
       when "csv"
-        convert_to_csv(CSV.new(output))
+        if glossary_info
+          convert_to_csv(CSV.new(output), glossary_info[:url])
+        else
+          convert_to_csv(CSV.new(output))
+        end
       else
         raise UnsupportedFormat, "unsupported format: <#{output_format}>"
       end
@@ -80,8 +84,17 @@ class Logaling::ExternalGlossary
   end
 
   private
-  def import_file_name
-    [self.class.name, self.class.source_language,
-     self.class.target_language, self.class.output_format].join('.')
+  def import_file_name(glossary_info=nil)
+    if glossary_info
+      glossary_info[:name] ||= self.class.name
+      glossary_info[:source_language] ||= self.class.source_language
+      glossary_info[:target_language] ||= self.class.target_language
+
+      [glossary_info[:name], glossary_info[:source_language],
+       glossary_info[:target_language], self.class.output_format].join('.')
+    else
+      [self.class.name, self.class.source_language,
+       self.class.target_language, self.class.output_format].join('.')
+    end
   end
 end

  Modified: lib/logaling/repository.rb (+9 -0)
===================================================================
--- lib/logaling/repository.rb    2012-03-29 12:47:08 +0900 (69c223f)
+++ lib/logaling/repository.rb    2012-04-24 16:47:57 +0900 (9ab399a)
@@ -54,6 +54,15 @@ module Logaling
       raise Logaling::CommandFailed, "Failed import #{glossary.class.name} to #{cache_path}."
     end
 
+    def import_tmx(glossary, glossary_info)
+      FileUtils.mkdir_p(cache_path) unless File.exist?(cache_path)
+      Dir.chdir(cache_path) do
+        glossary.import(glossary_info)
+      end
+    rescue
+      raise Logaling::CommandFailed, "Failed import_tmx #{glossary.class.name} to #{cache_path}."
+    end
+
     def lookup(source_term, glossary_source, dictionary=false)
       raise GlossaryDBNotFound unless File.exist?(logaling_db_home)
 




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