dc1295d5665788994bfc4e38bbe106f0c888e0ed
[chise/ruby.git] / src / kageserver.rb
1 #!/usr/bin/env ruby
2
3 $LOAD_PATH << '../ruby/src'
4 require 'chise'
5 include CHISE
6 require 'singleton'
7 require 'net/http'
8 require 'uri'
9 require 'network' #漢字間接続のネットワークを計算する
10
11 class KageServer #======================================================================
12   include Singleton
13   TYPES = "skeleton mincho gothic".split
14   SKELETON = 0
15   MINCHO = 1
16   GOTHIC = 2
17   HOME_DIR = "d:/work/chise/"
18   CACHE_DIR = HOME_DIR + "cache_kage"
19   URL  = "http://192.168.2.60:5100/"
20   #URL = "http://218.219.209.16:5100/"
21   def initialize(url=URL)
22     @url = url
23     @glyphs = []
24     @use_cache = true #デフォルト: cacheに存在する場合はcacheから引き出す。
25     @offline = false #テスト用
26     @offline = true #テスト用
27     Dir.mkdir(CACHE_DIR) unless FileTest.directory?(CACHE_DIR)
28   end
29   attr_accessor :url, :use_cache
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     open(cache_file(num, type), "w") {|f|
72       f.print svg
73     }
74   end
75   def error?(svg)
76     (svg =~ /<!-- error -->/)
77   end
78   def read_list
79     h = {}
80     open("kage-list.txt"){|f|
81       while line = f.gets
82         if line =~ /u([0-9a-f]+)\.skeleton/
83           code = $1
84           num = code.hex
85           error = false
86           error = true if line =~ /error/
87           h[num] = error
88         end
89       end
90     }
91     return h
92   end
93   def get_all
94     #error_h = read_list
95     STDOUT.binmode
96     @kn = KanjiNetwork.new
97     @kl = KanjiList.instance
98     #list = @kl.awase(0)
99     #list = @kl.awase(1)
100     list = @kl.joyo
101     @kn.make_network(list)
102     nodes, edges = @kn.nodes_and_edges
103     ar = []
104     nodes.each {|ch|
105       char = ch.char
106       num = char.to_i
107       #next if 0xffff < num
108       ar << num
109     }
110     get_ar(ar)
111   end
112   def get_ar(ar)
113     ar.each {|num| #intの数列
114       char = Character.get(num)
115       ch = char.to_s
116       er = char.to_er
117       #TYPES.each_with_index {|type, index|
118       #(0..2).each {|index|
119       (0..0).each {|index|
120         result = get(num, index) #cacheに保存するべしと。
121         next if result
122         err = "error"
123         print "#{er}    #{ch}   #{err}\n"
124       }
125     }
126   end
127   def test_kanji
128     char = "&CDP-8BA5;".de_er
129     #p char.inspect_all
130     #str = (char.to_s+"真")
131     str = (char.to_s+"直")
132     p str.find
133   end
134 end
135
136 if $0 == __FILE__ #======================================================================
137   ks = KageServer.instance
138   #print ks.get(0x4e03)
139   ks.get_all
140 end
141
142 #----------------------------------------------------------------------end.