update.
[chise/ruby.git] / chise / org-character.rb
1 module CHISE
2   class Character
3     def mcs_hex() sprintf("%x", @char_id) end
4
5     def char_feature_alist() check_all_database(); @features; end
6     def char_feature_list()  check_all_database(); @features.keys; end
7     alias alist char_feature_alist
8     alias list  char_feature_list
9
10     def has_feature?() # \88Ó\96¡\82Ì\82 \82éfeature\82ð\8e\9d\82Á\82Ä\82Ü\82·\82©?
11       keys = list
12       keys.delete_if {|k|
13         k =~ /ids/
14       }
15       return (keys.length != 0)
16     end
17
18     def check_database(a)
19       db = CharDB.instance
20       u8 = mcs_utf8()
21       db.get(a, u8) # u8\82Å\95\\82³\82ê\82é\95\8e\9a\82Ìa\83A\83g\83\8a\83r\83\85\81[\83g\82ð\92²\82×\82é\81B
22     end
23
24     def check_all_database() # \8c»\8dÝ\82Ì@char_id\82©\82ç\81A\95\8e\9a\83f\81[\83^\83x\81[\83X\82ð\8eQ\8fÆ\82·\82é
25       return if @check_all_database
26       return if @char_id.nil?
27       db = CharDB.instance
28       u8 = mcs_utf8()
29       atrs = db.get_all(u8) #u8\82Å\95\\82³\82ê\82é\95\8e\9a\82Ì\83A\83g\83\8a\83r\83\85\81[\83g\82ð\91S\95\94\8e\9d\82Á\82Ä\82±\82¢
30       atrs.each {|a,v|
31         @features[a] = v #\82Æ\82©\82¢\82¤\8a´\82\82Å\91ã\93ü\82·\82é\82Ì\82Å\82¦\82¦\82©\82È?
32       }
33       @check_all_database = true #\8fd\82¢\8f\88\97\9d\82È\82Ì\82Å\88ê\89\9echeck\82·\82é
34     end
35
36     def ucs()
37       #p "ucs"
38       #ar=%w{ucs ucs-big5 ucs-cdp ucs-cns ucs-jis ucs-ks =>ucs =>ucs* =>ucs-jis}
39       #ar=%w{ucs ucs-jis ucs-big5 ucs-cdp ucs-cns ucs-ks =>ucs =>ucs* =>ucs-jis}
40       ar = %w{ucs-jis ucs =>ucs-jis}
41       #\95À\82Ñ\8f\87\82Í\9c\93\88Ó\93I\82Å\81Aucs-jis\82ð\90æ\82É\8fo\82µ\82Ä\82¢\82é\81B\96{\97\88\82Í\82±\82ê\82à\8ew\92è\82Å\82«\82é\82æ\82¤\82É\82·\82é\82×\82«\81B
42       ar.each {|a|      #p [a]
43         u = get_char_feature(a)
44         return u if u
45       }
46       nil
47     end
48
49     #-------------------------------------------------------------------CCS\8aÖ\8cW
50     def to_utf8() Uconv.u4tou8(Character.u4itou4(ucs())) end #UTF8\95\8e\9a\97ñ\82ð\95Ô\82·
51     #alias to_s to_utf8
52     alias to_s mcs_utf8
53
54     def map_utf8()
55       u = ucs()
56       if u.nil? || 0xffff < u
57         return to_er()
58       else
59         return to_utf8()
60       end
61     end
62     alias map_ucs map_utf8
63
64     def map_ucs_er()
65       u = ucs()
66       if u.nil? || 0xffff < u
67         return to_er()
68       else
69         return Character.get(u).to_er()
70       end
71     end
72
73     def to_euc()
74       u = ucs()
75       return "" if u.nil? || 0xffff < u
76       Uconv.u16toeuc(Uconv.u4tou16(Character.u4itou4(ucs())))
77     end
78
79     def map_euc()
80       e = to_euc()
81       return e if e != ""
82       return to_er()
83     end
84
85     def to_sjis()
86       u = ucs()
87       return "" if u.nil? || 0xffff < u
88       Uconv.u16tosjis(Uconv.u4tou16(Character.u4itou4(ucs())))
89     end
90
91     def map_sjis()
92       e = to_sjis()
93       return e if e != ""
94       return to_er()
95     end
96
97     def to_er_list()
98       ar = []
99       EntityReference.each_codesys {|codesys, er_prefix, keta, numtype|
100         er = to_er(codesys)
101         ar << er if er
102       }
103       ar
104     end
105
106     def inspect_all_codesys() #\96¢\8a®\90¬
107       #to_er\82ð\91S\82Ä\82Ìcodesys\82É\82¨\82¢\82Ä\8eÀ\8ds\82·\82é\81B\82»\82Ì\8c\8b\89Ê\82ð\83R\83\93\83p\83N\83g\82É\82Ü\82Æ\82ß\82é
108     end
109
110     def inspect_all()
111       ar = [inspect.chop]
112       alist.to_a.sort.each {|a, v| ar << "#{a}:#{v}" }
113       return ar.join(",")+">"
114     end
115
116     def dump_all()
117       ar = [inspect]
118       alist.to_a.sort.each {|a, v| ar << "#{a}:#{v}" }
119       return ar.join('\n')+'\n'
120     end
121
122     def get_features()
123       str = ""
124       alist.to_a.sort.each {|a, v|
125         str += "#{a}: #{v}\n"
126       }
127       str
128     end
129
130     def inspect_ids(hex_flag=false)
131       ids = decompose
132       ar = []
133       ar << (hex_flag ? "x"+mcs_hex : to_utf8)
134       if to_s != ids #ids\82ª\95\94\95i\82»\82Ì\82à\82Ì\82¾\82Á\82½\82ç\95\94\95i\92Ç\89Á\82Í\82µ\82È\82¢
135         ids.each_char {|ch|
136           char = ch.char
137           next if char.is_ids?
138           if hex_flag then
139             ar << "x"+char.mcs_hex
140           else
141             u = char.to_utf8
142             if u != ""
143               ar << u
144             else
145               ar << char.to_er
146             end
147           end
148         }
149       end
150       return "("+ar.join("\t")+")"
151     end
152
153   end
154 end