1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
6 # IDC_LEFT_TO_RIGHT = "⿰"
7 # IDC_ABOVE_TO_BELOW = "⿱"
8 # IDC_LEFT_TO_MIDDLE_AND_RIGHT = "⿲"
9 # IDC_ABOVE_TO_MIDDLE_AND_BELOW = "⿳"
10 # IDC_FULL_SURROUND = "⿴"
11 # IDC_SURROUND_FROM_ABOVE = "⿵"
12 # IDC_SURROUND_FROM_BELOW = "⿶"
13 # IDC_SURROUND_FROM_LEFT = "⿷"
14 # IDC_SURROUND_FROM_UPPER_LEFT = "⿸"
15 # IDC_SURROUND_FROM_UPPER_RIGHT = "⿹"
16 # IDC_SURROUND_FROM_LOWER_LEFT = "⿺"
19 IDC_LEFT_TO_RIGHT = "\342\277\260" #2FF0
20 IDC_ABOVE_TO_BELOW = "\342\277\261"
21 IDC_LEFT_TO_MIDDLE_AND_RIGHT = "\342\277\262"
22 IDC_ABOVE_TO_MIDDLE_AND_BELOW = "\342\277\263"
23 IDC_FULL_SURROUND = "\342\277\264" #2FF4
24 IDC_SURROUND_FROM_ABOVE = "\342\277\265"
25 IDC_SURROUND_FROM_BELOW = "\342\277\266"
26 IDC_SURROUND_FROM_LEFT = "\342\277\267"
27 IDC_SURROUND_FROM_UPPER_LEFT = "\342\277\270"
28 IDC_SURROUND_FROM_UPPER_RIGHT = "\342\277\271"
29 IDC_SURROUND_FROM_LOWER_LEFT = "\342\277\272"
30 IDC_OVERLAID = "\342\277\273"
32 IDC_LR = IDC_LEFT_TO_RIGHT
33 IDC_AB = IDC_ABOVE_TO_BELOW
34 IDC_LM = IDC_LEFT_TO_MIDDLE_AND_RIGHT
35 IDC_AM = IDC_ABOVE_TO_MIDDLE_AND_BELOW
36 IDC_FS = IDC_FULL_SURROUND
37 IDC_FA = IDC_SURROUND_FROM_ABOVE
38 IDC_FB = IDC_SURROUND_FROM_BELOW
39 IDC_FL = IDC_SURROUND_FROM_LEFT
40 IDC_UL = IDC_SURROUND_FROM_UPPER_LEFT
41 IDC_UR = IDC_SURROUND_FROM_UPPER_RIGHT
42 IDC_LL = IDC_SURROUND_FROM_LOWER_LEFT
52 class IDS_TEXT_DB < DB
57 #IDS-UCS-Compat-Supplement.txt
66 IDS-JIS-X0208-1990.txt
89 @dir = Config.instance.ids_dir
91 @glob, @pre, @post = "#{@dir}/db/*", "#{@dir}/db/", ""
92 dir = File.dirname(@pre)
93 Dir.mkdir(dir) unless FileTest.exist?(dir)
98 return unless block_given?
99 @ids_list.each {|file|
108 next if line =~ /^;/ #コメントはとばす
110 code, char, ids = line.split
111 yield(code, char, ids)
118 dir = File.dirname(file) + "/../ids-new/"
119 Dir.mkdir(dir) if ! FileTest.directory?(dir)
120 newfile = dir + File.basename(file)
122 open(newfile, "w"){|out|
123 out.binmode.sync = true
124 each_line(file){|code, ch, ids|
125 char = Character.get(ch)
127 out.print "#{code} #{ch} #{ids}\n"
135 dir = File.dirname(file) + "/../ids-error"
136 Dir.mkdir(dir) unless FileTest.exist?(dir)
137 errfile = dir + "/" + File.basename(file)
139 open(errfile, "w"){|out|
140 out.binmode.sync = true
141 each_line(file){|code, ch, ids|
142 char = Character.get(ch)
143 ids_error = char["ids-error"]
144 next if ids_error.nil?
145 out.print "#{code} #{ch} #{ids} #{ids_error}\n"
152 class IDS_DB < DB # BDB化したIDS DBを扱う
156 @dbs = CharDB.instance
160 db = IDS_TEXT_DB.instance
163 @same_ids_counter = 0
164 @good_ids_counter = 0
165 @conflict_ids_counter = 0
166 db.each_line(file){|code, ch, ids|
169 ids = "" if ids == nil
170 next if ids == "" #IDSが定義されていない場合は、さっくりと無視するべしよ。
172 charimg = Character.get(ch) #実体参照である可能性がある
174 next if code =~ /'$/ || code =~ /"$/ #大漢和番号のダッシュ付きは無視する
175 char = Character.get("&"+code+";") #code表記を元に実体参照を作って解釈する
176 if char.nil? || char.to_s == "" #うまく文字にならなかった
177 print "char == null #{char.inspect} #{code} #{ch} #{ids}\n" unless code =~ /^M-/ || code =~ /^CB/
178 #大漢和、CBETA以外の場合は、エラーメッセージ。
181 if char != charimg #code表記と文字が一致していない?
182 unless code =~ /^M-/ || code =~ /^MH-/ || code =~ /^CB/ #食い違っていて当然であるので何もしない
183 print "unknown char #{char.inspect} #{code} #{ch} #{ids}\n"
184 next #それ以外の場合はエラーメッセージをだして、次へ。
187 #next if !char.has_attribute? #isolated characterはまぎれこませない。
189 ids.de_er! #実体参照を解除する
190 next if ids == char.to_s #もし文字とまったく一緒なら、意味が無いので情報を持たない
191 next if ids.char_length == 1
193 idstree = IDS_Tree.new(ids)
194 c = idstree.check_integrity
195 c = "contains self" if ids.include?(char.to_s)
196 if c #ちょっとでもエラーがある場合は、
197 char["ids-error"] = c #エラーを記録して、データとしては保持しない
201 if char["ids"].nil? || char["ids"] == "" #元々IDSが無かった場合は、
202 char["ids"] = ids #普通に代入すればそれでいいです。
203 @good_ids_counter += 1
204 else #しかしいままでにすでにIDSが定義されていた場合は?
205 if char["ids"] == ids #新しいIDSと古いIDSが完全に一致するなら無視しましょう。
206 @same_ids_counter += 1
207 else #しかしいままでのIDSと新しいIDSが食い違った場合は?
208 @conflict_ids_counter += 1
209 # print "conflict #{char.inspect} #{code} #{ids} #{char["ids"]}\n"
213 print "#{file} #{@char_counter} #{@same_ids_counter} #{@conflict_ids_counter} #{@good_ids_counter}\n"
214 CharacterFactory.instance.reset()
216 @dbs.dump_db("ids-error") #テキスト化する
217 @dbs.dump_db("ids") #テキスト化する
222 @dbs.each("ids") {|k, v|
225 h[ids] = "" if h[ids].nil?
229 h[k] = char_sort(v) #文字の順番を、よく使うっぽいものからの順番にする
231 h.delete_if {|k, v| #h[k]が""になる可能性もあるが、それはkeyとして入れないことにする。
234 print "length #{h.length}\n"
235 cdb = CodesysDB.instance
236 cdb.make_db_no_question_mark("ids", h)
237 cdb.open_db("ids") #これが無いと、dump_dbされません。
241 def char_sort(composed)
242 return composed if composed.char_length == 1
248 if char.char_id < 0xfffff #Unicodeっぽい?
254 EntityReference.each_codesys{|codesys, er_prefix, keta, numtype|
258 # p [codesys, v] if v
259 if v #EntityReferenceの順番に準拠する。
267 # p ["yokuwakaran character", ar, ar[0].inspect_all, arorg]
268 EntityReference.each_codesys{|codesys, er_prefix, keta, numtype|
272 # p [codesys, v] if v
279 def dump_ids_duplicated
280 open("ids-duplicated.txt", "w"){|out|
282 CodesysDB.instance.each("ids") {|k, v|
284 out.print "nil #{k} #{v}\n"
289 out.print "#{n} #{k} #{v}"
292 out.print " #{char.inspect}"
299 def make_ids_aggregated
300 @dbs.each("ids") {|k, v|
304 char["ids-aggregated"] = ag
306 @dbs.dump_db("ids-aggregated")
309 def dump_ids_aggregated
310 open("ids-aggregated.txt", "w"){|out|
312 @dbs.each("ids") {|k, v|
315 ag = char["ids-aggregated"]
316 out.print "#{char.to_s} #{ag} #{ids}\n" if ids != ag
322 @dbs.each("ids") {|k, v|
329 break if ids == pids #これ以上分割できないようだったら終了〜。
332 p [char.to_s, pids, ids, ar] if 10 < counter #これは何かおかしいぞと
337 #やっぱりIDS文字も加えることにする. by eto 2003-02-05
339 # ch.char.is_ids? #IDS文字はまぎれこませない。
342 char["ids-parts"] = str
344 @dbs.dump_db("ids-parts")
347 def make_ids_contained
349 @dbs.each("ids-parts") {|k, v|
351 parts = char.ids_parts
352 parts.each_char {|ch|
354 h[ch] = [] if h[ch].nil?
357 # part["ids-contained"] = "" if part["ids-contained"].nil?
358 # part["ids-contained"] += k
364 char["ids-contained"] = v.join("")
367 @dbs.dump_db("ids-contained")
370 def make_ids_decomposed
371 @dbs.each("ids") {|k, v|
373 de= char.decompose_all
374 char["ids-decomposed"] = de
376 @dbs.dump_db("ids-decomposed")
381 class Node < Array # 木構造の中の一つの枝
382 def initialize(nodeleaf=nil, nodenum=nil)
387 original_add(@nodeleaf)
392 alias original_add <<
393 private :original_add
397 @nodenum -= 1 if @nodenum
404 ar += n.nodes if n.is_a? Node
416 @depth = 1 #stackの深さが最大になったところの値、木構造が無いときは1となる
419 def depth() @depth - 1 end
421 def add_node(nodeleaf=nil, nodenum=nil) #枝を追加
422 new_node = Node.new(nodeleaf, nodenum)
423 @stack.last << new_node
425 if @depth < @stack.length
426 @depth = @stack.length
431 def end_node() #この枝は終り
436 def add_leaf(a) #葉を追加
443 n = @stack.last.nodenum
451 n = @stack.last.nodenum
452 return nil if @root.length == 0 #no tree is good tree
453 return "unmatch leaves" if n && n != 0
454 return "extra nodes" if @root.first.is_a?(Node) && @root.length != 1
455 return "extra leaves" if @root.length != 1
471 def to_s() @root.to_s end
473 def inspect() @root.inspect end
476 class IDS_Tree < Tree
485 char = Character.new(ch)
487 add_node(char, ids_operator_argc(char))
495 return true if "+*".include?(obj.to_s) #テスト用ですかね
496 return true if obj.is_ids?
500 def ids_operator_argc(obj)
501 return obj.ids_operator_argc if 0 < obj.ids_operator_argc
507 return r if r #不完全がすでにわかっているならreturn
508 return "contains ques" if @str =~ /\?/ #?が含まれている?
513 class IDS # IDSそのものを扱うclass
514 def initialize(str) #IDS文字列をうけとる。
521 def parse_x #柔軟型のParse. IDSキャラクターが前にきてなくてもよい。などなど。
527 #counter = Counter.new(50) { exit }