update.
[chise/ruby.git] / chise / ids.rb
1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
2
3 require "chise/idstree"
4
5 module CHISE
6   IDC_0 = "\342\277\260"
7   IDC_1 = "\342\277\261"
8   IDC_2 = "\342\277\262"
9   IDC_3 = "\342\277\263"
10   IDC_4 = "\342\277\264"
11   IDC_5 = "\342\277\265"
12   IDC_6 = "\342\277\266"
13   IDC_7 = "\342\277\267"
14   IDC_8 = "\342\277\270"
15   IDC_9 = "\342\277\271"
16   IDC_A = "\342\277\272"
17   IDC_B = "\342\277\273"
18
19   IDC_LEFT_TO_RIGHT = IDC_0
20   IDC_ABOVE_TO_BELOW = IDC_1
21   IDC_LEFT_TO_MIDDLE_AND_RIGHT = IDC_2
22   IDC_ABOVE_TO_MIDDLE_AND_BELOW = IDC_3
23   IDC_FULL_SURROUND = IDC_4
24   IDC_SURROUND_FROM_ABOVE = IDC_5
25   IDC_SURROUND_FROM_BELOW = IDC_6
26   IDC_SURROUND_FROM_LEFT = IDC_7
27   IDC_SURROUND_FROM_UPPER_LEFT = IDC_8
28   IDC_SURROUND_FROM_UPPER_RIGHT = IDC_9
29   IDC_SURROUND_FROM_LOWER_LEFT = IDC_A
30   IDC_OVERLAID = IDC_B
31
32   module StringIDS
33     def decompose
34       map_char {|ch| ch.char.decompose }
35     end
36
37     def decompose_all
38       map_char {|ch| ch.char.decompose_all }
39     end
40
41     def ids_tree() IDS_Tree.new(self); end
42
43     def compose(dbname="ids")
44       ids = self.aggregate
45       cd = ChiseDB.instance
46       #byids = cd.get_by_ids_db(dbname)
47       byids = cd.get_ccs(dbname)
48       #cid = byids.decode(ids)
49       cid = byids.get_by_str(ids)
50       return "" if cid.nil? # TO CHECK: why "", not nil?
51       composed = Character.get(cid).to_s
52       return "" if composed.nil?
53       return "" if composed.char_length == 0
54       return composed if composed.char_length == 1
55       composed.each_char {|ch|
56         char = ch.char
57         return ch # TO CHECK: the first character?
58       }
59       "" # TO CHECK: why "", not nil?
60     end
61
62     def aggregate(dbname="ids")
63       # In each sub part of IDS, search the corresponding char_id.
64       # If you could search the corresponding char_id, substitute with it.
65       #tree = self.ids_tree
66       tree = IDS_Tree.new(self)
67       return self if tree.depth <= 1 # no sub_node
68       tree.sub_nodes.each {|node|
69         c = node.compose(dbname)
70         next if c.nil? || c == ""
71         n = self.gsub(node, c)
72         return n.aggregate(dbname)
73       }
74       self
75     end
76
77     def find() # "日雲"→"曇"とかいう感じの操作
78       ar = []
79       length = char_length()
80       each_char {|ch|
81         char = ch.char
82         ar << char.ids_contained #その文字を含んでいる漢字のリスト
83       }
84       h = Hash.new(0)
85       #qp ar
86       ar.each {|list|
87         next if list.nil?
88         list.each_char {|ch|
89           h[ch] += 1
90         }
91       }
92       str = ""
93       h.each {|k, v|
94         #      p [k, v]
95         if length == v #全部に顔を出していたら
96           str += k
97         end
98       }
99       #    p str
100       str
101     end
102   end
103
104   module CharacterIDC
105     def is_idc?
106       0x2ff0 <= @char_id && @char_id <= 0x2fff
107     end
108
109     def idc_argument_number
110       return 0 unless is_idc?
111       return 3 if @char_id == 0x2ff2 || @char_id == 0x2ff3
112       return 2
113     end
114   end
115
116   module CharacterIDS
117     def decompose_by_meaning
118       k = self.to_s
119       ids = self.ids_represent
120       return ids if ids && !ids.empty? && k != ids
121       ids = self.ids_element
122       return ids if ids && !ids.empty? && k != ids
123       ids = self.ids_meaning
124       return ids if ids && !ids.empty? && k != ids
125       decompose
126     end
127
128     def decompose # by glyph
129       k = self.to_s
130       ids = self.ids
131       return ids if ids && !ids.empty? && k != ids
132       ids = self.ids_org
133       return ids if ids && !ids.empty? && k != ids
134       k
135     end
136
137     def decompose_all
138       pde = ""
139       de = self.decompose # the start point.
140       level = 0
141       while true
142         pde = de
143         de = pde.decompose # decompose it again.
144         break if pde == de # previous is same.
145         exit if 10 < level # p ["too many recursive", self] 
146         level += 1
147       end
148       de
149     end
150   end
151 end