1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
4 require "chise/kanjilist"
5 require "chise/defkanji"
6 require "chise/graphviz"
9 class KanjiNetwork # 漢字間のネットワークを計算する
12 @formatter = GraphvizFormatter.new(self)
13 # @formatter = XSpringiesFormatter.new(self)
23 def make_network(list) #@h, @listに結果を入れていく。
29 def make_network_one(ch) #実質的にこれが本体。再帰で呼ばれる。
33 @list << ch #登場文字リストに追加
37 # if ids.char_length == 1 #分割できない文字は、リンクを探す。
38 if ids.is_a_utf8? #分割できない文字は、リンクを探す。
39 link = char.ids_represent if char.ids_represent
40 link = char.ids_link if char.ids_link
42 @h[ch] << link #親字に追加する。
47 ids.each_char {|idsch|
49 next if idschar.is_ids?
50 @h[ch] << idsch #親字に追加する。
52 make_network_one(idsch) #再帰する。
57 def nodes_and_edges() #これを二つのarrayで返す
60 @list.sort.uniq.each_with_index {|ch, index|
67 edgear << [nodeh[ch], nodeh[idsch]]
74 open(filename, "w"){|out| out.print to_s }
77 def to_s() @formatter.to_s; end
80 class GraphvizFormatter # Graphviz関係
81 def initialize(network)
85 def to_s() #Graphvizのフォーマット、dotフォーマットに変換する。
87 page.size = "5.5, 5.5"
96 @network.list.sort.uniq.each {|ch|
98 node = DotNode.new(char.to_i)
99 node.label = char.map_ucs_er #node.label = char.to_utf8
100 node.fontsize = (node.label =~ /^&#/) ? 12 : 6
101 #node.fontsize = (node.label =~ /^&#/) ? 24 : 6
102 node.shape = "plaintext"
103 node.fontname = "MS-Mincho" #呪われてるがしかたがない
104 #node.fontname = "Arial-Unicode-MS" #ダメ
108 @network.h.each {|ch, ar|
112 edge = DotEdge.new(char.to_i, idschar.to_i)
116 edge.color = "lightgray"
124 class XSpringiesFormatter
125 def initialize(network)
129 def to_s # #1.0 *** XSpringies data file
131 @network.list.sort.uniq.each {|ch|
134 label = char.map_ucs_er
135 str << "mass #{num}\n"
137 @network.h.each {|ch, ar|
143 str << "spng #{from} #{to}\n"