update doc.
[chise/ruby.git] / src / chise / makegraph.rb
1 #!/usr/bin/env ruby
2 # MakeGraph routine by eto 2003-0305
3 # $Id: makegraph.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.
7
8 $LOAD_PATH << '../../src' if $0 == __FILE__
9 require 'chise'
10 include CHISE
11 require 'chise/network'
12
13 module CHISE
14   class MakeGraph #======================================================================実務的な処理を引き受ける。
15     #GRAPHVIZ_CMD = 0 # CLI
16     GRAPHVIZ_CMD = 1 # OLE
17     GRAPHVIZ_TYPE = Graphviz::TWOPI
18     #GRAPHVIZ_TYPE = Graphviz::NEATO
19     START_GAKUNEN = 1
20     END_GAKUNEN = 6
21     
22     def initialize()
23       @kl = KanjiList.instance
24       @hn = KanjiNetwork.new
25     end
26     def go()
27       make_min
28       #make_html_all
29       #make_svg_all
30     end
31     def make_html_all()
32       open("gakunen.html", "w"){|out|
33         out.print %Q|<pre>\n|
34           (1..2).each {|type|
35           typestr = type.to_s
36           out.print %Q|#{typestr}グラフ\n|
37             (1..6).each {|gakunen|
38             out.print %Q|小学#{gakunen}年生 |
39               base = "han-#{type}-#{gakunen}-all"
40             out.print %Q|<a href="#{base}.svg">all</a> |
41               (0..3).each {|kubun|
42               base = "han-#{type}-#{gakunen}-#{kubun}"
43               out.print %Q|<a href="#{base}.svg">#{kubun}</a> |
44             }
45             out.print %Q|\n|
46           }
47         }
48         out.print %Q|</pre>\n|
49       }
50     end
51     def make_min
52       @hn.reset()
53       @hn.make_network(@kl.awase(0))
54       # @hn.make_network(@kl.awase(1))
55       @hn.out("min.dot") #途中状態を保存
56       #graphviz(Graphviz::NEATO, "min.dot", "min.svg")
57       graphviz(GRAPHVIZ_TYPE, "min.dot", "min.svg")
58     end
59     def make_svg_all()
60       (START_GAKUNEN..END_GAKUNEN).each {|gakunen|
61         (0..3).each {|kubun|
62           make_dot(gakunen, kubun)
63           (2..2).each {|type|
64             make_svg(type, gakunen, kubun)
65           }
66         }
67         make_dot(gakunen, nil)
68         (2..2).each {|type|
69           unless type == 2 && 5 <= gakunen
70             make_svg(type, gakunen, nil)
71           end
72         }
73       }
74     end
75     def gaku_name(gakunen, kubun=nil)
76       return "#{gakunen}nen-all" if kubun.nil?
77       return "#{gakunen}nen-#{kubun}"
78     end
79     def make_dot(gakunen, kubun=nil) #Graphvizのtype, 学年, 象形、指示などの区分
80       @hn.reset()
81       list = @kl.kyoiku(1..gakunen, kubun)
82       @hn.make_network(list)
83       dotf = "list-"+gaku_name(gakunen, kubun)+".dot"
84       @hn.out(dotf) #途中状態を保存する
85       p [dotf, "done"]
86     end
87     def make_svg(type, gakunen, kubun=nil) #Graphvizのtype, 学年, 象形、指示などの区分
88       g = gaku_name(gakunen, kubun)
89       dotf = "list-"+g+".dot"
90       svgf = "han-"+g+"-"+GraphvizCLI::NAMES[type]+".svg"
91       graphviz(type, dotf, svgf)
92     end
93     def dot2svg(dir)
94       Dir.chdir(dir)
95       Dir.glob("*.dot").each {|inf|
96         out = inf.sub(/.dot$/, '.svg')
97         type = 1 if inf =~ /^han-1/
98         type = 2 if inf =~ /^han-2/
99         graphviz(type, inf, out)
100       }
101     end
102     def graphviz(type, inf, out)
103       gv = GraphvizCLI.new() if GRAPHVIZ_CMD == 0
104       gv = GraphvizOLE.new() if GRAPHVIZ_CMD == 1
105       gv.type = type
106       gv.target = 'svg'
107       gv.in  = inf
108       gv.out = out
109       gv.generate()
110     end
111   end
112 end
113
114 if $0 == __FILE__
115   mg = MakeGraph.new
116   mg.go
117 end
118
119 #----------------------------------------------------------------------end.