[Groonga-commit] groonga/groonga at 0cf0009 [master] example: use Ruby 2.0 or later API

Back to archive index

Kouhei Sutou null+****@clear*****
Thu May 14 12:22:25 JST 2015


Kouhei Sutou	2015-05-14 12:22:25 +0900 (Thu, 14 May 2015)

  New Revision: 0cf0009033a659eeb2006c18784cc79112f653e4
  https://github.com/groonga/groonga/commit/0cf0009033a659eeb2006c18784cc79112f653e4

  Message:
    example: use Ruby 2.0 or later API

  Modified files:
    examples/dictionary/edict/edict2grn.rb

  Modified: examples/dictionary/edict/edict2grn.rb (+17 -39)
===================================================================
--- examples/dictionary/edict/edict2grn.rb    2015-05-14 12:19:20 +0900 (664b12c)
+++ examples/dictionary/edict/edict2grn.rb    2015-05-14 12:22:25 +0900 (b795e25)
@@ -1,47 +1,23 @@
 #!/usr/bin/env ruby
-# -*- coding: utf-8 -*-
 
-$KCODE = 'u'
+require "English"
+require "nkf"
+require "json"
 
-require 'English'
-require 'kconv'
-
-class String
-  def to_json
-    a = split(//).map {|char|
-      case char
-      when '"' then '\\"'
-      when '\\' then '\\\\'
-      when "\b" then '\b'
-      when "\f" then '\f'
-      when "\n" then '\n'
-      when "\r" then ''
-      when "\t" then '\t'
-      else char
-      end
-    }
-    "\"#{a.join('')}\""
-  end
-end
-
-class Array
-  def to_json
-    '[' + map {|element|
-      element.to_json
-    }.join(',') + ']'
-  end
-end
-
-puts <<END
+print(<<HEADER.chomp)
 column_create item_dictionary edict_desc COLUMN_SCALAR ShortText
 column_create bigram item_dictionary_edict_desc COLUMN_INDEX|WITH_POSITION item_dictionary edict_desc
 load --table item_dictionary
-[["_key","edict_desc","kana"],
-END
+[
+["_key","edict_desc","kana"]
+HEADER
+
+loop do
+  raw_line = gets
+  break if raw_line.nil?
 
-while !STDIN.eof?
-  line = Kconv.toutf8(gets)
-  key, body = line.strip.split('/', 2)
+  line = raw_line.encode("UTF-8", "EUC-JP")
+  key, body = line.strip.split("/", 2)
   key = key.strip
   if /\s*\[(.+)\]\z/ =~ key
     key = $PREMATCH
@@ -51,6 +27,8 @@ while !STDIN.eof?
   else
     kana = NKF.nkf("-Ww --katakana", key)
   end
-  puts [key, body, kana].to_json
+  puts(",")
+  puts([key, body, kana].to_json)
 end
-puts ']'
+puts
+puts("]")
-------------- next part --------------
HTML����������������������������...
다운로드 



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