From: eto Date: Thu, 10 Jun 2004 01:44:23 +0000 (+0000) Subject: i X-Git-Url: http://git.chise.org/gitweb/?a=commitdiff_plain;h=3bb61fc3d9acdb9565d6a8e2f1b23bfa2342490d;p=chise%2Fruby.git i --- diff --git a/chise/char.rb b/chise/char.rb index 473ba0d..f8687e6 100755 --- a/chise/char.rb +++ b/chise/char.rb @@ -4,7 +4,6 @@ require "chise/db" require "chise/config" require "chise/iconv" require "chise/uconv" -require "chise/util" require "chise/string" require "chise/character" diff --git a/chise/config.rb b/chise/config.rb index 1494d39..52663ee 100755 --- a/chise/config.rb +++ b/chise/config.rb @@ -5,16 +5,16 @@ require "singleton" module CHISE def windows?() rp = RUBY_PLATFORM - rp =~ /cygwin/ || rp =~ /mswin32/ || rp =~ /mingw32/ + rp.index("cygwin") || rp.index("mswin32") || rp.index("mingw32") end module_function :windows? class Config include Singleton + def initialize @base_dir = File.expand_path(File.dirname(__FILE__)+"/../..") -# @base_dir = "c:/chise" -# qp @base_dir + @csf_dir = @base_dir+"/csf" if CHISE.windows?() @db_dir = @base_dir+"/char-db" # /sysmtem-char-id/ucs @@ -24,7 +24,6 @@ module CHISE @ids_dir = "/home/eto/chise/ids" end - @csf_dir = @base_dir+"/csf" instance_variables.each {|name| self.class.class_eval { attr_reader name.delete("@") } } diff --git a/chise/csf.rb b/chise/csf.rb index e29c219..fd8bf1f 100755 --- a/chise/csf.rb +++ b/chise/csf.rb @@ -2,7 +2,8 @@ require "sgl" #require "kconv" -require "uconv" +#require "uconv" +require "chise/iconv" require "chise/config" module StrokeFont @@ -64,10 +65,12 @@ module StrokeFont @strokes = nil end attr_reader :strokes + def parse() return if @strokes @strokes = CSFParser.parse(@stroke_str) end + def init parse if @strokes.nil? end @@ -82,6 +85,7 @@ module StrokeFont @rend = StrokesRenderer.new @rend.hsv = [50, 100, 100] end + def read_file() open(@file) {|f| while(line = f.gets) @@ -92,6 +96,7 @@ module StrokeFont end } end + def init(code) glyph = @glyphs[code] return if glyph == nil @@ -99,21 +104,24 @@ module StrokeFont glyph.parse @rend.set_strokes(glyph.strokes) end + def draw(code) #ˆø”‚É‚ÍJIS‚𐔒l‰»‚µ‚½‚à‚Ì‚ª‚Í‚¢‚é glyph = @glyphs[code] return if glyph == nil @rend.draw end + def print(code) jis = JISX0208.new char = jis.get_char(code) printf("[%s][%04x]\n", char.nil? ? "nil" : char.map_sjis, code) end + def ucs_to_jis(ucs) char = Character.get(ucs) j = char.japanese_jisx0208 return j end - end + end end diff --git a/chise/db.rb b/chise/db.rb index 2d82242..e408969 100755 --- a/chise/db.rb +++ b/chise/db.rb @@ -3,7 +3,6 @@ require "singleton" require "bdb" require "chise/config" -require "chise/util" require "chise/rbchise" module CHISE diff --git a/chise/defkanji.rb b/chise/defkanji.rb index e2ea84c..e0c7379 100755 --- a/chise/defkanji.rb +++ b/chise/defkanji.rb @@ -98,6 +98,7 @@ module CHISE #p "休".decompose.to_er eval_print_loop end + def show_ids(str) str.de_er.to_a.sort.uniq.each {|ch| char = ch.char @@ -105,6 +106,7 @@ module CHISE print "#{char} #{ids.map_ucs}\n" #できるだけucsに正規化するべし。 } end + def eval_print_loop while true line = STDIN.gets @@ -121,12 +123,14 @@ module CHISE define_ids_represent define_ids_link end + def define_ids_element IDS_ELEMENT.each_char {|ch| char = ch.char char.ids_element = ch } end + def define_ids_meaning IDS_MEANING.each {|line| next if line.nil? || line == "" || line =~ /^#/ @@ -136,6 +140,7 @@ module CHISE print "#{ch} #{ids}\n" } end + def define_ids_represent IDS_REPRESENT.each {|line| next if line.nil? || line == "" || line =~ /^#/ @@ -145,6 +150,7 @@ module CHISE print "#{char.map_ucs} #{rep}\n" } end + def define_ids_link IDS_LINK.each {|line| next if line.nil? || line == "" || line =~ /^#/ diff --git a/chise/graphviz.rb b/chise/graphviz.rb index 415c1c9..456ac66 100755 --- a/chise/graphviz.rb +++ b/chise/graphviz.rb @@ -4,11 +4,13 @@ class DotElement def initialize() @attr = Hash.new end + def method_missing(mid, *args) #ŽQl:ostruct.rb mname = mid.id2name return @attr[mname] if args.length == 0 @attr[mname.chop] = args[0] if mname =~ /=$/ #‘ã“ü end + def to_s str = " #{mainstr()} " str << "[" + @attr.map {|k, v| %Q|#{k}="#{v}"| }.join(" ") + "]" if 0 < @attr.length @@ -23,15 +25,19 @@ class DotPage < DotElement @nodes = [] @edges = [] end + def mainstr() @name.to_s; end + def add_node(node) return unless node.is_a? DotNode @nodes << node end + def add_edge(edge) return unless edge.is_a? DotEdge @edges << edge end + def to_s() str = "digraph G {\n" # str << %Q| size="6, 6"\n| @@ -52,6 +58,7 @@ class DotNode < DotElement @name = name super() end + def mainstr() @name.to_s; end end @@ -60,6 +67,7 @@ class DotEdge < DotElement @from, @to = from, to super() end + def mainstr() "#{@from.to_s} -> #{@to.to_s}"; end end @@ -90,7 +98,9 @@ end class GraphvizCLI < Graphviz # COMMAND_DIR = "c:\Program Files\ATT\Graphviz\bin" COMMAND_DIR = "" #PATH‚ª‚Æ‚¨‚Á‚Ä‚È‚¢‚Æ‚¾‚ß‚Ý‚½‚¢B - NAMES = "dot twopi neato".split +# NAMES = "dot twopi neato".split + NAMES = %w(dot twopi neato) + def generate(debug=false) ar = [] #ar << COMMAND_DIR + NAMES[@type] + ".exe" @@ -111,12 +121,14 @@ end class GraphvizOLE < Graphviz #NAMES = "DOT TWOPI NEATO".split + def generate(debug=false) require "win32ole" names = "DOT TWOPI NEATO".split @ole = WIN32OLE.new("Wingraphviz." + names[@type]) @ole.codepage = cp if @codepage #neato.codepage = 65001 #codepage: 65001 Unicode UTF-8 @instr = open(@in).read + if @target =~ /svg/i result = @ole.toSVG(@instr) open(@out, "w"){|out| out.print result} @@ -126,12 +138,14 @@ class GraphvizOLE < Graphviz result.save(@out) end end + def nu_to_png(filename=nil) return "" unless @dot.is_a?(String) png = @ole.toPNG(@dot) png.save(filename) unless filename.nil? return png end + #p neato.validate(str) #ps = neato.toPS(str) #open("test.ps", "w"){|out| out.print ps } diff --git a/chise/iconv.rb b/chise/iconv.rb index e6f97cb..5ef044b 100755 --- a/chise/iconv.rb +++ b/chise/iconv.rb @@ -80,7 +80,7 @@ class String def u32to_i return 0 if length == 0 s = self -# return (s[3] << 24 | s[2] << 16 | s[1] << 8 | s[0]) +# return (s[3] << 24 | s[2] << 16 | s[1] << 8 | s[0]) return (s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]) end end diff --git a/chise/ids.rb b/chise/ids.rb index 46e039c..d1c62c0 100755 --- a/chise/ids.rb +++ b/chise/ids.rb @@ -50,8 +50,9 @@ module CHISE IDC_FLL = IDC_LL IDC_O = IDC_OV - class IDS_TEXT_DB < DB #====================================================================== + class IDS_TEXT_DB < DB include Singleton + IDS_LIST = " IDS-UCS-Basic.txt #IDS-UCS-Compat-Supplement.txt @@ -80,6 +81,7 @@ IDS-Daikanwa-dx.txt IDS-Daikanwa-ho.txt IDS-CBETA.txt ".split + def initialize() super @ids_list = IDS_LIST @@ -111,6 +113,7 @@ IDS-CBETA.txt end } end + def dump_text_all each_file {|file| dir = File.dirname(file) + "/../ids-new/" @@ -127,6 +130,7 @@ IDS-CBETA.txt } } end + def make_ids_error each_file {|file| dir = File.dirname(file) + "/../ids-error" @@ -148,9 +152,11 @@ IDS-CBETA.txt class IDS_DB < DB # BDB化したIDS DBを扱う include Singleton + def initialize @dbs = CharDB.instance end + def make_ids_db db = IDS_TEXT_DB.instance db.each_file {|file| @@ -211,6 +217,7 @@ IDS-CBETA.txt @dbs.dump_db("ids-error") #テキスト化する @dbs.dump_db("ids") #テキスト化する end + def make_ids_reverse h = Hash.new @dbs.each("ids") {|k, v| @@ -231,6 +238,7 @@ IDS-CBETA.txt cdb.open_db("ids") #これが無いと、dump_dbされません。 cdb.dump_db("ids") end + def char_sort(composed) return composed if composed.char_length == 1 ar = composed.to_a @@ -268,6 +276,7 @@ IDS-CBETA.txt end return ar2.join("") end + def dump_ids_duplicated open("ids-duplicated.txt", "w"){|out| #out.binmode @@ -287,6 +296,7 @@ IDS-CBETA.txt } } end + def make_ids_aggregated @dbs.each("ids") {|k, v| char = k.char @@ -296,6 +306,7 @@ IDS-CBETA.txt } @dbs.dump_db("ids-aggregated") end + def dump_ids_aggregated open("ids-aggregated.txt", "w"){|out| #out.binmode @@ -307,6 +318,7 @@ IDS-CBETA.txt } } end + def make_ids_parts @dbs.each("ids") {|k, v| char = k.char @@ -332,6 +344,7 @@ IDS-CBETA.txt } @dbs.dump_db("ids-parts") end + def make_ids_contained h = Hash.new @dbs.each("ids-parts") {|k, v| @@ -354,6 +367,7 @@ IDS-CBETA.txt } @dbs.dump_db("ids-contained") end + def make_ids_decomposed @dbs.each("ids") {|k, v| char = k.char @@ -362,9 +376,10 @@ IDS-CBETA.txt } @dbs.dump_db("ids-decomposed") end + end - class Node < Array #==================================木構造の中の一つの枝 + class Node < Array # 木構造の中の一つの枝 def initialize(nodeleaf=nil, nodenum=nil) super() @nodeleaf = nodeleaf @@ -374,12 +389,15 @@ IDS-CBETA.txt end end attr_reader :nodenum + alias original_add << private :original_add + def <<(obj) original_add(obj) @nodenum -= 1 if @nodenum end + def nodes ar = [] ar << self.to_s @@ -388,16 +406,19 @@ IDS-CBETA.txt } return ar end + end - class Tree #==================================================木構造を扱う + class Tree # 木構造を扱う def initialize() @root = Node.new() @stack = [@root] @leafnum = 0 @depth = 1 #stackの深さが最大になったところの値、木構造が無いときは1となる end + def depth() @depth - 1 end + def add_node(nodeleaf=nil, nodenum=nil) #枝を追加 new_node = Node.new(nodeleaf, nodenum) @stack.last << new_node @@ -407,15 +428,18 @@ IDS-CBETA.txt end self end + def end_node() #この枝は終り @stack.pop self end + def add_leaf(a) #葉を追加 @stack.last << a end_check() self end + def end_check() n = @stack.last.nodenum if n && n == 0 @@ -423,6 +447,7 @@ IDS-CBETA.txt end_check() #再帰 end end + def check_integrity n = @stack.last.nodenum return nil if @root.length == 0 #no tree is good tree @@ -431,17 +456,21 @@ IDS-CBETA.txt return "extra leaves" if @root.length != 1 return nil end + def nodes r = @root.nodes r.shift r end + def sub_nodes r = nodes r.shift r end + def to_s() @root.to_s end + def inspect() @root.inspect end end @@ -451,6 +480,7 @@ IDS-CBETA.txt super() parse() end + def parse() @str.each_char {|ch| char = Character.new(ch) @@ -461,15 +491,18 @@ IDS-CBETA.txt end } end + def is_ids?(obj) return true if "+*".include?(obj.to_s) #テスト用ですかね return true if obj.is_ids? return false end + def ids_operator_argc(obj) return obj.ids_operator_argc if 0 < obj.ids_operator_argc return 2 #テスト用ってことで end + def check_integrity r = super return r if r #不完全がすでにわかっているならreturn @@ -478,12 +511,14 @@ IDS-CBETA.txt end end - class IDS #=========================================IDSそのものを扱うclass + class IDS # IDSそのものを扱うclass def initialize(str) #IDS文字列をうけとる。 @str = str end + def parse end + def parse_x #柔軟型のParse. IDSキャラクターが前にきてなくてもよい。などなど。 end end @@ -497,12 +532,13 @@ IDS-CBETA.txt @count = 0 @proc = proc end + def count @count += 1 if @max <= @count @proc.call end end - end + end end diff --git a/chise/kage.rb b/chise/kage.rb index 46c0cbc..9b60ddb 100755 --- a/chise/kage.rb +++ b/chise/kage.rb @@ -2,7 +2,6 @@ require "sgl" require "singleton" -$LOAD_PATH << "../../lib" if $0 == __FILE__ require "chise/kageserver" #こんな感じのフォーマットになっている。 @@ -14,17 +13,21 @@ require "chise/kageserver" # module StrokeFont - class QuadraticPath #========================動的な分割に対応できるようにする + class QuadraticPath # 動的な分割に対応できるようにする DEFAULT_DIVIDE = 4 + def initialize(p1, p2, p3) @p1, @p2, @p3 = p1, p2, p3 @num = DEFAULT_DIVIDE end + def divide_adaptic #適応的分割数をする。 end + def divide() divide_num(@num) end + def divide_num(num) #分割数を指定できる #p [num] x1, y1 = @p1 @@ -39,20 +42,24 @@ module StrokeFont curve << [x,y] } #p curve - return curve + curve end + end class PathResolver #M 50,540 950,255 #M 330,50 330,900 M 330,900 Q 330,950 380,950 M 380,950 840,950 M 840,950 Q 890,950 915,850 + def initialize reset end + def reset @lines = [] @px, @py = -1, -1 end + def parse(str) reset cmd = [] @@ -69,6 +76,7 @@ module StrokeFont exec_cmd(cmd) if 0 < cmd.length #前のコマンドが残っていたら実行 @lines end + def exec_cmd(cmd) c = cmd.shift #先頭をとる case c @@ -78,10 +86,12 @@ module StrokeFont quadratic([@px, @py], cmd.shift, cmd.shift) end end + def moveto(x, y) @lines << [@px, @py, x, y] if @px != -1 @px, @py = x, y end + def quadratic(p1, p2, p3) #p ["quadratic", p1, p2, p3] qp = QuadraticPath.new(p1, p2, p3) @@ -90,6 +100,7 @@ module StrokeFont moveto(x, y) } end + end class KageParser @@ -111,17 +122,19 @@ module StrokeFont end end - class KageGlyph #================================================== てなかんじ + class KageGlyph # てなかんじ def initialize(code, svg) @code = code @svg = svg @strokes = nil end attr_reader :strokes + def parse return if @strokes @strokes = KageParser.parse(@svg) end + def init parse if @strokes.nil? end @@ -140,12 +153,14 @@ module StrokeFont @rend.hsv = [6, 100, 100] #オレンジ end attr_reader :cache_list, :server + def get(code) return @glyphs[code] if @glyphs[code] svg = @server.get(code) return nil if svg.nil? @glyphs[code] = KageGlyph.new(code, svg) end + def init(code) glyph = get(code) return if glyph.nil? @@ -153,15 +168,17 @@ module StrokeFont glyph.parse @rend.set_strokes(glyph.strokes) end + def draw(code) glyph = get(code) return if glyph.nil? @rend.draw end + def print(code) char = Character.new(@code) printf("[%s][%04x]\n", char.nil? ? "nil" : char.map_sjis, code) end - end + end end diff --git a/chise/kageserver.rb b/chise/kageserver.rb index 26a956d..e01f530 100755 --- a/chise/kageserver.rb +++ b/chise/kageserver.rb @@ -3,19 +3,20 @@ require "singleton" require "net/http" require "uri" -$LOAD_PATH << "../../lib" require "chise" require "chise/network" #漢字間接続のネットワークを計算する #require "chise/kanjilist" -include CHISE +#include CHISE class KageServer include Singleton - TYPES = "skeleton mincho gothic".split + + TYPES = %w(skeleton mincho gothic) SKELETON = 0 MINCHO = 1 GOTHIC = 2 URL = "http://192.168.2.60:5100/" + def initialize(url=URL) @url = url @glyphs = [] @@ -27,8 +28,14 @@ class KageServer Dir.mkdir(@cache_dir) unless FileTest.directory?(@cache_dir) end attr_accessor :url, :use_cache, :offline - def filename(num, type=SKELETON) sprintf("u%04x.%s", num, TYPES[type]) end - def cache_file(num, type=SKELETON) @cache_dir+"/"+filename(num, type)+".svg" end + + def filename(num, type=SKELETON) + sprintf("u%04x.%s", num, TYPES[type]) + end + + def cache_file(num, type=SKELETON) + @cache_dir+"/"+filename(num, type)+".svg" + end def list_cache ar = [] @@ -43,15 +50,19 @@ class KageServer end def get(num, type=SKELETON) - return open(cache_file(num, type)).read if FileTest.exist?(cache_file(num, type)) + if FileTest.exist?(cache_file(num, type)) + return open(cache_file(num, type)) {|f| f.read } + end svg = get_http(num, type) - return svg if svg - return nil + svg end + def get_http(num, type=SKELETON) return nil if @offline + uri = URI.parse(URL + filename(num, type)) p ["uri", uri.to_s] + Net::HTTP.version_1_1 # declear to use 1.1 features. Net::HTTP.start( uri.host, uri.port ) {|http| response, body = http.get("/"+filename(num, type)+".svg") @@ -65,17 +76,20 @@ class KageServer end end } - return nil + nil end + def store_cache(num, type, svg) #p ["store", num] open(cache_file(num, type), "w") {|f| f.print svg } end + def error?(svg) (svg =~ //) end + def read_list h = {} open("kage-list.txt"){|f| @@ -91,6 +105,7 @@ class KageServer } return h end + def get_all #error_h = read_list STDOUT.binmode @@ -118,6 +133,7 @@ class KageServer } get_ar(ar) end + def get_ar(ar) ar.each {|num| #intの数列 char = Character.get(num) @@ -133,6 +149,7 @@ class KageServer } } end + def test_kanji char = "&CDP-8BA5;".de_er #p char.inspect_all diff --git a/chise/makegraph.rb b/chise/makegraph.rb index b289295..ef0a895 100755 --- a/chise/makegraph.rb +++ b/chise/makegraph.rb @@ -2,11 +2,10 @@ $LOAD_PATH << "../../lib" if $0 == __FILE__ require "chise" -include CHISE require "chise/network" module CHISE - class MakeGraph #==================================実務的な処理を引き受ける。 + class MakeGraph # 実務的な処理を引き受ける。 #GRAPHVIZ_CMD = 0 # CLI GRAPHVIZ_CMD = 1 # OLE GRAPHVIZ_TYPE = Graphviz::TWOPI diff --git a/chise/network.rb b/chise/network.rb index 18bb9a7..ed0d679 100755 --- a/chise/network.rb +++ b/chise/network.rb @@ -1,25 +1,25 @@ # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved. -$LOAD_PATH << "../../lib" if $0 == __FILE__ -require "chise" -include CHISE +require "chise/char" require "chise/kanjilist" require "chise/defkanji" require "chise/graphviz" module CHISE - class KanjiNetwork #=========================漢字間のネットワークを計算する。 + class KanjiNetwork # 漢字間のネットワークを計算する def initialize() reset() @formatter = GraphvizFormatter.new(self) # @formatter = XSpringiesFormatter.new(self) end attr_reader :h, :list + def reset @h = Hash.new @done = Hash.new @list = [] end + def make_network(list) #@h, @listに結果を入れていく。 list.each_char {|ch| make_network_one(ch) @@ -72,13 +72,15 @@ module CHISE def out(filename) open(filename, "w"){|out| out.print to_s } end + def to_s() @formatter.to_s; end end - class GraphvizFormatter #======================================== Graphviz関係 + class GraphvizFormatter # Graphviz関係 def initialize(network) @network = network end + def to_s() #Graphvizのフォーマット、dotフォーマットに変換する。 page = DotPage.new page.size = "5.5, 5.5" @@ -118,7 +120,7 @@ module CHISE end end - class XSpringiesFormatter #================================================= + class XSpringiesFormatter def initialize(network) @network = network end diff --git a/chise/parser.rb b/chise/parser.rb index 6402fca..232b554 100755 --- a/chise/parser.rb +++ b/chise/parser.rb @@ -2,8 +2,8 @@ module CHISE class CharacterParser - def parse(c) # parse a value and return a number - return 0 if c.nil? # what? should raise exception? + def parse(c) # parse a value and return a number (MCS) + raise "c is nil" if c.nil? if c.kind_of?(Numeric) c = 0x80000000 + c if c < 0 # negative value diff --git a/test/common.rb b/test/common.rb index faa4295..d5bdc43 100755 --- a/test/common.rb +++ b/test/common.rb @@ -6,7 +6,6 @@ $LOAD_PATH.unshift("..") require "test/unit" require "chise/char" require "chise/qp" -#include CHISE class String alias su sjistou8 diff --git a/test/test-db.rb b/test/test-db.rb index 4c64dc4..c94a2d6 100755 --- a/test/test-db.rb +++ b/test/test-db.rb @@ -5,20 +5,17 @@ require "common" class TestBDB < Test::Unit::TestCase -#class TestBDB def test_bdb @config = CHISE::Config.instance dir = @config.db_dir assert_match(%r|/chise/char-db\Z|, dir) file = dir+"/=jis-x0208/system-char-id" -# db = BDB::Hash.open(file, nil, BDB::CREATE | BDB::EXCL) db = BDB::Hash.open(file, nil, 0) -# qp db end def test_db -# @cdb = CHISE::CharDB.instance +# @cdb = CHISE::CharDB.instance # @sdb = CHISE::CodesysDB.instance end @@ -27,52 +24,9 @@ class TestBDB < Test::Unit::TestCase end end -#class TC_DB < Test::Unit::TestCase -class TestDataBase - def setup - @cdb = CHISE::CharDB.instance - @sdb = CHISE::CodesysDB.instance - end - - def test_db - assert_equal("()+!", CHISE::DB.unix_to_win("<>*?")) - assert_equal("<>*?", CHISE::DB.win_to_unix("()+!")) - end - - def test_each_db(db) - assert_instance_of(Array, db.keys) - end - - def test_make_db(db) - h = {"a" => 1, "b" => 2, "c" => 3} - db.remove_db("test-db") #‚Ü‚¸Å‰‚ɏÁ‚µ‚Ä‚¨‚­ - assert_not_nil(db.make_db("test-db", h)) - assert_not_nil(db.open_db("test-db")) - assert_equal(1, db.get("test-db", "a")) - assert_equal(2, db.get("test-db", "b")) - assert_equal(3, db.get("test-db", "c")) - db.remove_db("test-db") #ÅŒã‚É‚Ü‚½Á‚µ‚Ä‚¨‚­ - end - - def test_dbs - test_each_db(@cdb) - test_each_db(@sdb) -# test_make_db(@cdb) -# test_make_db(@sdb) - end - - def test_db_put - char = "Žš".char - char.put_char_attribute("test-attribute", "test") - assert_equal("test", char.test_attribute) - end - -end - class TestCodesys < Test::Unit::TestCase -#class TestCodesys - def setup -# @db = CHISE::CodesysDB.instance + def nusetup + @db = CHISE::CodesysDB.instance end def test_dummy @@ -134,5 +88,4 @@ class TestCodesys < Test::Unit::TestCase assert_equal(8481, ks.first) assert_equal(29566, ks.last) end - end