2 # calc KanjiNetwork by eto 2003-0305
3 # $Id: network.rb,v 1.1 2003-11-10 08:11:47 eto Exp $
4 # Copyright (C) 2002-2003 Kouichirou Eto, All rights reserved.
5 # This is free software with ABSOLUTELY NO WARRANTY.
6 # You can redistribute it and/or modify it under the terms of the GNU GPL2.
8 $LOAD_PATH << '../../src' if $0 == __FILE__
11 require 'chise/kanjilist'
12 require 'chise/defkanji'
13 require 'chise/graphviz'
16 class KanjiNetwork #======================================================================漢字間のネットワークを計算する。
19 @formatter = GraphvizFormatter.new(self)
20 # @formatter = XSpringiesFormatter.new(self)
28 def make_network(list) #@h, @listに結果を入れていく。
34 def make_network_one(ch) #実質的にこれが本体。再帰で呼ばれる。
38 @list << ch #登場文字リストに追加
42 if ids.char_length == 1 #分割できない文字は、リンクを探す。
43 link = char.ids_represent if char.ids_represent
44 link = char.ids_link if char.ids_link
46 @h[ch] << link #親字に追加する。
51 ids.each_char {|idsch|
53 next if idschar.is_ids?
54 @h[ch] << idsch #親字に追加する。
56 make_network_one(idsch) #再帰する。
61 def nodes_and_edges() #これを二つのarrayで返す
64 @list.sort.uniq.each_with_index {|ch, index|
71 edgear << [nodeh[ch], nodeh[idsch]]
78 open(filename, "w"){|out| out.print to_s }
80 def to_s() @formatter.to_s; end
83 class GraphvizFormatter #====================================================================== Graphviz関係
84 def initialize(network)
87 def to_s() #Graphvizのフォーマット、dotフォーマットに変換する。
89 page.size = "5.5, 5.5"
98 @network.list.sort.uniq.each {|ch|
100 node = DotNode.new(char.to_i)
101 node.label = char.map_ucs_er #node.label = char.to_utf8
102 node.fontsize = (node.label =~ /^&#/) ? 12 : 6
103 #node.fontsize = (node.label =~ /^&#/) ? 24 : 6
104 node.shape = "plaintext"
105 node.fontname = "MS-Mincho" #呪われてるがしかたがない
106 #node.fontname = "Arial-Unicode-MS" #ダメ
110 @network.h.each {|ch, ar|
114 edge = DotEdge.new(char.to_i, idschar.to_i)
118 edge.color = "lightgray"
126 class XSpringiesFormatter #======================================================================
127 def initialize(network)
130 def to_s # #1.0 *** XSpringies data file
132 @network.list.sort.uniq.each {|ch|
135 label = char.map_ucs_er
136 str << "mass #{num}\n"
138 @network.h.each {|ch, ar|
144 str << "spng #{from} #{to}\n"
152 #----------------------------------------------------------------------end.