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