375a2d7f19c0905e4b17080e90615e37104c34ef
[chise/ruby.git] / chise / org-string.rb
1 class String
2   def each_character() to_a.each {|ch| yield ch.char } end
3   def char_length() to_a.length end
4   def to_utf8()
5     return to_a.map {|ch|
6       ch.char.to_utf8
7     }.join("")
8   end
9
10   def map_char(block = Proc.new)
11     return unless block_given?
12     return self.to_a.map {|ch| (block.call(ch)).to_s }.join("")
13   end
14
15   def map_char!(block = Proc.new)
16     return unless block_given?
17     self.replace(self.map_char {|ch| block.call(ch)})
18   end
19
20   def map_character(block = Proc.new)
21     return unless block_given?
22     return self.to_a.map {|ch| (block.call(ch.char)).to_s }.join("")
23   end
24
25   def map_character!(block = Proc.new)
26     return unless block_given?
27     self.replace(self.map_char {|ch| block.call(ch.char)})
28   end
29
30   def map_utf8() map_char {|ch| ch.char.map_utf8 } end
31   alias map_ucs map_utf8
32
33   def map_ucs_er() map_char {|ch| ch.char.map_ucs_er } end
34   def to_er() map_char {|ch| ch.char.to_er } end
35
36   #put\8aÖ\8cW\81A[]\8aÖ\8cW\82Í\97p\88Ó\82µ\82È\82¢\82±\82Æ\82É\82µ\82½\81B
37
38   def inspect_all() map_char {|ch| ch.char.inspect_all } end
39   def inspect_x()   map_char {|ch| ch.char.inspect_x   } end
40
41 # def to_euc()   map_char {|ch| ch.char.to_euc   } end
42   def map_euc()  map_char {|ch| ch.char.map_euc  } end
43 # def to_sjis()  map_char {|ch| ch.char.to_sjis  } end
44   def map_sjis() map_char {|ch| ch.char.map_sjis } end
45
46   def glyph_decompose() map_char {|ch| ch.char.glyph_decompose } end
47   def decompose() map_char {|ch| ch.char.decompose } end
48   def decompose!() self.replace(self.decompose); self; end
49
50   def nu_decompose_all(level=nil)
51     level = 0 if level.nil?
52     if 10 < level
53       p ["too many recursive", self] 
54       exit
55     end
56     de = self.decompose
57     return de.decompose_all(level+1) if de != self #\82È\82É\82©\95Ï\89»\82ª\82 \82Á\82½\82©\82ç\8dÄ\8bA
58     de #\82à\82¤\82±\82ê\88È\8fã\95Ï\89»\82Í\96³\82³\82»\82¤\82¾\82¼\82Æ\81B
59   end
60
61   def decompose_all() map_char {|ch| ch.char.decompose_all } end
62   def decompose_all!() self.replace(self.decompose_all); self; end
63
64   def find() #"\93ú\89_"\81¨"\93Ü"\82Æ\82©\82¢\82¤\8a´\82\82Ì\91\80\8dì
65     ar = []
66     length = char_length()
67     each_char {|ch|
68       char = ch.char
69       ar << char.ids_contained #\82»\82Ì\95\8e\9a\82ð\8aÜ\82ñ\82Å\82¢\82é\8a¿\8e\9a\82Ì\83\8a\83X\83g
70     }
71     h = Hash.new(0)
72     ar.each {|list|
73       next if list.nil?
74       list.each_char {|ch|
75         h[ch] += 1
76       }
77     }
78     str = ""
79     h.each {|k, v|
80       #      p [k, v]
81       if length == v #\91S\95\94\82É\8aç\82ð\8fo\82µ\82Ä\82¢\82½\82ç
82         str += k
83       end
84     }
85     #    p str
86     str
87   end
88
89   def compose()
90     db = CHISE::CodesysDB.instance
91     composed = db.get("ids", self)
92     return "" if composed.nil? #\82È\82©\82Á\82½\82æ\82Æ\81B
93     return "" if composed.char_length == 0 #\82È\82É\82²\82Æ?
94     return composed if composed.char_length == 1
95     composed.each_char {|ch|
96       char = ch.char
97       return ch if char.has_attribute? #\82Æ\82è\82 \82¦\82¸\8dÅ\8f\89\82É\82Ý\82Â\82©\82Á\82½\82à\82Ì\82ð\95Ô\82·\82Æ\82¢\82¤\83k\83\8b\82¢\8ed\97l
98     }
99     return "" #attribute\82ð\8e\9d\82Â\82à\82Ì\82ª\88ê\82Â\82à\96³\82©\82Á\82½\82ç\81A""\82É\82·\82é
100   end
101
102   def aggregate()
103     #self\82Å\82 \82é\95\8e\9a\97ñ\82ðIDS\82¾\82Æ\89¼\92è\82µ\81A\82»\82ê\82ð\8a®\91S\82Écompose\82µ\82«\82ç\82È\82¢\82Å\81A
104     #\82»\82Ì\95\94\95ª\8fW\8d\87\82¾\82¯\82ð\82Æ\82è\82¾\82µ\82Ä\81Acompose\89Â\94\\82Å\82 \82ê\82Î\82Å\82«\82é\82¾\82¯compose\82·\82é\81B
105     tree = CHISE::IDS_Tree.new(self)
106     return self if tree.depth <= 1 #sub_nodes\82ª\96³\82¢\8fê\8d\87\82Í\82±\82±\82Å\82³\82æ\82È\82ç
107     tree.sub_nodes.each {|node|
108       c = node.compose
109       next if c.nil? || c == ""
110       #      print "#{self}     #{node} #{c}\n"
111       #      p [self, node, c]
112       n = self.gsub(node, c)
113       return n.aggregate
114     }
115     return self #\82¨\82«\82©\82¦\82ç\82ê\82é\82à\82Ì\82ª\82Ü\82Á\82½\82­\82È\82©\82Á\82½\82ç\81A\8e©\95ª\82ð\82©\82¦\82·\81B
116   end
117 end