2 # rbchise compatible ruby library by eto 2003-0317
6 class IDS_TEXT_DB < DB #======================================================================
10 #IDS-UCS-Compat-Supplement.txt
19 IDS-JIS-X0208-1990.txt
40 @glob, @pre, @post = "#{IDS_DB_DIR}/db/*", "#{IDS_DB_DIR}/db/", ""
41 dir = File.dirname(@pre)
42 Dir.mkdir(dir) unless FileTest.exist?(dir)
46 return unless block_given?
47 @ids_list.each {|file|
49 yield(IDS_DB_DIR+file)
55 next if line =~ /^;/ #コメントはとばす
57 code, char, ids = line.split
58 yield(code, char, ids)
64 dir = File.dirname(file) + '/../ids-new/'
65 Dir.mkdir(dir) if ! FileTest.directory?(dir)
66 newfile = dir + File.basename(file)
68 open(newfile, "w"){|out|
69 out.binmode.sync = true
70 each_line(file){|code, ch, ids|
71 char = Character.get(ch)
73 out.print "#{code} #{ch} #{ids}\n"
80 dir = File.dirname(file) + '/../ids-error'
81 Dir.mkdir(dir) unless FileTest.exist?(dir)
82 errfile = dir + '/' + File.basename(file)
84 open(errfile, "w"){|out|
85 out.binmode.sync = true
86 each_line(file){|code, ch, ids|
87 char = Character.get(ch)
88 ids_error = char['ids-error']
89 next if ids_error.nil?
90 out.print "#{code} #{ch} #{ids} #{ids_error}\n"
97 class IDS_DB < DB #======================================================================BDB化したIDS DBを扱う
100 @dbs = CharDB.instance
103 db = IDS_TEXT_DB.instance
106 @same_ids_counter = 0
107 @good_ids_counter = 0
108 @conflict_ids_counter = 0
109 db.each_line(file){|code, ch, ids|
112 ids = "" if ids == nil
113 next if ids == "" #IDSが定義されていない場合は、さっくりと無視するべしよ。
115 charimg = Character.get(ch) #実体参照である可能性がある
117 next if code =~ /'$/ || code =~ /"$/ #大漢和番号のダッシュ付きは無視する
118 char = Character.get("&"+code+";") #code表記を元に実体参照を作って解釈する
119 if char.nil? || char.to_s == "" #うまく文字にならなかった
120 print "char == null #{char.inspect} #{code} #{ch} #{ids}\n" unless code =~ /^M-/ || code =~ /^CB/
121 #大漢和、CBETA以外の場合は、エラーメッセージ。
124 if char != charimg #code表記と文字が一致していない?
125 unless code =~ /^M-/ || code =~ /^MH-/ || code =~ /^CB/ #食い違っていて当然であるので何もしない
126 print "unknown char #{char.inspect} #{code} #{ch} #{ids}\n"
127 next #それ以外の場合はエラーメッセージをだして、次へ。
130 #next if !char.has_attribute? #isolated characterはまぎれこませない。
132 ids.de_er! #実体参照を解除する
133 next if ids == char.to_s #もし文字とまったく一緒なら、意味が無いので情報を持たない
134 next if ids.char_length == 1
136 idstree = IDS_Tree.new(ids)
137 c = idstree.check_integrity
138 c = "contains self" if ids.include?(char.to_s)
139 if c #ちょっとでもエラーがある場合は、
140 char['ids-error'] = c #エラーを記録して、データとしては保持しない
144 if char['ids'].nil? || char['ids'] == "" #元々IDSが無かった場合は、
145 char['ids'] = ids #普通に代入すればそれでいいです。
146 @good_ids_counter += 1
147 else #しかしいままでにすでにIDSが定義されていた場合は?
148 if char['ids'] == ids #新しいIDSと古いIDSが完全に一致するなら無視しましょう。
149 @same_ids_counter += 1
150 else #しかしいままでのIDSと新しいIDSが食い違った場合は?
151 @conflict_ids_counter += 1
152 # print "conflict #{char.inspect} #{code} #{ids} #{char['ids']}\n"
156 print "#{file} #{@char_counter} #{@same_ids_counter} #{@conflict_ids_counter} #{@good_ids_counter}\n"
157 CharacterFactory.instance.reset()
159 @dbs.dump_db('ids-error') #テキスト化する
160 @dbs.dump_db('ids') #テキスト化する
164 @dbs.each('ids') {|k, v|
167 h[ids] = "" if h[ids].nil?
171 h[k] = char_sort(v) #文字の順番を、よく使うっぽいものからの順番にする
173 h.delete_if {|k, v| #h[k]が""になる可能性もあるが、それはkeyとして入れないことにする。
176 print "length #{h.length}\n"
177 cdb = CodesysDB.instance
178 cdb.make_db_no_question_mark('ids', h)
179 cdb.open_db('ids') #これが無いと、dump_dbされません。
182 def char_sort(composed)
183 return composed if composed.char_length == 1
189 if char.char_id < 0xfffff #Unicodeっぽい?
195 EntityReference.each_codesys{|codesys, er_prefix, keta, numtype|
199 # p [codesys, v] if v
200 if v #EntityReferenceの順番に準拠する。
208 # p ['yokuwakaran character', ar, ar[0].inspect_all, arorg]
209 EntityReference.each_codesys{|codesys, er_prefix, keta, numtype|
213 # p [codesys, v] if v
219 def dump_ids_duplicated
220 open('ids-duplicated.txt', 'w'){|out|
222 CodesysDB.instance.each('ids') {|k, v|
224 out.print "nil #{k} #{v}\n"
229 out.print "#{n} #{k} #{v}"
232 out.print " #{char.inspect}"
238 def make_ids_aggregated
239 @dbs.each('ids') {|k, v|
243 char['ids-aggregated'] = ag
245 @dbs.dump_db('ids-aggregated')
247 def dump_ids_aggregated
248 open('ids-aggregated.txt', 'w'){|out|
250 @dbs.each('ids') {|k, v|
253 ag = char['ids-aggregated']
254 out.print "#{char.to_s} #{ag} #{ids}\n" if ids != ag
259 @dbs.each('ids') {|k, v|
266 break if ids == pids #これ以上分割できないようだったら終了〜。
269 p [char.to_s, pids, ids, ar] if 10 < counter #これは何かおかしいぞと
274 #やっぱりIDS文字も加えることにする. by eto 2003-02-05
276 # ch.char.is_ids? #IDS文字はまぎれこませない。
279 char['ids-parts'] = str
281 @dbs.dump_db('ids-parts')
283 def make_ids_contained
285 @dbs.each('ids-parts') {|k, v|
287 parts = char.ids_parts
288 parts.each_char {|ch|
290 h[ch] = [] if h[ch].nil?
293 # part['ids-contained'] = "" if part['ids-contained'].nil?
294 # part['ids-contained'] += k
300 char['ids-contained'] = v.join('')
303 @dbs.dump_db('ids-contained')
305 def make_ids_decomposed
306 @dbs.each('ids') {|k, v|
308 de= char.decompose_all
309 char['ids-decomposed'] = de
311 @dbs.dump_db('ids-decomposed')
315 class Node < Array #=======================================================木構造の中の一つの枝
316 def initialize(nodeleaf=nil, nodenum=nil)
321 original_add(@nodeleaf)
325 alias original_add <<
326 private :original_add
329 @nodenum -= 1 if @nodenum
335 ar += n.nodes if n.is_a? Node
341 class Tree #======================================================================木構造を扱う
346 @depth = 1 #stackの深さが最大になったところの値、木構造が無いときは1となる
348 def depth() @depth - 1 end
349 def add_node(nodeleaf=nil, nodenum=nil) #枝を追加
350 new_node = Node.new(nodeleaf, nodenum)
351 @stack.last << new_node
353 if @depth < @stack.length
354 @depth = @stack.length
358 def end_node() #この枝は終り
362 def add_leaf(a) #葉を追加
368 n = @stack.last.nodenum
375 n = @stack.last.nodenum
376 return nil if @root.length == 0 #no tree is good tree
377 return "unmatch leaves" if n && n != 0
378 return "extra nodes" if @root.first.is_a?(Node) && @root.length != 1
379 return "extra leaves" if @root.length != 1
392 def to_s() @root.to_s end
393 def inspect() @root.inspect end
396 class IDS_Tree < Tree #======================================================================
404 char = Character.new(ch)
406 add_node(char, ids_operator_argc(char))
413 return true if "+*".include?(obj.to_s) #テスト用ですかね
414 return true if obj.is_ids?
417 def ids_operator_argc(obj)
418 return obj.ids_operator_argc if 0 < obj.ids_operator_argc
423 return r if r #不完全がすでにわかっているならreturn
424 return "contains ques" if @str =~ /\?/ #?が含まれている?
429 class IDS #======================================================================IDSそのものを扱うclass
430 def initialize(str) #IDS文字列をうけとる。
435 def parse_x #柔軟型のParse. IDSキャラクターが前にきてなくてもよい。などなど。
439 class Counter #======================================================================
441 #counter = Counter.new(50) { exit }
458 #----------------------------------------------------------------------end.