ndc_tree 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :rubygems
2
+
3
+ gem 'rubytree', '>=0.8.1'
4
+ gem 'ruby-graphviz'
5
+
6
+ group :development do
7
+ gem 'jeweler'
8
+ gem 'rake'
9
+ gem 'rspec'
10
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Mao Tsunekawa
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ = ndc_tree
2
+
3
+ link://output.gif
4
+
5
+ ndc_treeはNDC(日本十進分類法)のコードを木構造データとして集計して視覚化するツールです。
6
+
7
+ ndc_tree is a ruby gem aggregating and visualizing a list of NDC(Nippon Decimal Classification).
8
+
9
+ == Requirements
10
+
11
+ - GraphViz
12
+ - rubytree
13
+ - ruby-graphviz
14
+
15
+ == How to use
16
+
17
+ NdcTreeを作成するには、NdcTree << メソッドでNDCコードのリストを入力します。
18
+
19
+ If you want to generate NDC Tree, apply NdcTree << and input a list of NDC.
20
+
21
+ require 'ndc_tree'
22
+
23
+ tree = NdcTree << %w{
24
+ 913.6
25
+ 411
26
+ 007
27
+ 913.6
28
+ 914
29
+ 914.6
30
+ 913.6
31
+ 913.6
32
+ 913.6
33
+ 913.6
34
+ }
35
+
36
+ テキスト形式でNDCツリーを表示することができます。
37
+
38
+ With print_tree method, you can show text-based NDC Tree.
39
+
40
+ tree.print_tree
41
+
42
+ NDCツリーはGraphVizを使って画像形式で出力することができます。
43
+
44
+ ndc_tree can generate a image of NDC Tree with GraphViz.
45
+
46
+ tree.print_image(:gif=>"output.gif")
47
+
48
+ もし画像のレイアウトを変更したい場合は、print_imageを実行する際にブロックを与えてください。
49
+
50
+ If you want to change design of tree image, use block.
51
+
52
+ tree.print_image(:gif=>"output.gif") do |g|
53
+ g.node[:style] = "filled"
54
+ g.node[:shape] = "note"
55
+ g.node[:color] = "black"
56
+ g.node[:fontcolor] = "white"
57
+ end
58
+
59
+ 上のコード中の g は、GraphVizクラスのインスタンスです。
60
+
61
+ In a above code, g is a instance of GraphViz object.
62
+
63
+ == ライセンス ( Licence )
64
+
65
+ MITライセンスです。
66
+
67
+ This is MIT Licence.
68
+
69
+ == Copyright
70
+
71
+ Copyright (c) 2012 Mao Tsunekawa. See LICENSE.txt for
72
+ further details.
73
+
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.version = File.read('VERSION')
18
+ gem.name = "ndc_tree"
19
+ gem.homepage = "http://github.com/tsunekawa/ndc_tree"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{NdcTree is a library outputs a list of NDC(Nippon Decimal Classification) as a tree-structured data}
22
+ gem.description = %Q{NdcTree is a library outputs a list of NDC(Nippon Decimal Classification) as a tree-structured data.}
23
+ gem.email = "tsunekaw@slis.tsukuba.ac.jp"
24
+ gem.authors = ["Mao Tsunekawa"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rdoc/task'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "ndc_tree #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,26 @@
1
+ # -*- coding:utf-8 -*-
2
+ $:.unshift File.join(File.expand_path(File.dirname(__FILE__)), %w{ .. lib })
3
+
4
+ require 'ndc_tree'
5
+
6
+ tree = NdcTree << %w{
7
+ 913.6
8
+ 411
9
+ 007
10
+ 913.6
11
+ 914
12
+ 914.6
13
+ 913.6
14
+ 913.6
15
+ 913.6
16
+ 913.6
17
+ }
18
+
19
+ filepath = File.join(File.dirname(__FILE__), %w{ .. data })
20
+ tree.print_image(:gif=>File.join(filepath,%w{output.gif})) do |g|
21
+ g.node[:style] = "filled"
22
+ g.node[:shape] = "note"
23
+ g.node[:color] = "black"
24
+ g.node[:fontcolor] = "white"
25
+ end
26
+
@@ -0,0 +1,18 @@
1
+ # -*- coding:utf-8 -*-
2
+ $:.unshift File.expand_path(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'tree'
6
+ require 'graphviz'
7
+
8
+ module NdcTree
9
+ extend self
10
+ VERSION = File.read(File.join(File.dirname(__FILE__),%w{ .. VERSION}))
11
+
12
+ #
13
+ def <<(ndcs)
14
+ Node.new << ndcs
15
+ end
16
+
17
+ autoload :Node, "ndc_tree/node.rb"
18
+ end
@@ -0,0 +1,178 @@
1
+ # -*- coding:utf-8 -*-
2
+
3
+ class NdcTree::Node < Tree::TreeNode
4
+ class InputNdcError < Exception;end
5
+
6
+ attr_accessor :weight
7
+
8
+ ##
9
+ # converts this tree to array of nodes
10
+
11
+ alias_method :nodes, :to_a
12
+
13
+ ##
14
+ # Returns:: a instance object of NdcTree::Node
15
+ def initialize(name="root",content={})
16
+ @weight=1
17
+ super(name, content)
18
+ end
19
+
20
+ ##
21
+ #
22
+ # Returns:: a node has name same as a given key
23
+
24
+ def [](key)
25
+ nodes.find {|node| node.name==key }
26
+ end
27
+
28
+ ##
29
+ # this method searches nodes have name matching a given expression
30
+ #
31
+ # Returns :: a list of NdcTree::Node objects.
32
+
33
+ def search(exp)
34
+ nodes.find_all {|node| node.name =~ exp }
35
+ end
36
+
37
+ ##
38
+ # this method sorts nodes at each layers by ndc codes ( it's bang method )
39
+ # Returns :: a instance object of NdcTree::Node
40
+
41
+ def sort!
42
+ unless self.is_leaf? then
43
+ @children.sort!.map{|node| node.sort!}
44
+ end
45
+ self
46
+ end
47
+
48
+ ##
49
+ # sorts nodes at each layers by ndc codes ( it is not bang method )
50
+ #
51
+ # Returns :: a instance object of NdcTree::Node
52
+
53
+ def sort
54
+ node = self.dup
55
+ node.sort!
56
+ end
57
+
58
+ ##
59
+ # This method inserts a node has name same as given value as a child node.
60
+ # If given value is instance of Array, this method repeats inserting each element of value.
61
+ #
62
+ # Returns :: self instance
63
+
64
+ def <<(value)
65
+ case value.class.name.to_sym
66
+ when :String
67
+ add_ndc(value)
68
+ when :Array
69
+ value.flatten.each{|v| add_ndc(v) }
70
+ when :Node
71
+ super
72
+ else
73
+ raise TypeError,"#{value} is not valid"
74
+ end
75
+ self
76
+ end
77
+
78
+ ##
79
+ # This method loads dump of a tree formatted marshal object.
80
+ #
81
+ # Return :: self instance
82
+
83
+ def marshal_load(dumped_tree_array)
84
+ nodes = { }
85
+ dumped_tree_array.each do |node_hash|
86
+ name = node_hash[:name]
87
+ parent_name = node_hash[:parent]
88
+ content = Marshal.load(node_hash[:content])
89
+
90
+ if parent_name then
91
+ nodes[name] = current_node = self.class.new(name, content)
92
+ nodes[parent_name].add current_node
93
+ else
94
+ # This is the root node, hence initialize self.
95
+ initialize(name, content)
96
+
97
+ nodes[name] = self # Add self to the list of nodes
98
+ end
99
+ end
100
+ end
101
+
102
+ # This method output a image file with GraphViz.
103
+ #
104
+ # Returns :: true
105
+ # Raise :: when given file path is wrong
106
+ def print_image(opts={:gif=>"ndc_tree.gif"},&block)
107
+ sort!
108
+
109
+ g = GraphViz::new("G")
110
+ set_default_style
111
+ to_graphviz(g,opts)
112
+
113
+ yield g if block_given?
114
+
115
+ g.output(opts)
116
+ true
117
+ end
118
+
119
+ protected
120
+
121
+ def add_ndc (str) #nodoc#
122
+ unless /^[0-9]{3}(\.[0-9]+){0,1}$/ =~ str
123
+ raise InputNdcError, "入力された文字列(#{str})はNDCコードではありません"
124
+ end
125
+
126
+ labels=Array.new
127
+ name = ""
128
+ node = self
129
+ str.split(//).each do |n|
130
+ name+=n
131
+ asta= "*"*([3-name.size,0].max)
132
+ next if n=="."
133
+ label = name+asta
134
+ if node[label].nil? then
135
+ unless node.is_leaf? then
136
+ index = -1
137
+ else
138
+ index = node.children.inject(-1) {|count,n|
139
+ (n.name > label) ? count : count+1
140
+ }
141
+ end
142
+ node.add(self.class.new(label),index)
143
+ else
144
+ node[label].weight+=1
145
+ end
146
+ node = node[label]
147
+ end
148
+
149
+ self
150
+ end
151
+
152
+ def set_default_style
153
+ g = GraphViz::new("G")
154
+ g.node[:style] = "filled"
155
+ g.node[:shape] = "note"
156
+ g.node[:color] = "black"
157
+ g.node[:fontcolor] = "white"
158
+ end
159
+
160
+ def to_graphviz(g,opts={})
161
+ case weight
162
+ when 0..1;color="black";
163
+ when 2..4;color="blue";
164
+ else;color="red";
165
+ end
166
+
167
+ gnode = g.add_nodes(name+"(#{weight})")
168
+ gnode[:color] = color
169
+
170
+ unless is_leaf? then
171
+ children.each do |node|
172
+ g.add_edges(gnode, node.to_graphviz(g,opts))
173
+ end
174
+ end
175
+ gnode
176
+ end
177
+
178
+ end
@@ -0,0 +1,2 @@
1
+ #-*- coding:utf-8 -*-
2
+ NdcTree::VERSION = "0.1.0"
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "ndc_tree"
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mao Tsunekawa"]
12
+ s.date = "2012-02-12"
13
+ s.description = "NdcTree is a library outputs a list of NDC(Nippon Decimal Classification) as a tree-structured data."
14
+ s.email = "tsunekaw@slis.tsukuba.ac.jp"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "LICENSE.txt",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "example/graph.rb",
26
+ "lib/ndc_tree.rb",
27
+ "lib/ndc_tree/node.rb",
28
+ "lib/ndc_tree/version.rb",
29
+ "ndc_tree.gemspec",
30
+ "spec/insert_spec.rb",
31
+ "spec/ndc_tree_spec.rb",
32
+ "spec/search_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = "http://github.com/tsunekawa/ndc_tree"
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = "1.8.10"
39
+ s.summary = "NdcTree is a library outputs a list of NDC(Nippon Decimal Classification) as a tree-structured data"
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<rubytree>, [">= 0.8.1"])
46
+ s.add_runtime_dependency(%q<ruby-graphviz>, [">= 0"])
47
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
48
+ s.add_development_dependency(%q<rake>, [">= 0"])
49
+ s.add_development_dependency(%q<rspec>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<rubytree>, [">= 0.8.1"])
52
+ s.add_dependency(%q<ruby-graphviz>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_dependency(%q<rake>, [">= 0"])
55
+ s.add_dependency(%q<rspec>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rubytree>, [">= 0.8.1"])
59
+ s.add_dependency(%q<ruby-graphviz>, [">= 0"])
60
+ s.add_dependency(%q<jeweler>, [">= 0"])
61
+ s.add_dependency(%q<rake>, [">= 0"])
62
+ s.add_dependency(%q<rspec>, [">= 0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "NdcTree <<" do
4
+
5
+ subject { @tree = NdcTree << [ "913.6" ] }
6
+
7
+ it { should be_instance_of NdcTree::Node }
8
+
9
+ its(:name) { should == "root" }
10
+ its(:nodes) {
11
+ should be_instance_of Array
12
+ should have(5).items
13
+ }
14
+ its(:weight) { should >= 1 }
15
+ its(:children) {
16
+ should have(1).items
17
+
18
+ corrects = [ "9**", "91*", "913", "913.6" ]
19
+ corrects.inject(subject.first) do |node, correct|
20
+ node.name.should == correct
21
+ if node.is_leaf? then
22
+ next node
23
+ else
24
+ node.children.should have(1).items
25
+ next node.children.first
26
+ end
27
+ end
28
+ }
29
+ its(:content) {
30
+ should be_instance_of Hash
31
+ should have(0).items
32
+ }
33
+
34
+ end
@@ -0,0 +1,36 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "NdcTree::Node" do
4
+
5
+ before do
6
+ @tree = NdcTree::Node.new
7
+ end
8
+
9
+ it "should initialize" do
10
+ @tree.should be_instance_of NdcTree::Node
11
+ end
12
+
13
+ it "should has name" do
14
+ @tree.name.should be_instance_of String
15
+ end
16
+
17
+ it "should not has accessor of name" do
18
+ (defined? @tree.name="newname").should be_nil
19
+ end
20
+
21
+ it "should has weight" do
22
+ @tree.weight.should > 0
23
+ end
24
+
25
+ it "should has content" do
26
+ @tree.content.should be_instance_of Hash
27
+ end
28
+
29
+ it "should has accessor of weight" do
30
+ @tree.weight = 1
31
+ @tree.weight.should == 1
32
+ @tree.weight+=1
33
+ @tree.weight.should == 2
34
+ end
35
+
36
+ end
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "NdcTree::Node searching" do
4
+ subject { NdcTree << %w{ 913.6 913.6 914 400 007 515 } }
5
+
6
+ it {
7
+ result = subject["913.6"]
8
+ result.should be_instance_of NdcTree::Node
9
+ result.name.should == "913.6"
10
+ result.weight.should == 2
11
+ }
12
+
13
+ it {
14
+ result = subject["611"]
15
+ result.should be_nil
16
+ }
17
+
18
+ it {
19
+ result = subject.search(/^9/)
20
+ result.should be_instance_of Array
21
+ result.should have(5).items
22
+ }
23
+
24
+ it {
25
+ result = subject.search(/^1/)
26
+ result.should be_instance_of Array
27
+ result.should have(0).items
28
+ }
29
+
30
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'ndc_tree'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ndc_tree
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mao Tsunekawa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rubytree
16
+ requirement: &80559200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *80559200
25
+ - !ruby/object:Gem::Dependency
26
+ name: ruby-graphviz
27
+ requirement: &80557600 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *80557600
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &80556190 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *80556190
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &80555280 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *80555280
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &80569350 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *80569350
69
+ description: NdcTree is a library outputs a list of NDC(Nippon Decimal Classification)
70
+ as a tree-structured data.
71
+ email: tsunekaw@slis.tsukuba.ac.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files:
75
+ - LICENSE.txt
76
+ - README.rdoc
77
+ files:
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.rdoc
81
+ - Rakefile
82
+ - VERSION
83
+ - example/graph.rb
84
+ - lib/ndc_tree.rb
85
+ - lib/ndc_tree/node.rb
86
+ - lib/ndc_tree/version.rb
87
+ - ndc_tree.gemspec
88
+ - spec/insert_spec.rb
89
+ - spec/ndc_tree_spec.rb
90
+ - spec/search_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: http://github.com/tsunekawa/ndc_tree
93
+ licenses:
94
+ - MIT
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 48156979
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 1.8.10
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: NdcTree is a library outputs a list of NDC(Nippon Decimal Classification)
120
+ as a tree-structured data
121
+ test_files: []