i
[chise/ruby.git] / chise / kageserver.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "singleton"
4 require "net/http"
5 require "uri"
6 $LOAD_PATH << "../../lib"
7 require "chise"
8 require "chise/network" #漢字間接続のネットワークを計算する
9 #require "chise/kanjilist"
10 include CHISE
11
12 class KageServer
13   include Singleton
14   TYPES = "skeleton mincho gothic".split
15   SKELETON = 0
16   MINCHO = 1
17   GOTHIC = 2
18   URL  = "http://192.168.2.60:5100/"
19   def initialize(url=URL)
20     @url = url
21     @glyphs = []
22     @use_cache = true #デフォルト: cacheに存在する場合はcacheから引き出す。
23     @offline = false #テスト用
24     @offline = true #テスト用
25     c = Config.instance
26     @cache_dir = c.base_dir+"/cache_kage"
27     Dir.mkdir(@cache_dir) unless FileTest.directory?(@cache_dir)
28   end
29   attr_accessor :url, :use_cache, :offline
30   def filename(num, type=SKELETON) sprintf("u%04x.%s", num, TYPES[type]) end
31   def cache_file(num, type=SKELETON) @cache_dir+"/"+filename(num, type)+".svg" end
32
33   def list_cache
34     ar = []
35     Dir.chdir(@cache_dir)
36     Dir.glob("*.svg").each {|file|
37       if file =~ /^u([0-9a-fA-F]+).skeleton.svg$/
38         code = $1.hex
39         ar << code
40       end
41     }
42     ar
43   end
44
45   def get(num, type=SKELETON)
46     return open(cache_file(num, type)).read if FileTest.exist?(cache_file(num, type))
47     svg = get_http(num, type)
48     return svg if svg
49     return nil
50   end
51   def get_http(num, type=SKELETON)
52     return nil if @offline
53     uri = URI.parse(URL + filename(num, type))
54     p ["uri", uri.to_s]
55     Net::HTTP.version_1_1   # declear to use 1.1 features.
56     Net::HTTP.start( uri.host, uri.port ) {|http|
57       response, body = http.get("/"+filename(num, type)+".svg")
58       if body
59         if error?(body)
60 #         p ["error", uri.to_s]
61           return nil
62         else
63           store_cache(num, type, body)
64           return body
65         end
66       end
67     }
68     return nil
69   end
70   def store_cache(num, type, svg)
71     #p ["store", num]
72     open(cache_file(num, type), "w") {|f|
73       f.print svg
74     }
75   end
76   def error?(svg)
77     (svg =~ /<!-- error -->/)
78   end
79   def read_list
80     h = {}
81     open("kage-list.txt"){|f|
82       while line = f.gets
83         if line =~ /u([0-9a-f]+)\.skeleton/
84           code = $1
85           num = code.hex
86           error = false
87           error = true if line =~ /error/
88           h[num] = error
89         end
90       end
91     }
92     return h
93   end
94   def get_all
95     #error_h = read_list
96     STDOUT.binmode
97     @kn = KanjiNetwork.new
98     @kl = KanjiList.instance
99     #list = @kl.awase(0)
100     #list = @kl.awase(1)
101     #list = @kl.joyo
102     #list = open("../../jis.txt").read
103     list = @kl.jisx0208()
104     @kn.make_network(list)
105     nodes, edges = @kn.nodes_and_edges
106     ar = []
107     nodes.each {|ch|
108       char = ch.char
109       num = char.to_i
110       next if 0xffff < num
111       next if num == 0x3561
112       next if num == 0x4fdb
113       next if num == 0x58d1
114       next if num == 0x891d
115       next if num == 0x8902
116       next if num < 0x8900
117       ar << num
118     }
119     get_ar(ar)
120   end
121   def get_ar(ar)
122     ar.each {|num| #intの数列
123       char = Character.get(num)
124       ch = char.to_s
125       er = char.to_er
126       #TYPES.each_with_index {|type, index|
127       #(0..2).each {|index|
128       (0..0).each {|index|
129         result = get(num, index) #cacheに保存するべしと。
130         next if result
131         err = "error"
132         print "#{er}    #{ch}   #{err}\n"
133       }
134     }
135   end
136   def test_kanji
137     char = "&CDP-8BA5;".de_er
138     #p char.inspect_all
139     #str = (char.to_s+"真")
140     str = (char.to_s+"直")
141     p str.find
142   end
143 end
144
145 if $0 == __FILE__
146   ks = KageServer.instance
147   #print ks.get(0x4e03)
148   ks.get_all
149 end