decisiontree 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +7 -1
- data/Manifest.txt +24 -18
- data/Rakefile +82 -12
- data/examples/simple.rb +28 -0
- data/lib/decisiontree/id3_tree.rb +52 -36
- data/lib/decisiontree/version.rb +1 -1
- data/scripts/txt2html +67 -0
- data/website/index.html +11 -0
- data/website/index.txt +38 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.rhtml +48 -0
- metadata +26 -9
data/CHANGELOG.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
1
|
0.1.0 - Apr. 04/07
|
2
2
|
* ID3 algorithms for continuous and discrete cases
|
3
|
-
* Graphviz component to visualize the learned tree
|
3
|
+
* Graphviz component to visualize the learned tree
|
4
|
+
|
5
|
+
0.2.0 - Jul. 07/07
|
6
|
+
* Modified and improved by Jose Ignacio (joseignacio.fernandez@gmail.com)
|
7
|
+
* Added support for multiple, and symbolic outputs and graphing of continuos trees.
|
8
|
+
* Modified to return the default value when no branches are suitable for the input.
|
9
|
+
* Refactored entropy code.
|
data/Manifest.txt
CHANGED
@@ -1,18 +1,24 @@
|
|
1
|
-
CHANGELOG.txt
|
2
|
-
History.txt
|
3
|
-
Manifest.txt
|
4
|
-
README.txt
|
5
|
-
Rakefile
|
6
|
-
|
7
|
-
lib/decisiontree.rb
|
8
|
-
lib/decisiontree/
|
9
|
-
|
10
|
-
examples/
|
11
|
-
examples/
|
12
|
-
examples/data/continuous-test.txt
|
13
|
-
examples/data/discrete-test.txt
|
14
|
-
examples/data/continuous-training.txt
|
15
|
-
examples/data/discrete-training.txt
|
16
|
-
setup.rb
|
17
|
-
test/test_helper.rb
|
18
|
-
test/test_decisiontree.rb
|
1
|
+
CHANGELOG.txt
|
2
|
+
History.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
lib/decisiontree.rb
|
7
|
+
lib/decisiontree/version.rb
|
8
|
+
lib/decisiontree/id3_tree.rb
|
9
|
+
examples/continuous-id3.rb
|
10
|
+
examples/discrete-id3.rb
|
11
|
+
examples/simple.rb
|
12
|
+
examples/data/continuous-test.txt
|
13
|
+
examples/data/discrete-test.txt
|
14
|
+
examples/data/continuous-training.txt
|
15
|
+
examples/data/discrete-training.txt
|
16
|
+
setup.rb
|
17
|
+
test/test_helper.rb
|
18
|
+
test/test_decisiontree.rb
|
19
|
+
scripts/txt2html
|
20
|
+
website/index.html
|
21
|
+
website/index.txt
|
22
|
+
website/javascripts/rounded_corners_lite.inc.js
|
23
|
+
website/stylesheets/screen.css
|
24
|
+
website/template.rhtml
|
data/Rakefile
CHANGED
@@ -8,21 +8,44 @@ require 'rake/rdoctask'
|
|
8
8
|
require 'rake/contrib/rubyforgepublisher'
|
9
9
|
require 'fileutils'
|
10
10
|
require 'hoe'
|
11
|
+
|
11
12
|
include FileUtils
|
12
13
|
require File.join(File.dirname(__FILE__), 'lib', 'decisiontree', 'version')
|
13
14
|
|
14
|
-
AUTHOR =
|
15
|
+
AUTHOR = 'Ilya Grigorik' # can also be an array of Authors
|
15
16
|
EMAIL = "ilya <at> fortehost.com"
|
16
17
|
DESCRIPTION = "ID3-based implementation of the M.L. Decision Tree algorithm"
|
17
|
-
GEM_NAME =
|
18
|
-
|
18
|
+
GEM_NAME = 'decisiontree' # what ppl will type to install your gem
|
19
|
+
|
20
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
21
|
+
@config = nil
|
22
|
+
def rubyforge_username
|
23
|
+
unless @config
|
24
|
+
begin
|
25
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
26
|
+
rescue
|
27
|
+
puts <<-EOS
|
28
|
+
ERROR: No rubyforge config file found: #{@config_file}"
|
29
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
30
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
31
|
+
EOS
|
32
|
+
exit
|
33
|
+
end
|
34
|
+
end
|
35
|
+
@rubyforge_username ||= @config["username"]
|
36
|
+
end
|
37
|
+
|
38
|
+
RUBYFORGE_PROJECT = 'decisiontree' # The unix name for your project
|
19
39
|
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
40
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
20
41
|
|
21
42
|
NAME = "decisiontree"
|
22
|
-
REV = nil
|
23
|
-
|
24
|
-
|
25
|
-
|
43
|
+
REV = nil
|
44
|
+
# UNCOMMENT IF REQUIRED:
|
45
|
+
# REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
|
46
|
+
VERS = DecisionTree::VERSION::STRING + (REV ? ".#{REV}" : "")
|
47
|
+
CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
|
48
|
+
RDOC_OPTS = ['--quiet', '--title', 'decisiontree documentation',
|
26
49
|
"--opname", "index.html",
|
27
50
|
"--line-numbers",
|
28
51
|
"--main", "README",
|
@@ -43,11 +66,58 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
|
43
66
|
p.summary = DESCRIPTION
|
44
67
|
p.url = HOMEPATH
|
45
68
|
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
46
|
-
p.test_globs = ["test
|
47
|
-
p.clean_globs
|
69
|
+
p.test_globs = ["test/**/test_*.rb"]
|
70
|
+
p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
|
48
71
|
|
49
72
|
# == Optional
|
50
|
-
|
51
|
-
#p.extra_deps
|
52
|
-
#p.spec_extras
|
73
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
74
|
+
#p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
|
75
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
76
|
+
end
|
77
|
+
|
78
|
+
CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
|
79
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
80
|
+
hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
81
|
+
|
82
|
+
desc 'Generate website files'
|
83
|
+
task :website_generate do
|
84
|
+
Dir['website/**/*.txt'].each do |txt|
|
85
|
+
sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
desc 'Upload website files to rubyforge'
|
90
|
+
task :website_upload do
|
91
|
+
host = "#{rubyforge_username}@rubyforge.org"
|
92
|
+
remote_dir = "/var/www/gforge-projects/#{PATH}/"
|
93
|
+
local_dir = 'website'
|
94
|
+
sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
|
53
95
|
end
|
96
|
+
|
97
|
+
desc 'Generate and upload website files'
|
98
|
+
task :website => [:website_generate, :website_upload, :publish_docs]
|
99
|
+
|
100
|
+
desc 'Release the website and new gem version'
|
101
|
+
task :deploy => [:check_version, :website, :release] do
|
102
|
+
puts "Remember to create SVN tag:"
|
103
|
+
puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
|
104
|
+
"svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
|
105
|
+
puts "Suggested comment:"
|
106
|
+
puts "Tagging release #{CHANGES}"
|
107
|
+
end
|
108
|
+
|
109
|
+
desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
|
110
|
+
task :local_deploy => [:website_generate, :install_gem]
|
111
|
+
|
112
|
+
task :check_version do
|
113
|
+
unless ENV['VERSION']
|
114
|
+
puts 'Must pass a VERSION=x.y.z release version'
|
115
|
+
exit
|
116
|
+
end
|
117
|
+
unless ENV['VERSION'] == VERS
|
118
|
+
puts "Please update your version.rb to match the release version, currently #{VERS}"
|
119
|
+
exit
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
|
data/examples/simple.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'decisiontree'
|
5
|
+
|
6
|
+
attributes = ['Temperature']
|
7
|
+
training = [
|
8
|
+
[36.6, 'healthy'],
|
9
|
+
[37, 'sick'],
|
10
|
+
[38, 'sick'],
|
11
|
+
[36.7, 'healthy'],
|
12
|
+
[40, 'sick'],
|
13
|
+
[50, 'really sick'],
|
14
|
+
]
|
15
|
+
|
16
|
+
# Instantiate the tree, and train it based on the data (set default to '1')
|
17
|
+
dec_tree = DecisionTree::ID3Tree.new(attributes, training, 'sick', :continuous)
|
18
|
+
dec_tree.train
|
19
|
+
|
20
|
+
test = [37, 'sick']
|
21
|
+
|
22
|
+
decision = dec_tree.predict(test)
|
23
|
+
puts "Predicted: #{decision} ... True decision: #{test.last}";
|
24
|
+
|
25
|
+
# Graph the tree, save to 'tree.png'
|
26
|
+
dec_tree.graph("tree")
|
27
|
+
|
28
|
+
|
@@ -1,6 +1,7 @@
|
|
1
1
|
#The MIT License
|
2
2
|
|
3
3
|
###Copyright (c) 2007 Ilya Grigorik <ilya AT fortehost DOT com>
|
4
|
+
###Modifed at 2007 by José Ignacio Fernández <joseignacio.fernandez AT gmail DOT com>
|
4
5
|
|
5
6
|
begin;
|
6
7
|
require 'graph/graphviz_dot'
|
@@ -10,8 +11,21 @@ end
|
|
10
11
|
|
11
12
|
class Array
|
12
13
|
def classification; collect { |v| v.last }; end
|
13
|
-
|
14
|
-
|
14
|
+
|
15
|
+
# calculate Information entropy
|
16
|
+
def entropy
|
17
|
+
return 0 if empty?
|
18
|
+
|
19
|
+
info = {}
|
20
|
+
total = 0
|
21
|
+
each {|i| info[i] = !info[i] ? 1 : (info[i] + 1); total += 1}
|
22
|
+
|
23
|
+
result = 0
|
24
|
+
info.each do |symbol, count|
|
25
|
+
result += -count.to_f/total*Math.log(count.to_f/total)/Math.log(2.0) if (count > 0)
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
15
29
|
end
|
16
30
|
|
17
31
|
module DecisionTree
|
@@ -30,6 +44,7 @@ module DecisionTree
|
|
30
44
|
end
|
31
45
|
|
32
46
|
return default if data.empty?
|
47
|
+
|
33
48
|
# return classification if all examples have the same classification
|
34
49
|
return data.first.last if data.classification.uniq.size == 1
|
35
50
|
|
@@ -38,7 +53,7 @@ module DecisionTree
|
|
38
53
|
max = performance.max { |a,b| a[0] <=> b[0] }
|
39
54
|
best = Node.new(attributes[performance.index(max)], max[1], max[0])
|
40
55
|
@used.has_key?(best.attribute) ? @used[best.attribute] += [best.threshold] : @used[best.attribute] = [best.threshold]
|
41
|
-
tree, l = {best => {}}, ['
|
56
|
+
tree, l = {best => {}}, ['>=', '<']
|
42
57
|
|
43
58
|
case @type
|
44
59
|
when :continuous
|
@@ -67,7 +82,7 @@ module DecisionTree
|
|
67
82
|
pos = (sp[0].size).to_f / data.size
|
68
83
|
neg = (sp[1].size).to_f / data.size
|
69
84
|
|
70
|
-
[
|
85
|
+
[data.classification.entropy - pos*sp[0].classification.entropy - neg*sp[1].classification.entropy, threshold]
|
71
86
|
}.max { |a,b| a[0] <=> b[0] }
|
72
87
|
end
|
73
88
|
|
@@ -75,29 +90,15 @@ module DecisionTree
|
|
75
90
|
def id3_discrete(data, attributes, attribute)
|
76
91
|
values = data.collect { |d| d[attributes.index(attribute)] }.uniq.sort
|
77
92
|
partitions = values.collect { |val| data.select { |d| d[attributes.index(attribute)] == val } }
|
78
|
-
remainder = partitions.collect {|p| (p.size.to_f / data.size) *
|
93
|
+
remainder = partitions.collect {|p| (p.size.to_f / data.size) * p.classification.entropy}.inject(0) {|i,s| s+=i }
|
79
94
|
|
80
|
-
[
|
95
|
+
[data.classification.entropy - remainder, attributes.index(attribute)]
|
81
96
|
end
|
82
97
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
# calculate Information based on probabilities
|
87
|
-
def entropy(p, n)
|
88
|
-
p = 0 if p.nan?
|
89
|
-
n = 0 if n.nan?
|
90
|
-
|
91
|
-
if(n < 0.00000001 and p < 0.00000001); return 0
|
92
|
-
elsif (p < 0.00000001); return - n.to_f/(p+n)*Math.log(n.to_f/(p+n))/Math.log(2.0)
|
93
|
-
elsif (n < 0.00000001); return - p.to_f/(p+n)*Math.log(p.to_f/(p+n))/Math.log(2.0)
|
94
|
-
end
|
95
|
-
|
96
|
-
return (- p.to_f/(p+n)) * Math.log(p.to_f/(p+n))/Math.log(2.0) + (- n.to_f/(p+n)) * Math.log(n.to_f/(p+n))/Math.log(2.0)
|
98
|
+
def predict(test)
|
99
|
+
@type == :discrete ? descend_discrete(@tree, test) : descend_continuous(@tree, test)
|
97
100
|
end
|
98
101
|
|
99
|
-
def predict(test); @type == :discrete ? descend_discrete(@tree, test) : descend_continuous(@tree,test); end
|
100
|
-
|
101
102
|
def graph(filename)
|
102
103
|
dgp = DotGraphPrinter.new(build_tree)
|
103
104
|
dgp.write_to_file("#{filename}.png", "png")
|
@@ -106,26 +107,41 @@ module DecisionTree
|
|
106
107
|
private
|
107
108
|
def descend_continuous(tree, test)
|
108
109
|
attr = tree.to_a.first
|
109
|
-
return
|
110
|
-
return attr[1]['
|
111
|
-
return
|
112
|
-
return descend_continuous(attr[1]['
|
110
|
+
return @default if !attr
|
111
|
+
return attr[1]['>='] if !attr[1]['>='].is_a?(Hash) and test[@attributes.index(attr.first.attribute)] >= attr.first.threshold
|
112
|
+
return attr[1]['<'] if !attr[1]['<'].is_a?(Hash) and test[@attributes.index(attr.first.attribute)] < attr.first.threshold
|
113
|
+
return descend_continuous(attr[1]['>='],test) if test[@attributes.index(attr.first.attribute)] >= attr.first.threshold
|
114
|
+
return descend_continuous(attr[1]['<'],test) if test[@attributes.index(attr.first.attribute)] < attr.first.threshold
|
113
115
|
end
|
114
116
|
|
115
|
-
def descend_discrete(tree,test)
|
116
|
-
attr = tree.to_a.first
|
117
|
-
return
|
117
|
+
def descend_discrete(tree, test)
|
118
|
+
attr = tree.to_a.first
|
119
|
+
return @default if !attr
|
120
|
+
return attr[1][test[@attributes.index(attr[0].attribute)]] if !attr[1][test[@attributes.index(attr[0].attribute)]].is_a?(Hash)
|
118
121
|
return descend_discrete(attr[1][test[@attributes.index(attr[0].attribute)]],test)
|
119
122
|
end
|
120
123
|
|
121
|
-
def build_tree(tree = @tree
|
122
|
-
return [
|
123
|
-
|
124
|
+
def build_tree(tree = @tree)
|
125
|
+
return [] unless tree.is_a?(Hash)
|
126
|
+
return [["Always", @default]] if tree.empty?
|
127
|
+
|
124
128
|
attr = tree.to_a.first
|
125
|
-
|
126
|
-
links =
|
127
|
-
|
128
|
-
|
129
|
+
|
130
|
+
links = attr[1].keys.collect do |key|
|
131
|
+
parent_text = "#{attr[0].attribute}\n(#{attr[0].object_id})"
|
132
|
+
if attr[1][key].is_a?(Hash) then
|
133
|
+
child = attr[1][key].to_a.first[0]
|
134
|
+
child_text = "#{child.attribute}\n(#{child.object_id})"
|
135
|
+
else
|
136
|
+
child = attr[1][key]
|
137
|
+
child_text = "#{child}\n(#{child.to_s.object_id})"
|
138
|
+
end
|
139
|
+
label_text = "#{key} #{@type == :continuous ? attr[0].threshold : ""}"
|
140
|
+
|
141
|
+
[parent_text, child_text, label_text]
|
142
|
+
end
|
143
|
+
attr[1].keys.each { |key| links += build_tree(attr[1][key]) }
|
144
|
+
|
129
145
|
return links
|
130
146
|
end
|
131
147
|
end
|
data/lib/decisiontree/version.rb
CHANGED
data/scripts/txt2html
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'redcloth'
|
5
|
+
require 'syntax/convertors/html'
|
6
|
+
require 'erb'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/decisiontree/version.rb'
|
8
|
+
|
9
|
+
version = Decisiontree::VERSION::STRING
|
10
|
+
download = 'http://rubyforge.org/projects/decisiontree'
|
11
|
+
|
12
|
+
class Fixnum
|
13
|
+
def ordinal
|
14
|
+
# teens
|
15
|
+
return 'th' if (10..19).include?(self % 100)
|
16
|
+
# others
|
17
|
+
case self % 10
|
18
|
+
when 1: return 'st'
|
19
|
+
when 2: return 'nd'
|
20
|
+
when 3: return 'rd'
|
21
|
+
else return 'th'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Time
|
27
|
+
def pretty
|
28
|
+
return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def convert_syntax(syntax, source)
|
33
|
+
return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
|
34
|
+
end
|
35
|
+
|
36
|
+
if ARGV.length >= 1
|
37
|
+
src, template = ARGV
|
38
|
+
template ||= File.dirname(__FILE__) + '/../website/template.rhtml'
|
39
|
+
|
40
|
+
else
|
41
|
+
puts("Usage: #{File.split($0).last} source.txt [template.rhtml] > output.html")
|
42
|
+
exit!
|
43
|
+
end
|
44
|
+
|
45
|
+
template = ERB.new(File.open(template).read)
|
46
|
+
|
47
|
+
title = nil
|
48
|
+
body = nil
|
49
|
+
File.open(src) do |fsrc|
|
50
|
+
title_text = fsrc.readline
|
51
|
+
body_text = fsrc.read
|
52
|
+
syntax_items = []
|
53
|
+
body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</>!m){
|
54
|
+
ident = syntax_items.length
|
55
|
+
element, syntax, source = $1, $2, $3
|
56
|
+
syntax_items << "<#{element} class='syntax'>#{convert_syntax(syntax, source)}</#{element}>"
|
57
|
+
"syntax-temp-#{ident}"
|
58
|
+
}
|
59
|
+
title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
|
60
|
+
body = RedCloth.new(body_text).to_html
|
61
|
+
body.gsub!(%r!(?:<pre><code>)?syntax-temp-(d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
|
62
|
+
end
|
63
|
+
stat = File.stat(src)
|
64
|
+
created = stat.ctime
|
65
|
+
modified = stat.mtime
|
66
|
+
|
67
|
+
$stdout << template.result(binding)
|
data/website/index.html
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
<html>
|
2
|
+
<head>
|
3
|
+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
4
|
+
<title>decisiontree</title>
|
5
|
+
|
6
|
+
</head>
|
7
|
+
<body id="body">
|
8
|
+
<p>This page has not yet been created for RubyGem <code>decisiontree</code></p>
|
9
|
+
<p>To the developer: To generate it, update website/index.txt and run the rake task <code>website</code> to generate this <code>index.html</code> file.</p>
|
10
|
+
</body>
|
11
|
+
</html>
|
data/website/index.txt
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
h1. decisiontree
|
2
|
+
|
3
|
+
h1. → 'decisiontree'
|
4
|
+
|
5
|
+
|
6
|
+
h2. What
|
7
|
+
|
8
|
+
|
9
|
+
h2. Installing
|
10
|
+
|
11
|
+
<pre syntax="ruby">sudo gem install decisiontree</pre>
|
12
|
+
|
13
|
+
h2. The basics
|
14
|
+
|
15
|
+
|
16
|
+
h2. Demonstration of usage
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
h2. Forum
|
21
|
+
|
22
|
+
"http://groups.google.com/group/decisiontree":http://groups.google.com/group/decisiontree
|
23
|
+
|
24
|
+
TODO - create Google Group - decisiontree
|
25
|
+
|
26
|
+
h2. How to submit patches
|
27
|
+
|
28
|
+
Read the "8 steps for fixing other people's code":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/ and for section "8b: Submit patch to Google Groups":http://drnicwilliams.com/2007/06/01/8-steps-for-fixing-other-peoples-code/#8b-google-groups, use the Google Group above.
|
29
|
+
|
30
|
+
The trunk repository is <code>svn://rubyforge.org/var/svn/decisiontree/trunk</code> for anonymous access.
|
31
|
+
|
32
|
+
h2. License
|
33
|
+
|
34
|
+
This code is free to use under the terms of the MIT license.
|
35
|
+
|
36
|
+
h2. Contact
|
37
|
+
|
38
|
+
Comments are welcome. Send an email to "FIXME full name":mailto:FIXME email.
|
@@ -0,0 +1,285 @@
|
|
1
|
+
|
2
|
+
/****************************************************************
|
3
|
+
* *
|
4
|
+
* curvyCorners *
|
5
|
+
* ------------ *
|
6
|
+
* *
|
7
|
+
* This script generates rounded corners for your divs. *
|
8
|
+
* *
|
9
|
+
* Version 1.2.9 *
|
10
|
+
* Copyright (c) 2006 Cameron Cooke *
|
11
|
+
* By: Cameron Cooke and Tim Hutchison. *
|
12
|
+
* *
|
13
|
+
* *
|
14
|
+
* Website: http://www.curvycorners.net *
|
15
|
+
* Email: info@totalinfinity.com *
|
16
|
+
* Forum: http://www.curvycorners.net/forum/ *
|
17
|
+
* *
|
18
|
+
* *
|
19
|
+
* This library is free software; you can redistribute *
|
20
|
+
* it and/or modify it under the terms of the GNU *
|
21
|
+
* Lesser General Public License as published by the *
|
22
|
+
* Free Software Foundation; either version 2.1 of the *
|
23
|
+
* License, or (at your option) any later version. *
|
24
|
+
* *
|
25
|
+
* This library is distributed in the hope that it will *
|
26
|
+
* be useful, but WITHOUT ANY WARRANTY; without even the *
|
27
|
+
* implied warranty of MERCHANTABILITY or FITNESS FOR A *
|
28
|
+
* PARTICULAR PURPOSE. See the GNU Lesser General Public *
|
29
|
+
* License for more details. *
|
30
|
+
* *
|
31
|
+
* You should have received a copy of the GNU Lesser *
|
32
|
+
* General Public License along with this library; *
|
33
|
+
* Inc., 59 Temple Place, Suite 330, Boston, *
|
34
|
+
* MA 02111-1307 USA *
|
35
|
+
* *
|
36
|
+
****************************************************************/
|
37
|
+
|
38
|
+
var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1; var isMoz = document.implementation && document.implementation.createDocument; var isSafari = ((navigator.userAgent.toLowerCase().indexOf('safari')!=-1)&&(navigator.userAgent.toLowerCase().indexOf('mac')!=-1))?true:false; function curvyCorners()
|
39
|
+
{ if(typeof(arguments[0]) != "object") throw newCurvyError("First parameter of curvyCorners() must be an object."); if(typeof(arguments[1]) != "object" && typeof(arguments[1]) != "string") throw newCurvyError("Second parameter of curvyCorners() must be an object or a class name."); if(typeof(arguments[1]) == "string")
|
40
|
+
{ var startIndex = 0; var boxCol = getElementsByClass(arguments[1]);}
|
41
|
+
else
|
42
|
+
{ var startIndex = 1; var boxCol = arguments;}
|
43
|
+
var curvyCornersCol = new Array(); if(arguments[0].validTags)
|
44
|
+
var validElements = arguments[0].validTags; else
|
45
|
+
var validElements = ["div"]; for(var i = startIndex, j = boxCol.length; i < j; i++)
|
46
|
+
{ var currentTag = boxCol[i].tagName.toLowerCase(); if(inArray(validElements, currentTag) !== false)
|
47
|
+
{ curvyCornersCol[curvyCornersCol.length] = new curvyObject(arguments[0], boxCol[i]);}
|
48
|
+
}
|
49
|
+
this.objects = curvyCornersCol; this.applyCornersToAll = function()
|
50
|
+
{ for(var x = 0, k = this.objects.length; x < k; x++)
|
51
|
+
{ this.objects[x].applyCorners();}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
function curvyObject()
|
55
|
+
{ this.box = arguments[1]; this.settings = arguments[0]; this.topContainer = null; this.bottomContainer = null; this.masterCorners = new Array(); this.contentDIV = null; var boxHeight = get_style(this.box, "height", "height"); var boxWidth = get_style(this.box, "width", "width"); var borderWidth = get_style(this.box, "borderTopWidth", "border-top-width"); var borderColour = get_style(this.box, "borderTopColor", "border-top-color"); var boxColour = get_style(this.box, "backgroundColor", "background-color"); var backgroundImage = get_style(this.box, "backgroundImage", "background-image"); var boxPosition = get_style(this.box, "position", "position"); var boxPadding = get_style(this.box, "paddingTop", "padding-top"); this.boxHeight = parseInt(((boxHeight != "" && boxHeight != "auto" && boxHeight.indexOf("%") == -1)? boxHeight.substring(0, boxHeight.indexOf("px")) : this.box.scrollHeight)); this.boxWidth = parseInt(((boxWidth != "" && boxWidth != "auto" && boxWidth.indexOf("%") == -1)? boxWidth.substring(0, boxWidth.indexOf("px")) : this.box.scrollWidth)); this.borderWidth = parseInt(((borderWidth != "" && borderWidth.indexOf("px") !== -1)? borderWidth.slice(0, borderWidth.indexOf("px")) : 0)); this.boxColour = format_colour(boxColour); this.boxPadding = parseInt(((boxPadding != "" && boxPadding.indexOf("px") !== -1)? boxPadding.slice(0, boxPadding.indexOf("px")) : 0)); this.borderColour = format_colour(borderColour); this.borderString = this.borderWidth + "px" + " solid " + this.borderColour; this.backgroundImage = ((backgroundImage != "none")? backgroundImage : ""); this.boxContent = this.box.innerHTML; if(boxPosition != "absolute") this.box.style.position = "relative"; this.box.style.padding = "0px"; if(isIE && boxWidth == "auto" && boxHeight == "auto") this.box.style.width = "100%"; if(this.settings.autoPad == true && this.boxPadding > 0)
|
56
|
+
this.box.innerHTML = ""; this.applyCorners = function()
|
57
|
+
{ for(var t = 0; t < 2; t++)
|
58
|
+
{ switch(t)
|
59
|
+
{ case 0:
|
60
|
+
if(this.settings.tl || this.settings.tr)
|
61
|
+
{ var newMainContainer = document.createElement("DIV"); newMainContainer.style.width = "100%"; newMainContainer.style.fontSize = "1px"; newMainContainer.style.overflow = "hidden"; newMainContainer.style.position = "absolute"; newMainContainer.style.paddingLeft = this.borderWidth + "px"; newMainContainer.style.paddingRight = this.borderWidth + "px"; var topMaxRadius = Math.max(this.settings.tl ? this.settings.tl.radius : 0, this.settings.tr ? this.settings.tr.radius : 0); newMainContainer.style.height = topMaxRadius + "px"; newMainContainer.style.top = 0 - topMaxRadius + "px"; newMainContainer.style.left = 0 - this.borderWidth + "px"; this.topContainer = this.box.appendChild(newMainContainer);}
|
62
|
+
break; case 1:
|
63
|
+
if(this.settings.bl || this.settings.br)
|
64
|
+
{ var newMainContainer = document.createElement("DIV"); newMainContainer.style.width = "100%"; newMainContainer.style.fontSize = "1px"; newMainContainer.style.overflow = "hidden"; newMainContainer.style.position = "absolute"; newMainContainer.style.paddingLeft = this.borderWidth + "px"; newMainContainer.style.paddingRight = this.borderWidth + "px"; var botMaxRadius = Math.max(this.settings.bl ? this.settings.bl.radius : 0, this.settings.br ? this.settings.br.radius : 0); newMainContainer.style.height = botMaxRadius + "px"; newMainContainer.style.bottom = 0 - botMaxRadius + "px"; newMainContainer.style.left = 0 - this.borderWidth + "px"; this.bottomContainer = this.box.appendChild(newMainContainer);}
|
65
|
+
break;}
|
66
|
+
}
|
67
|
+
if(this.topContainer) this.box.style.borderTopWidth = "0px"; if(this.bottomContainer) this.box.style.borderBottomWidth = "0px"; var corners = ["tr", "tl", "br", "bl"]; for(var i in corners)
|
68
|
+
{ if(i > -1 < 4)
|
69
|
+
{ var cc = corners[i]; if(!this.settings[cc])
|
70
|
+
{ if(((cc == "tr" || cc == "tl") && this.topContainer != null) || ((cc == "br" || cc == "bl") && this.bottomContainer != null))
|
71
|
+
{ var newCorner = document.createElement("DIV"); newCorner.style.position = "relative"; newCorner.style.fontSize = "1px"; newCorner.style.overflow = "hidden"; if(this.backgroundImage == "")
|
72
|
+
newCorner.style.backgroundColor = this.boxColour; else
|
73
|
+
newCorner.style.backgroundImage = this.backgroundImage; switch(cc)
|
74
|
+
{ case "tl":
|
75
|
+
newCorner.style.height = topMaxRadius - this.borderWidth + "px"; newCorner.style.marginRight = this.settings.tr.radius - (this.borderWidth*2) + "px"; newCorner.style.borderLeft = this.borderString; newCorner.style.borderTop = this.borderString; newCorner.style.left = -this.borderWidth + "px"; break; case "tr":
|
76
|
+
newCorner.style.height = topMaxRadius - this.borderWidth + "px"; newCorner.style.marginLeft = this.settings.tl.radius - (this.borderWidth*2) + "px"; newCorner.style.borderRight = this.borderString; newCorner.style.borderTop = this.borderString; newCorner.style.backgroundPosition = "-" + (topMaxRadius + this.borderWidth) + "px 0px"; newCorner.style.left = this.borderWidth + "px"; break; case "bl":
|
77
|
+
newCorner.style.height = botMaxRadius - this.borderWidth + "px"; newCorner.style.marginRight = this.settings.br.radius - (this.borderWidth*2) + "px"; newCorner.style.borderLeft = this.borderString; newCorner.style.borderBottom = this.borderString; newCorner.style.left = -this.borderWidth + "px"; newCorner.style.backgroundPosition = "-" + (this.borderWidth) + "px -" + (this.boxHeight + (botMaxRadius + this.borderWidth)) + "px"; break; case "br":
|
78
|
+
newCorner.style.height = botMaxRadius - this.borderWidth + "px"; newCorner.style.marginLeft = this.settings.bl.radius - (this.borderWidth*2) + "px"; newCorner.style.borderRight = this.borderString; newCorner.style.borderBottom = this.borderString; newCorner.style.left = this.borderWidth + "px"
|
79
|
+
newCorner.style.backgroundPosition = "-" + (botMaxRadius + this.borderWidth) + "px -" + (this.boxHeight + (botMaxRadius + this.borderWidth)) + "px"; break;}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
else
|
83
|
+
{ if(this.masterCorners[this.settings[cc].radius])
|
84
|
+
{ var newCorner = this.masterCorners[this.settings[cc].radius].cloneNode(true);}
|
85
|
+
else
|
86
|
+
{ var newCorner = document.createElement("DIV"); newCorner.style.height = this.settings[cc].radius + "px"; newCorner.style.width = this.settings[cc].radius + "px"; newCorner.style.position = "absolute"; newCorner.style.fontSize = "1px"; newCorner.style.overflow = "hidden"; var borderRadius = parseInt(this.settings[cc].radius - this.borderWidth); for(var intx = 0, j = this.settings[cc].radius; intx < j; intx++)
|
87
|
+
{ if((intx +1) >= borderRadius)
|
88
|
+
var y1 = -1; else
|
89
|
+
var y1 = (Math.floor(Math.sqrt(Math.pow(borderRadius, 2) - Math.pow((intx+1), 2))) - 1); if(borderRadius != j)
|
90
|
+
{ if((intx) >= borderRadius)
|
91
|
+
var y2 = -1; else
|
92
|
+
var y2 = Math.ceil(Math.sqrt(Math.pow(borderRadius,2) - Math.pow(intx, 2))); if((intx+1) >= j)
|
93
|
+
var y3 = -1; else
|
94
|
+
var y3 = (Math.floor(Math.sqrt(Math.pow(j ,2) - Math.pow((intx+1), 2))) - 1);}
|
95
|
+
if((intx) >= j)
|
96
|
+
var y4 = -1; else
|
97
|
+
var y4 = Math.ceil(Math.sqrt(Math.pow(j ,2) - Math.pow(intx, 2))); if(y1 > -1) this.drawPixel(intx, 0, this.boxColour, 100, (y1+1), newCorner, -1, this.settings[cc].radius); if(borderRadius != j)
|
98
|
+
{ for(var inty = (y1 + 1); inty < y2; inty++)
|
99
|
+
{ if(this.settings.antiAlias)
|
100
|
+
{ if(this.backgroundImage != "")
|
101
|
+
{ var borderFract = (pixelFraction(intx, inty, borderRadius) * 100); if(borderFract < 30)
|
102
|
+
{ this.drawPixel(intx, inty, this.borderColour, 100, 1, newCorner, 0, this.settings[cc].radius);}
|
103
|
+
else
|
104
|
+
{ this.drawPixel(intx, inty, this.borderColour, 100, 1, newCorner, -1, this.settings[cc].radius);}
|
105
|
+
}
|
106
|
+
else
|
107
|
+
{ var pixelcolour = BlendColour(this.boxColour, this.borderColour, pixelFraction(intx, inty, borderRadius)); this.drawPixel(intx, inty, pixelcolour, 100, 1, newCorner, 0, this.settings[cc].radius, cc);}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
if(this.settings.antiAlias)
|
111
|
+
{ if(y3 >= y2)
|
112
|
+
{ if (y2 == -1) y2 = 0; this.drawPixel(intx, y2, this.borderColour, 100, (y3 - y2 + 1), newCorner, 0, 0);}
|
113
|
+
}
|
114
|
+
else
|
115
|
+
{ if(y3 >= y1)
|
116
|
+
{ this.drawPixel(intx, (y1 + 1), this.borderColour, 100, (y3 - y1), newCorner, 0, 0);}
|
117
|
+
}
|
118
|
+
var outsideColour = this.borderColour;}
|
119
|
+
else
|
120
|
+
{ var outsideColour = this.boxColour; var y3 = y1;}
|
121
|
+
if(this.settings.antiAlias)
|
122
|
+
{ for(var inty = (y3 + 1); inty < y4; inty++)
|
123
|
+
{ this.drawPixel(intx, inty, outsideColour, (pixelFraction(intx, inty , j) * 100), 1, newCorner, ((this.borderWidth > 0)? 0 : -1), this.settings[cc].radius);}
|
124
|
+
}
|
125
|
+
}
|
126
|
+
this.masterCorners[this.settings[cc].radius] = newCorner.cloneNode(true);}
|
127
|
+
if(cc != "br")
|
128
|
+
{ for(var t = 0, k = newCorner.childNodes.length; t < k; t++)
|
129
|
+
{ var pixelBar = newCorner.childNodes[t]; var pixelBarTop = parseInt(pixelBar.style.top.substring(0, pixelBar.style.top.indexOf("px"))); var pixelBarLeft = parseInt(pixelBar.style.left.substring(0, pixelBar.style.left.indexOf("px"))); var pixelBarHeight = parseInt(pixelBar.style.height.substring(0, pixelBar.style.height.indexOf("px"))); if(cc == "tl" || cc == "bl"){ pixelBar.style.left = this.settings[cc].radius -pixelBarLeft -1 + "px";}
|
130
|
+
if(cc == "tr" || cc == "tl"){ pixelBar.style.top = this.settings[cc].radius -pixelBarHeight -pixelBarTop + "px";}
|
131
|
+
switch(cc)
|
132
|
+
{ case "tr":
|
133
|
+
pixelBar.style.backgroundPosition = "-" + Math.abs((this.boxWidth - this.settings[cc].radius + this.borderWidth) + pixelBarLeft) + "px -" + Math.abs(this.settings[cc].radius -pixelBarHeight -pixelBarTop - this.borderWidth) + "px"; break; case "tl":
|
134
|
+
pixelBar.style.backgroundPosition = "-" + Math.abs((this.settings[cc].radius -pixelBarLeft -1) - this.borderWidth) + "px -" + Math.abs(this.settings[cc].radius -pixelBarHeight -pixelBarTop - this.borderWidth) + "px"; break; case "bl":
|
135
|
+
pixelBar.style.backgroundPosition = "-" + Math.abs((this.settings[cc].radius -pixelBarLeft -1) - this.borderWidth) + "px -" + Math.abs((this.boxHeight + this.settings[cc].radius + pixelBarTop) -this.borderWidth) + "px"; break;}
|
136
|
+
}
|
137
|
+
}
|
138
|
+
}
|
139
|
+
if(newCorner)
|
140
|
+
{ switch(cc)
|
141
|
+
{ case "tl":
|
142
|
+
if(newCorner.style.position == "absolute") newCorner.style.top = "0px"; if(newCorner.style.position == "absolute") newCorner.style.left = "0px"; if(this.topContainer) this.topContainer.appendChild(newCorner); break; case "tr":
|
143
|
+
if(newCorner.style.position == "absolute") newCorner.style.top = "0px"; if(newCorner.style.position == "absolute") newCorner.style.right = "0px"; if(this.topContainer) this.topContainer.appendChild(newCorner); break; case "bl":
|
144
|
+
if(newCorner.style.position == "absolute") newCorner.style.bottom = "0px"; if(newCorner.style.position == "absolute") newCorner.style.left = "0px"; if(this.bottomContainer) this.bottomContainer.appendChild(newCorner); break; case "br":
|
145
|
+
if(newCorner.style.position == "absolute") newCorner.style.bottom = "0px"; if(newCorner.style.position == "absolute") newCorner.style.right = "0px"; if(this.bottomContainer) this.bottomContainer.appendChild(newCorner); break;}
|
146
|
+
}
|
147
|
+
}
|
148
|
+
}
|
149
|
+
var radiusDiff = new Array(); radiusDiff["t"] = Math.abs(this.settings.tl.radius - this.settings.tr.radius)
|
150
|
+
radiusDiff["b"] = Math.abs(this.settings.bl.radius - this.settings.br.radius); for(z in radiusDiff)
|
151
|
+
{ if(z == "t" || z == "b")
|
152
|
+
{ if(radiusDiff[z])
|
153
|
+
{ var smallerCornerType = ((this.settings[z + "l"].radius < this.settings[z + "r"].radius)? z +"l" : z +"r"); var newFiller = document.createElement("DIV"); newFiller.style.height = radiusDiff[z] + "px"; newFiller.style.width = this.settings[smallerCornerType].radius+ "px"
|
154
|
+
newFiller.style.position = "absolute"; newFiller.style.fontSize = "1px"; newFiller.style.overflow = "hidden"; newFiller.style.backgroundColor = this.boxColour; switch(smallerCornerType)
|
155
|
+
{ case "tl":
|
156
|
+
newFiller.style.bottom = "0px"; newFiller.style.left = "0px"; newFiller.style.borderLeft = this.borderString; this.topContainer.appendChild(newFiller); break; case "tr":
|
157
|
+
newFiller.style.bottom = "0px"; newFiller.style.right = "0px"; newFiller.style.borderRight = this.borderString; this.topContainer.appendChild(newFiller); break; case "bl":
|
158
|
+
newFiller.style.top = "0px"; newFiller.style.left = "0px"; newFiller.style.borderLeft = this.borderString; this.bottomContainer.appendChild(newFiller); break; case "br":
|
159
|
+
newFiller.style.top = "0px"; newFiller.style.right = "0px"; newFiller.style.borderRight = this.borderString; this.bottomContainer.appendChild(newFiller); break;}
|
160
|
+
}
|
161
|
+
var newFillerBar = document.createElement("DIV"); newFillerBar.style.position = "relative"; newFillerBar.style.fontSize = "1px"; newFillerBar.style.overflow = "hidden"; newFillerBar.style.backgroundColor = this.boxColour; newFillerBar.style.backgroundImage = this.backgroundImage; switch(z)
|
162
|
+
{ case "t":
|
163
|
+
if(this.topContainer)
|
164
|
+
{ if(this.settings.tl.radius && this.settings.tr.radius)
|
165
|
+
{ newFillerBar.style.height = topMaxRadius - this.borderWidth + "px"; newFillerBar.style.marginLeft = this.settings.tl.radius - this.borderWidth + "px"; newFillerBar.style.marginRight = this.settings.tr.radius - this.borderWidth + "px"; newFillerBar.style.borderTop = this.borderString; if(this.backgroundImage != "")
|
166
|
+
newFillerBar.style.backgroundPosition = "-" + (topMaxRadius + this.borderWidth) + "px 0px"; this.topContainer.appendChild(newFillerBar);}
|
167
|
+
this.box.style.backgroundPosition = "0px -" + (topMaxRadius - this.borderWidth) + "px";}
|
168
|
+
break; case "b":
|
169
|
+
if(this.bottomContainer)
|
170
|
+
{ if(this.settings.bl.radius && this.settings.br.radius)
|
171
|
+
{ newFillerBar.style.height = botMaxRadius - this.borderWidth + "px"; newFillerBar.style.marginLeft = this.settings.bl.radius - this.borderWidth + "px"; newFillerBar.style.marginRight = this.settings.br.radius - this.borderWidth + "px"; newFillerBar.style.borderBottom = this.borderString; if(this.backgroundImage != "")
|
172
|
+
newFillerBar.style.backgroundPosition = "-" + (botMaxRadius + this.borderWidth) + "px -" + (this.boxHeight + (topMaxRadius + this.borderWidth)) + "px"; this.bottomContainer.appendChild(newFillerBar);}
|
173
|
+
}
|
174
|
+
break;}
|
175
|
+
}
|
176
|
+
}
|
177
|
+
if(this.settings.autoPad == true && this.boxPadding > 0)
|
178
|
+
{ var contentContainer = document.createElement("DIV"); contentContainer.style.position = "relative"; contentContainer.innerHTML = this.boxContent; contentContainer.className = "autoPadDiv"; var topPadding = Math.abs(topMaxRadius - this.boxPadding); var botPadding = Math.abs(botMaxRadius - this.boxPadding); if(topMaxRadius < this.boxPadding)
|
179
|
+
contentContainer.style.paddingTop = topPadding + "px"; if(botMaxRadius < this.boxPadding)
|
180
|
+
contentContainer.style.paddingBottom = botMaxRadius + "px"; contentContainer.style.paddingLeft = this.boxPadding + "px"; contentContainer.style.paddingRight = this.boxPadding + "px"; this.contentDIV = this.box.appendChild(contentContainer);}
|
181
|
+
}
|
182
|
+
this.drawPixel = function(intx, inty, colour, transAmount, height, newCorner, image, cornerRadius)
|
183
|
+
{ var pixel = document.createElement("DIV"); pixel.style.height = height + "px"; pixel.style.width = "1px"; pixel.style.position = "absolute"; pixel.style.fontSize = "1px"; pixel.style.overflow = "hidden"; var topMaxRadius = Math.max(this.settings["tr"].radius, this.settings["tl"].radius); if(image == -1 && this.backgroundImage != "")
|
184
|
+
{ pixel.style.backgroundImage = this.backgroundImage; pixel.style.backgroundPosition = "-" + (this.boxWidth - (cornerRadius - intx) + this.borderWidth) + "px -" + ((this.boxHeight + topMaxRadius + inty) -this.borderWidth) + "px";}
|
185
|
+
else
|
186
|
+
{ pixel.style.backgroundColor = colour;}
|
187
|
+
if (transAmount != 100)
|
188
|
+
setOpacity(pixel, transAmount); pixel.style.top = inty + "px"; pixel.style.left = intx + "px"; newCorner.appendChild(pixel);}
|
189
|
+
}
|
190
|
+
function insertAfter(parent, node, referenceNode)
|
191
|
+
{ parent.insertBefore(node, referenceNode.nextSibling);}
|
192
|
+
function BlendColour(Col1, Col2, Col1Fraction)
|
193
|
+
{ var red1 = parseInt(Col1.substr(1,2),16); var green1 = parseInt(Col1.substr(3,2),16); var blue1 = parseInt(Col1.substr(5,2),16); var red2 = parseInt(Col2.substr(1,2),16); var green2 = parseInt(Col2.substr(3,2),16); var blue2 = parseInt(Col2.substr(5,2),16); if(Col1Fraction > 1 || Col1Fraction < 0) Col1Fraction = 1; var endRed = Math.round((red1 * Col1Fraction) + (red2 * (1 - Col1Fraction))); if(endRed > 255) endRed = 255; if(endRed < 0) endRed = 0; var endGreen = Math.round((green1 * Col1Fraction) + (green2 * (1 - Col1Fraction))); if(endGreen > 255) endGreen = 255; if(endGreen < 0) endGreen = 0; var endBlue = Math.round((blue1 * Col1Fraction) + (blue2 * (1 - Col1Fraction))); if(endBlue > 255) endBlue = 255; if(endBlue < 0) endBlue = 0; return "#" + IntToHex(endRed)+ IntToHex(endGreen)+ IntToHex(endBlue);}
|
194
|
+
function IntToHex(strNum)
|
195
|
+
{ base = strNum / 16; rem = strNum % 16; base = base - (rem / 16); baseS = MakeHex(base); remS = MakeHex(rem); return baseS + '' + remS;}
|
196
|
+
function MakeHex(x)
|
197
|
+
{ if((x >= 0) && (x <= 9))
|
198
|
+
{ return x;}
|
199
|
+
else
|
200
|
+
{ switch(x)
|
201
|
+
{ case 10: return "A"; case 11: return "B"; case 12: return "C"; case 13: return "D"; case 14: return "E"; case 15: return "F";}
|
202
|
+
}
|
203
|
+
}
|
204
|
+
function pixelFraction(x, y, r)
|
205
|
+
{ var pixelfraction = 0; var xvalues = new Array(1); var yvalues = new Array(1); var point = 0; var whatsides = ""; var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(x,2))); if ((intersect >= y) && (intersect < (y+1)))
|
206
|
+
{ whatsides = "Left"; xvalues[point] = 0; yvalues[point] = intersect - y; point = point + 1;}
|
207
|
+
var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(y+1,2))); if ((intersect >= x) && (intersect < (x+1)))
|
208
|
+
{ whatsides = whatsides + "Top"; xvalues[point] = intersect - x; yvalues[point] = 1; point = point + 1;}
|
209
|
+
var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(x+1,2))); if ((intersect >= y) && (intersect < (y+1)))
|
210
|
+
{ whatsides = whatsides + "Right"; xvalues[point] = 1; yvalues[point] = intersect - y; point = point + 1;}
|
211
|
+
var intersect = Math.sqrt((Math.pow(r,2) - Math.pow(y,2))); if ((intersect >= x) && (intersect < (x+1)))
|
212
|
+
{ whatsides = whatsides + "Bottom"; xvalues[point] = intersect - x; yvalues[point] = 0;}
|
213
|
+
switch (whatsides)
|
214
|
+
{ case "LeftRight":
|
215
|
+
pixelfraction = Math.min(yvalues[0],yvalues[1]) + ((Math.max(yvalues[0],yvalues[1]) - Math.min(yvalues[0],yvalues[1]))/2); break; case "TopRight":
|
216
|
+
pixelfraction = 1-(((1-xvalues[0])*(1-yvalues[1]))/2); break; case "TopBottom":
|
217
|
+
pixelfraction = Math.min(xvalues[0],xvalues[1]) + ((Math.max(xvalues[0],xvalues[1]) - Math.min(xvalues[0],xvalues[1]))/2); break; case "LeftBottom":
|
218
|
+
pixelfraction = (yvalues[0]*xvalues[1])/2; break; default:
|
219
|
+
pixelfraction = 1;}
|
220
|
+
return pixelfraction;}
|
221
|
+
function rgb2Hex(rgbColour)
|
222
|
+
{ try{ var rgbArray = rgb2Array(rgbColour); var red = parseInt(rgbArray[0]); var green = parseInt(rgbArray[1]); var blue = parseInt(rgbArray[2]); var hexColour = "#" + IntToHex(red) + IntToHex(green) + IntToHex(blue);}
|
223
|
+
catch(e){ alert("There was an error converting the RGB value to Hexadecimal in function rgb2Hex");}
|
224
|
+
return hexColour;}
|
225
|
+
function rgb2Array(rgbColour)
|
226
|
+
{ var rgbValues = rgbColour.substring(4, rgbColour.indexOf(")")); var rgbArray = rgbValues.split(", "); return rgbArray;}
|
227
|
+
function setOpacity(obj, opacity)
|
228
|
+
{ opacity = (opacity == 100)?99.999:opacity; if(isSafari && obj.tagName != "IFRAME")
|
229
|
+
{ var rgbArray = rgb2Array(obj.style.backgroundColor); var red = parseInt(rgbArray[0]); var green = parseInt(rgbArray[1]); var blue = parseInt(rgbArray[2]); obj.style.backgroundColor = "rgba(" + red + ", " + green + ", " + blue + ", " + opacity/100 + ")";}
|
230
|
+
else if(typeof(obj.style.opacity) != "undefined")
|
231
|
+
{ obj.style.opacity = opacity/100;}
|
232
|
+
else if(typeof(obj.style.MozOpacity) != "undefined")
|
233
|
+
{ obj.style.MozOpacity = opacity/100;}
|
234
|
+
else if(typeof(obj.style.filter) != "undefined")
|
235
|
+
{ obj.style.filter = "alpha(opacity:" + opacity + ")";}
|
236
|
+
else if(typeof(obj.style.KHTMLOpacity) != "undefined")
|
237
|
+
{ obj.style.KHTMLOpacity = opacity/100;}
|
238
|
+
}
|
239
|
+
function inArray(array, value)
|
240
|
+
{ for(var i = 0; i < array.length; i++){ if (array[i] === value) return i;}
|
241
|
+
return false;}
|
242
|
+
function inArrayKey(array, value)
|
243
|
+
{ for(key in array){ if(key === value) return true;}
|
244
|
+
return false;}
|
245
|
+
function addEvent(elm, evType, fn, useCapture) { if (elm.addEventListener) { elm.addEventListener(evType, fn, useCapture); return true;}
|
246
|
+
else if (elm.attachEvent) { var r = elm.attachEvent('on' + evType, fn); return r;}
|
247
|
+
else { elm['on' + evType] = fn;}
|
248
|
+
}
|
249
|
+
function removeEvent(obj, evType, fn, useCapture){ if (obj.removeEventListener){ obj.removeEventListener(evType, fn, useCapture); return true;} else if (obj.detachEvent){ var r = obj.detachEvent("on"+evType, fn); return r;} else { alert("Handler could not be removed");}
|
250
|
+
}
|
251
|
+
function format_colour(colour)
|
252
|
+
{ var returnColour = "#ffffff"; if(colour != "" && colour != "transparent")
|
253
|
+
{ if(colour.substr(0, 3) == "rgb")
|
254
|
+
{ returnColour = rgb2Hex(colour);}
|
255
|
+
else if(colour.length == 4)
|
256
|
+
{ returnColour = "#" + colour.substring(1, 2) + colour.substring(1, 2) + colour.substring(2, 3) + colour.substring(2, 3) + colour.substring(3, 4) + colour.substring(3, 4);}
|
257
|
+
else
|
258
|
+
{ returnColour = colour;}
|
259
|
+
}
|
260
|
+
return returnColour;}
|
261
|
+
function get_style(obj, property, propertyNS)
|
262
|
+
{ try
|
263
|
+
{ if(obj.currentStyle)
|
264
|
+
{ var returnVal = eval("obj.currentStyle." + property);}
|
265
|
+
else
|
266
|
+
{ if(isSafari && obj.style.display == "none")
|
267
|
+
{ obj.style.display = ""; var wasHidden = true;}
|
268
|
+
var returnVal = document.defaultView.getComputedStyle(obj, '').getPropertyValue(propertyNS); if(isSafari && wasHidden)
|
269
|
+
{ obj.style.display = "none";}
|
270
|
+
}
|
271
|
+
}
|
272
|
+
catch(e)
|
273
|
+
{ }
|
274
|
+
return returnVal;}
|
275
|
+
function getElementsByClass(searchClass, node, tag)
|
276
|
+
{ var classElements = new Array(); if(node == null)
|
277
|
+
node = document; if(tag == null)
|
278
|
+
tag = '*'; var els = node.getElementsByTagName(tag); var elsLen = els.length; var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)"); for (i = 0, j = 0; i < elsLen; i++)
|
279
|
+
{ if(pattern.test(els[i].className))
|
280
|
+
{ classElements[j] = els[i]; j++;}
|
281
|
+
}
|
282
|
+
return classElements;}
|
283
|
+
function newCurvyError(errorMessage)
|
284
|
+
{ return new Error("curvyCorners Error:\n" + errorMessage)
|
285
|
+
}
|
@@ -0,0 +1,138 @@
|
|
1
|
+
body {
|
2
|
+
background-color: #E1D1F1;
|
3
|
+
font-family: "Georgia", sans-serif;
|
4
|
+
font-size: 16px;
|
5
|
+
line-height: 1.6em;
|
6
|
+
padding: 1.6em 0 0 0;
|
7
|
+
color: #333;
|
8
|
+
}
|
9
|
+
h1, h2, h3, h4, h5, h6 {
|
10
|
+
color: #444;
|
11
|
+
}
|
12
|
+
h1 {
|
13
|
+
font-family: sans-serif;
|
14
|
+
font-weight: normal;
|
15
|
+
font-size: 4em;
|
16
|
+
line-height: 0.8em;
|
17
|
+
letter-spacing: -0.1ex;
|
18
|
+
margin: 5px;
|
19
|
+
}
|
20
|
+
li {
|
21
|
+
padding: 0;
|
22
|
+
margin: 0;
|
23
|
+
list-style-type: square;
|
24
|
+
}
|
25
|
+
a {
|
26
|
+
color: #5E5AFF;
|
27
|
+
background-color: #DAC;
|
28
|
+
font-weight: normal;
|
29
|
+
text-decoration: underline;
|
30
|
+
}
|
31
|
+
blockquote {
|
32
|
+
font-size: 90%;
|
33
|
+
font-style: italic;
|
34
|
+
border-left: 1px solid #111;
|
35
|
+
padding-left: 1em;
|
36
|
+
}
|
37
|
+
.caps {
|
38
|
+
font-size: 80%;
|
39
|
+
}
|
40
|
+
|
41
|
+
#main {
|
42
|
+
width: 45em;
|
43
|
+
padding: 0;
|
44
|
+
margin: 0 auto;
|
45
|
+
}
|
46
|
+
.coda {
|
47
|
+
text-align: right;
|
48
|
+
color: #77f;
|
49
|
+
font-size: smaller;
|
50
|
+
}
|
51
|
+
|
52
|
+
table {
|
53
|
+
font-size: 90%;
|
54
|
+
line-height: 1.4em;
|
55
|
+
color: #ff8;
|
56
|
+
background-color: #111;
|
57
|
+
padding: 2px 10px 2px 10px;
|
58
|
+
border-style: dashed;
|
59
|
+
}
|
60
|
+
|
61
|
+
th {
|
62
|
+
color: #fff;
|
63
|
+
}
|
64
|
+
|
65
|
+
td {
|
66
|
+
padding: 2px 10px 2px 10px;
|
67
|
+
}
|
68
|
+
|
69
|
+
.success {
|
70
|
+
color: #0CC52B;
|
71
|
+
}
|
72
|
+
|
73
|
+
.failed {
|
74
|
+
color: #E90A1B;
|
75
|
+
}
|
76
|
+
|
77
|
+
.unknown {
|
78
|
+
color: #995000;
|
79
|
+
}
|
80
|
+
pre, code {
|
81
|
+
font-family: monospace;
|
82
|
+
font-size: 90%;
|
83
|
+
line-height: 1.4em;
|
84
|
+
color: #ff8;
|
85
|
+
background-color: #111;
|
86
|
+
padding: 2px 10px 2px 10px;
|
87
|
+
}
|
88
|
+
.comment { color: #aaa; font-style: italic; }
|
89
|
+
.keyword { color: #eff; font-weight: bold; }
|
90
|
+
.punct { color: #eee; font-weight: bold; }
|
91
|
+
.symbol { color: #0bb; }
|
92
|
+
.string { color: #6b4; }
|
93
|
+
.ident { color: #ff8; }
|
94
|
+
.constant { color: #66f; }
|
95
|
+
.regex { color: #ec6; }
|
96
|
+
.number { color: #F99; }
|
97
|
+
.expr { color: #227; }
|
98
|
+
|
99
|
+
#version {
|
100
|
+
float: right;
|
101
|
+
text-align: right;
|
102
|
+
font-family: sans-serif;
|
103
|
+
font-weight: normal;
|
104
|
+
background-color: #B3ABFF;
|
105
|
+
color: #141331;
|
106
|
+
padding: 15px 20px 10px 20px;
|
107
|
+
margin: 0 auto;
|
108
|
+
margin-top: 15px;
|
109
|
+
border: 3px solid #141331;
|
110
|
+
}
|
111
|
+
|
112
|
+
#version .numbers {
|
113
|
+
display: block;
|
114
|
+
font-size: 4em;
|
115
|
+
line-height: 0.8em;
|
116
|
+
letter-spacing: -0.1ex;
|
117
|
+
margin-bottom: 15px;
|
118
|
+
}
|
119
|
+
|
120
|
+
#version p {
|
121
|
+
text-decoration: none;
|
122
|
+
color: #141331;
|
123
|
+
background-color: #B3ABFF;
|
124
|
+
margin: 0;
|
125
|
+
padding: 0;
|
126
|
+
}
|
127
|
+
|
128
|
+
#version a {
|
129
|
+
text-decoration: none;
|
130
|
+
color: #141331;
|
131
|
+
background-color: #B3ABFF;
|
132
|
+
}
|
133
|
+
|
134
|
+
.clickable {
|
135
|
+
cursor: pointer;
|
136
|
+
cursor: hand;
|
137
|
+
}
|
138
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<link rel="stylesheet" href="stylesheets/screen.css" type="text/css" media="screen" />
|
6
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
7
|
+
<title>
|
8
|
+
<%= title %>
|
9
|
+
</title>
|
10
|
+
<script src="javascripts/rounded_corners_lite.inc.js" type="text/javascript"></script>
|
11
|
+
<style>
|
12
|
+
|
13
|
+
</style>
|
14
|
+
<script type="text/javascript">
|
15
|
+
window.onload = function() {
|
16
|
+
settings = {
|
17
|
+
tl: { radius: 10 },
|
18
|
+
tr: { radius: 10 },
|
19
|
+
bl: { radius: 10 },
|
20
|
+
br: { radius: 10 },
|
21
|
+
antiAlias: true,
|
22
|
+
autoPad: true,
|
23
|
+
validTags: ["div"]
|
24
|
+
}
|
25
|
+
var versionBox = new curvyCorners(settings, document.getElementById("version"));
|
26
|
+
versionBox.applyCornersToAll();
|
27
|
+
}
|
28
|
+
</script>
|
29
|
+
</head>
|
30
|
+
<body>
|
31
|
+
<div id="main">
|
32
|
+
|
33
|
+
<h1><%= title %></h1>
|
34
|
+
<div id="version" class="clickable" onclick='document.location = "<%= download %>"; return false'>
|
35
|
+
<p>Get Version</p>
|
36
|
+
<a href="<%= download %>" class="numbers"><%= version %></a>
|
37
|
+
</div>
|
38
|
+
<%= body %>
|
39
|
+
<p class="coda">
|
40
|
+
<a href="mailto:drnicwilliams@gmail.com">Dr Nic</a>, <%= modified.pretty %><br>
|
41
|
+
Theme extended from <a href="http://rb2js.rubyforge.org/">Paul Battley</a>
|
42
|
+
</p>
|
43
|
+
</div>
|
44
|
+
|
45
|
+
<!-- insert site tracking codes here, like Google Urchin -->
|
46
|
+
|
47
|
+
</body>
|
48
|
+
</html>
|
metadata
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.2
|
3
3
|
specification_version: 1
|
4
4
|
name: decisiontree
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2007-07-07 00:00:00 -04:00
|
8
8
|
summary: ID3-based implementation of the M.L. Decision Tree algorithm
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- lib/decisiontree/id3_tree.rb
|
40
40
|
- examples/continuous-id3.rb
|
41
41
|
- examples/discrete-id3.rb
|
42
|
+
- examples/simple.rb
|
42
43
|
- examples/data/continuous-test.txt
|
43
44
|
- examples/data/discrete-test.txt
|
44
45
|
- examples/data/continuous-training.txt
|
@@ -46,12 +47,28 @@ files:
|
|
46
47
|
- setup.rb
|
47
48
|
- test/test_helper.rb
|
48
49
|
- test/test_decisiontree.rb
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
- scripts/txt2html
|
51
|
+
- website/index.html
|
52
|
+
- website/index.txt
|
53
|
+
- website/javascripts/rounded_corners_lite.inc.js
|
54
|
+
- website/stylesheets/screen.css
|
55
|
+
- website/template.rhtml
|
56
|
+
test_files:
|
57
|
+
- test/test_decisiontree.rb
|
58
|
+
- test/test_helper.rb
|
59
|
+
rdoc_options:
|
60
|
+
- --main
|
61
|
+
- README.txt
|
62
|
+
extra_rdoc_files:
|
63
|
+
- CHANGELOG.txt
|
64
|
+
- History.txt
|
65
|
+
- Manifest.txt
|
66
|
+
- README.txt
|
67
|
+
- examples/data/continuous-test.txt
|
68
|
+
- examples/data/discrete-test.txt
|
69
|
+
- examples/data/continuous-training.txt
|
70
|
+
- examples/data/discrete-training.txt
|
71
|
+
- website/index.txt
|
55
72
|
executables: []
|
56
73
|
|
57
74
|
extensions: []
|