Class: Hal4R
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/hal4r/vector.rb,
lib/hal4r.rb,
lib/hal4r/matrix.rb,
lib/hal4r/version.rb
Overview
–
hal4r – Hyperspace analogue to language for Ruby #
Copyright © 2015 Jens Wille #
Authors: #
Jens Wille <[email protected]> #
#
hal4r is free software; you can redistribute it and/or modify it under the # terms of the GNU Affero General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #
hal4r is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for # more details. #
You should have received a copy of the GNU Affero General Public License # along with hal4r. If not, see <www.gnu.org/licenses/>. #
++
Defined Under Namespace
Modules: Version
Classes: Matrix, Vector
Constant Summary
collapse
- DEFAULT_WINDOW_SIZE =
10
- VERSION =
Version.to_s
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#<<(term) ⇒ Object
-
#add(terms) ⇒ Object
-
#each_distance(norm = true, dimension = 2) ⇒ Object
-
#each_norm(&block) ⇒ Object
(also: #each)
-
#each_vector(norm = false) ⇒ Object
-
#euclidean(term1, term2, norm = true) ⇒ Object
-
#initialize(terms = [], window_size = nil) ⇒ Hal4R
constructor
-
#inspect ⇒ Object
-
#manhattan(term1, term2, norm = true) ⇒ Object
(also: #cityblock)
-
#minkowski(term1, term2, dimension, norm = true) ⇒ Object
(also: #distance)
-
#norm(term) ⇒ Object
-
#related(term, num = window_size, dimension = 2) ⇒ Object
-
#reset(window_size = window_size()) ⇒ Object
-
#to_a(norm = true) ⇒ Object
-
#to_s ⇒ Object
-
#vector(term, norm = false) ⇒ Object
(also: #[])
Constructor Details
#initialize(terms = [], window_size = nil) ⇒ Hal4R
Returns a new instance of Hal4R.
42
43
44
45
|
# File 'lib/hal4r.rb', line 42
def initialize(terms = [], window_size = nil)
reset(window_size)
add(terms)
end
|
Instance Attribute Details
#window_size ⇒ Object
Returns the value of attribute window_size.
47
48
49
|
# File 'lib/hal4r.rb', line 47
def window_size
@window_size
end
|
Instance Method Details
#<<(term) ⇒ Object
56
57
58
59
60
61
62
63
64
|
# File 'lib/hal4r.rb', line 56
def <<(term)
row = @matrix.get(term_index = @idmap[term])
@window.each_with_index { |index, weight|
row[index] += weight + 1 if index
}.insert(-1, term_index).shift
self
end
|
#add(terms) ⇒ Object
66
67
68
69
|
# File 'lib/hal4r.rb', line 66
def add(terms)
terms.each { |term| self << term }
self
end
|
#each_distance(norm = true, dimension = 2) ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/hal4r.rb', line 95
def each_distance(norm = true, dimension = 2)
return enum_for(:each_distance, norm, dimension) unless block_given?
terms.combination(2) { |t| yield *t.sort!, minkowski(*t, dimension, norm) }
self
end
|
#each_norm(&block) ⇒ Object
Also known as:
each
89
90
91
|
# File 'lib/hal4r.rb', line 89
def each_norm(&block)
each_vector(true, &block)
end
|
#each_vector(norm = false) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/hal4r.rb', line 81
def each_vector(norm = false)
return enum_for(:each_vector, norm) unless block_given?
@idmap.each_value { |index| yield vector_i(index, norm).to_a }
self
end
|
#euclidean(term1, term2, norm = true) ⇒ Object
114
115
116
|
# File 'lib/hal4r.rb', line 114
def euclidean(term1, term2, norm = true)
minkowski(term1, term2, 2, norm)
end
|
#inspect ⇒ Object
141
142
143
144
145
|
# File 'lib/hal4r.rb', line 141
def inspect
'#<%s:0x%x @window_size=%p, @size=%p>' % [
self.class, object_id, window_size, size
]
end
|
#manhattan(term1, term2, norm = true) ⇒ Object
Also known as:
cityblock
118
119
120
|
# File 'lib/hal4r.rb', line 118
def manhattan(term1, term2, norm = true)
minkowski(term1, term2, 1, norm)
end
|
#minkowski(term1, term2, dimension, norm = true) ⇒ Object
Also known as:
distance
107
108
109
110
|
# File 'lib/hal4r.rb', line 107
def minkowski(term1, term2, dimension, norm = true)
[term1, term2].map { |term| vector(term, norm).vector }
.inject(:-).abs.to_f.pow(dimension).sum ** 1.fdiv(dimension)
end
|
#norm(term) ⇒ Object
77
78
79
|
# File 'lib/hal4r.rb', line 77
def norm(term)
vector(term, true)
end
|
103
104
105
|
# File 'lib/hal4r.rb', line 103
def related(term, num = window_size, dimension = 2)
(terms - [term]).sort_by { |t| minkowski(term, t, dimension) }[0, num]
end
|
#reset(window_size = window_size()) ⇒ Object
49
50
51
52
53
54
|
# File 'lib/hal4r.rb', line 49
def reset(window_size = window_size())
@idmap, @matrix, @window = Hash.idmap(-1), Matrix.new,
Array.new(@window_size = window_size || DEFAULT_WINDOW_SIZE)
self
end
|
#to_a(norm = true) ⇒ Object
124
125
126
|
# File 'lib/hal4r.rb', line 124
def to_a(norm = true)
norm ? each_norm.to_a : each_vector.to_a
end
|
#to_s ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/hal4r.rb', line 128
def to_s
cols = [terms.unshift(nil)]
@matrix.each_col.with_index { |col, index|
cols << [@idmap.key(index), *col] unless col.isnull? }
fmt = cols.map { |col|
"%#{col.map { |val| val.to_s.length }.max}s" }.join(' ') << $/
cols.first.each_index.map { |index|
fmt % cols.map { |col| col[index] } }.join
end
|
#vector(term, norm = false) ⇒ Object
Also known as:
[]
71
72
73
|
# File 'lib/hal4r.rb', line 71
def vector(term, norm = false)
vector_i(@idmap.fetch(term), norm)
end
|