add map_character
[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   def initialize(url=URL)
21     @url = url
22     @glyphs = []
23     @use_cache = true #デフォルト: cacheに存在する場合はcacheから引き出す。
24     @offline = false #テスト用
25     #@offline = true #テスト用
26     Dir.mkdir(CACHE_DIR) unless FileTest.directory?(CACHE_DIR)
27   end
28   attr_accessor :url, :use_cache
29   def filename(num, type=SKELETON) sprintf("u%04x.%s", num, TYPES[type]) end
30   def cache_file(num, type=SKELETON) CACHE_DIR+"/"+filename(num, type)+".svg" end
31
32   def list_cache
33     ar = []
34     Dir.chdir(CACHE_DIR)
35     Dir.glob("*.svg").each {|file|
36       if file =~ /^u([0-9a-fA-F]+).skeleton.svg$/
37         code = $1.hex
38         ar << code
39       end
40     }
41     ar
42   end
43
44   def get(num, type=SKELETON)
45     return open(cache_file(num, type)).read if FileTest.exist?(cache_file(num, type))
46     svg = get_http(num, type)
47     return svg if svg
48     return nil
49   end
50   def get_http(num, type=SKELETON)
51     return nil if @offline
52     uri = URI.parse(URL + filename(num, type))
53     p ['uri', uri.to_s]
54     Net::HTTP.version_1_1   # declear to use 1.1 features.
55     Net::HTTP.start( uri.host, uri.port ) {|http|
56       response, body = http.get('/'+filename(num, type)+".svg")
57       if body
58         if error?(body)
59 #         p ['error', uri.to_s]
60           return nil
61         else
62           store_cache(num, type, body)
63           return body
64         end
65       end
66     }
67     return nil
68   end
69   def store_cache(num, type, svg)
70     #p ["store", num]
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     list = open("../../jis.txt").read
102     @kn.make_network(list)
103     nodes, edges = @kn.nodes_and_edges
104     ar = []
105     nodes.each {|ch|
106       char = ch.char
107       num = char.to_i
108       next if 0xffff < num
109       next if num == 0x3561
110       next if num == 0x4fdb
111       next if num == 0x58d1
112       next if num == 0x891d
113       next if num == 0x8902
114       next if num < 0x8900
115       ar << num
116     }
117     get_ar(ar)
118   end
119   def get_ar(ar)
120     ar.each {|num| #intの数列
121       char = Character.get(num)
122       ch = char.to_s
123       er = char.to_er
124       #TYPES.each_with_index {|type, index|
125       #(0..2).each {|index|
126       (0..0).each {|index|
127         result = get(num, index) #cacheに保存するべしと。
128         next if result
129         err = "error"
130         print "#{er}    #{ch}   #{err}\n"
131       }
132     }
133   end
134   def test_kanji
135     char = "&CDP-8BA5;".de_er
136     #p char.inspect_all
137     #str = (char.to_s+"真")
138     str = (char.to_s+"直")
139     p str.find
140   end
141 end
142
143 if $0 == __FILE__ #======================================================================
144   ks = KageServer.instance
145   #print ks.get(0x4e03)
146   ks.get_all
147 end
148
149 #----------------------------------------------------------------------end.