1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
3 $LOAD_PATH << "../../lib" if $0 == __FILE__
7 # IDC_LEFT_TO_RIGHT = "⿰"
8 # IDC_ABOVE_TO_BELOW = "⿱"
9 # IDC_LEFT_TO_MIDDLE_AND_RIGHT = "⿲"
10 # IDC_ABOVE_TO_MIDDLE_AND_BELOW = "⿳"
11 # IDC_FULL_SURROUND = "⿴"
12 # IDC_SURROUND_FROM_ABOVE = "⿵"
13 # IDC_SURROUND_FROM_BELOW = "⿶"
14 # IDC_SURROUND_FROM_LEFT = "⿷"
15 # IDC_SURROUND_FROM_UPPER_LEFT = "⿸"
16 # IDC_SURROUND_FROM_UPPER_RIGHT = "⿹"
17 # IDC_SURROUND_FROM_LOWER_LEFT = "⿺"
20 IDC_LEFT_TO_RIGHT = "\342\277\260" #2FF0
21 IDC_ABOVE_TO_BELOW = "\342\277\261"
22 IDC_LEFT_TO_MIDDLE_AND_RIGHT = "\342\277\262"
23 IDC_ABOVE_TO_MIDDLE_AND_BELOW = "\342\277\263"
24 IDC_FULL_SURROUND = "\342\277\264" #2FF4
25 IDC_SURROUND_FROM_ABOVE = "\342\277\265"
26 IDC_SURROUND_FROM_BELOW = "\342\277\266"
27 IDC_SURROUND_FROM_LEFT = "\342\277\267"
28 IDC_SURROUND_FROM_UPPER_LEFT = "\342\277\270"
29 IDC_SURROUND_FROM_UPPER_RIGHT = "\342\277\271"
30 IDC_SURROUND_FROM_LOWER_LEFT = "\342\277\272"
31 IDC_OVERLAID = "\342\277\273"
33 IDC_LR = IDC_LEFT_TO_RIGHT
34 IDC_AB = IDC_ABOVE_TO_BELOW
35 IDC_LM = IDC_LEFT_TO_MIDDLE_AND_RIGHT
36 IDC_AM = IDC_ABOVE_TO_MIDDLE_AND_BELOW
37 IDC_FS = IDC_FULL_SURROUND
38 IDC_FA = IDC_SURROUND_FROM_ABOVE
39 IDC_FB = IDC_SURROUND_FROM_BELOW
40 IDC_FL = IDC_SURROUND_FROM_LEFT
41 IDC_UL = IDC_SURROUND_FROM_UPPER_LEFT
42 IDC_UR = IDC_SURROUND_FROM_UPPER_RIGHT
43 IDC_LL = IDC_SURROUND_FROM_LOWER_LEFT
53 class IDS_TEXT_DB < DB
58 #IDS-UCS-Compat-Supplement.txt
67 IDS-JIS-X0208-1990.txt
90 @dir = Config.instance.ids_dir
92 @glob, @pre, @post = "#{@dir}/db/*", "#{@dir}/db/", ""
93 dir = File.dirname(@pre)
94 Dir.mkdir(dir) unless FileTest.exist?(dir)
99 return unless block_given?
100 @ids_list.each {|file|
109 next if line =~ /^;/ #コメントはとばす
111 code, char, ids = line.split
112 yield(code, char, ids)
119 dir = File.dirname(file) + "/../ids-new/"
120 Dir.mkdir(dir) if ! FileTest.directory?(dir)
121 newfile = dir + File.basename(file)
123 open(newfile, "w"){|out|
124 out.binmode.sync = true
125 each_line(file){|code, ch, ids|
126 char = Character.get(ch)
128 out.print "#{code} #{ch} #{ids}\n"
136 dir = File.dirname(file) + "/../ids-error"
137 Dir.mkdir(dir) unless FileTest.exist?(dir)
138 errfile = dir + "/" + File.basename(file)
140 open(errfile, "w"){|out|
141 out.binmode.sync = true
142 each_line(file){|code, ch, ids|
143 char = Character.get(ch)
144 ids_error = char["ids-error"]
145 next if ids_error.nil?
146 out.print "#{code} #{ch} #{ids} #{ids_error}\n"
153 class IDS_DB < DB # BDB化したIDS DBを扱う
157 @dbs = CharDB.instance
161 db = IDS_TEXT_DB.instance
164 @same_ids_counter = 0
165 @good_ids_counter = 0
166 @conflict_ids_counter = 0
167 db.each_line(file){|code, ch, ids|
170 ids = "" if ids == nil
171 next if ids == "" #IDSが定義されていない場合は、さっくりと無視するべしよ。
173 charimg = Character.get(ch) #実体参照である可能性がある
175 next if code =~ /'$/ || code =~ /"$/ #大漢和番号のダッシュ付きは無視する
176 char = Character.get("&"+code+";") #code表記を元に実体参照を作って解釈する
177 if char.nil? || char.to_s == "" #うまく文字にならなかった
178 print "char == null #{char.inspect} #{code} #{ch} #{ids}\n" unless code =~ /^M-/ || code =~ /^CB/
179 #大漢和、CBETA以外の場合は、エラーメッセージ。
182 if char != charimg #code表記と文字が一致していない?
183 unless code =~ /^M-/ || code =~ /^MH-/ || code =~ /^CB/ #食い違っていて当然であるので何もしない
184 print "unknown char #{char.inspect} #{code} #{ch} #{ids}\n"
185 next #それ以外の場合はエラーメッセージをだして、次へ。
188 #next if !char.has_attribute? #isolated characterはまぎれこませない。
190 ids.de_er! #実体参照を解除する
191 next if ids == char.to_s #もし文字とまったく一緒なら、意味が無いので情報を持たない
192 next if ids.char_length == 1
194 idstree = IDS_Tree.new(ids)
195 c = idstree.check_integrity
196 c = "contains self" if ids.include?(char.to_s)
197 if c #ちょっとでもエラーがある場合は、
198 char["ids-error"] = c #エラーを記録して、データとしては保持しない
202 if char["ids"].nil? || char["ids"] == "" #元々IDSが無かった場合は、
203 char["ids"] = ids #普通に代入すればそれでいいです。
204 @good_ids_counter += 1
205 else #しかしいままでにすでにIDSが定義されていた場合は?
206 if char["ids"] == ids #新しいIDSと古いIDSが完全に一致するなら無視しましょう。
207 @same_ids_counter += 1
208 else #しかしいままでのIDSと新しいIDSが食い違った場合は?
209 @conflict_ids_counter += 1
210 # print "conflict #{char.inspect} #{code} #{ids} #{char["ids"]}\n"
214 print "#{file} #{@char_counter} #{@same_ids_counter} #{@conflict_ids_counter} #{@good_ids_counter}\n"
215 CharacterFactory.instance.reset()
217 @dbs.dump_db("ids-error") #テキスト化する
218 @dbs.dump_db("ids") #テキスト化する
223 @dbs.each("ids") {|k, v|
226 h[ids] = "" if h[ids].nil?
230 h[k] = char_sort(v) #文字の順番を、よく使うっぽいものからの順番にする
232 h.delete_if {|k, v| #h[k]が""になる可能性もあるが、それはkeyとして入れないことにする。
235 print "length #{h.length}\n"
236 cdb = CodesysDB.instance
237 cdb.make_db_no_question_mark("ids", h)
238 cdb.open_db("ids") #これが無いと、dump_dbされません。
242 def char_sort(composed)
243 return composed if composed.char_length == 1
249 if char.char_id < 0xfffff #Unicodeっぽい?
255 EntityReference.each_codesys{|codesys, er_prefix, keta, numtype|
259 # p [codesys, v] if v
260 if v #EntityReferenceの順番に準拠する。
268 # p ["yokuwakaran character", ar, ar[0].inspect_all, arorg]
269 EntityReference.each_codesys{|codesys, er_prefix, keta, numtype|
273 # p [codesys, v] if v
280 def dump_ids_duplicated
281 open("ids-duplicated.txt", "w"){|out|
283 CodesysDB.instance.each("ids") {|k, v|
285 out.print "nil #{k} #{v}\n"
290 out.print "#{n} #{k} #{v}"
293 out.print " #{char.inspect}"
300 def make_ids_aggregated
301 @dbs.each("ids") {|k, v|
305 char["ids-aggregated"] = ag
307 @dbs.dump_db("ids-aggregated")
310 def dump_ids_aggregated
311 open("ids-aggregated.txt", "w"){|out|
313 @dbs.each("ids") {|k, v|
316 ag = char["ids-aggregated"]
317 out.print "#{char.to_s} #{ag} #{ids}\n" if ids != ag
323 @dbs.each("ids") {|k, v|
330 break if ids == pids #これ以上分割できないようだったら終了〜。
333 p [char.to_s, pids, ids, ar] if 10 < counter #これは何かおかしいぞと
338 #やっぱりIDS文字も加えることにする. by eto 2003-02-05
340 # ch.char.is_ids? #IDS文字はまぎれこませない。
343 char["ids-parts"] = str
345 @dbs.dump_db("ids-parts")
348 def make_ids_contained
350 @dbs.each("ids-parts") {|k, v|
352 parts = char.ids_parts
353 parts.each_char {|ch|
355 h[ch] = [] if h[ch].nil?
358 # part["ids-contained"] = "" if part["ids-contained"].nil?
359 # part["ids-contained"] += k
365 char["ids-contained"] = v.join("")
368 @dbs.dump_db("ids-contained")
371 def make_ids_decomposed
372 @dbs.each("ids") {|k, v|
374 de= char.decompose_all
375 char["ids-decomposed"] = de
377 @dbs.dump_db("ids-decomposed")
382 class Node < Array # 木構造の中の一つの枝
383 def initialize(nodeleaf=nil, nodenum=nil)
388 original_add(@nodeleaf)
393 alias original_add <<
394 private :original_add
398 @nodenum -= 1 if @nodenum
405 ar += n.nodes if n.is_a? Node
417 @depth = 1 #stackの深さが最大になったところの値、木構造が無いときは1となる
420 def depth() @depth - 1 end
422 def add_node(nodeleaf=nil, nodenum=nil) #枝を追加
423 new_node = Node.new(nodeleaf, nodenum)
424 @stack.last << new_node
426 if @depth < @stack.length
427 @depth = @stack.length
432 def end_node() #この枝は終り
437 def add_leaf(a) #葉を追加
444 n = @stack.last.nodenum
452 n = @stack.last.nodenum
453 return nil if @root.length == 0 #no tree is good tree
454 return "unmatch leaves" if n && n != 0
455 return "extra nodes" if @root.first.is_a?(Node) && @root.length != 1
456 return "extra leaves" if @root.length != 1
472 def to_s() @root.to_s end
474 def inspect() @root.inspect end
477 class IDS_Tree < Tree
486 char = Character.new(ch)
488 add_node(char, ids_operator_argc(char))
496 return true if "+*".include?(obj.to_s) #テスト用ですかね
497 return true if obj.is_ids?
501 def ids_operator_argc(obj)
502 return obj.ids_operator_argc if 0 < obj.ids_operator_argc
508 return r if r #不完全がすでにわかっているならreturn
509 return "contains ques" if @str =~ /\?/ #?が含まれている?
514 class IDS # IDSそのものを扱うclass
515 def initialize(str) #IDS文字列をうけとる。
522 def parse_x #柔軟型のParse. IDSキャラクターが前にきてなくてもよい。などなど。
528 #counter = Counter.new(50) { exit }