classifier 2.0.0 → 2.1.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.
- checksums.yaml +4 -4
- data/CLAUDE.md +23 -13
- data/README.md +82 -67
- data/ext/classifier/classifier_ext.c +25 -0
- data/ext/classifier/extconf.rb +15 -0
- data/ext/classifier/linalg.h +64 -0
- data/ext/classifier/matrix.c +387 -0
- data/ext/classifier/svd.c +208 -0
- data/ext/classifier/vector.c +319 -0
- data/lib/classifier/bayes.rb +253 -33
- data/lib/classifier/errors.rb +16 -0
- data/lib/classifier/extensions/vector.rb +12 -4
- data/lib/classifier/lsi/content_node.rb +5 -5
- data/lib/classifier/lsi.rb +439 -141
- data/lib/classifier/storage/base.rb +50 -0
- data/lib/classifier/storage/file.rb +51 -0
- data/lib/classifier/storage/memory.rb +49 -0
- data/lib/classifier/storage.rb +9 -0
- data/lib/classifier.rb +2 -0
- data/sig/vendor/json.rbs +4 -0
- data/sig/vendor/mutex_m.rbs +16 -0
- data/test/test_helper.rb +2 -0
- metadata +36 -5
- data/lib/classifier/extensions/vector_serialize.rb +0 -18
|
@@ -21,12 +21,20 @@ end
|
|
|
21
21
|
class Vector
|
|
22
22
|
EPSILON = 1e-10
|
|
23
23
|
|
|
24
|
+
# Cache magnitude since Vector is immutable after creation
|
|
25
|
+
# Note: We undefine the matrix gem's normalize method first, then redefine it
|
|
26
|
+
# to provide a more robust implementation that handles zero vectors
|
|
27
|
+
undef_method :normalize if method_defined?(:normalize)
|
|
28
|
+
|
|
24
29
|
def magnitude
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
sum_of_squares
|
|
30
|
+
# Cache magnitude since Vector is immutable after creation
|
|
31
|
+
@magnitude ||= begin
|
|
32
|
+
sum_of_squares = 0.to_r
|
|
33
|
+
size.times do |i|
|
|
34
|
+
sum_of_squares += self[i]**2.to_r
|
|
35
|
+
end
|
|
36
|
+
Math.sqrt(sum_of_squares.to_f)
|
|
28
37
|
end
|
|
29
|
-
Math.sqrt(sum_of_squares.to_f)
|
|
30
38
|
end
|
|
31
39
|
|
|
32
40
|
def normalize
|
|
@@ -50,8 +50,8 @@ module Classifier
|
|
|
50
50
|
#
|
|
51
51
|
# @rbs (WordList) -> untyped
|
|
52
52
|
def raw_vector_with(word_list)
|
|
53
|
-
vec = if Classifier::LSI.
|
|
54
|
-
|
|
53
|
+
vec = if Classifier::LSI.native_available?
|
|
54
|
+
Classifier::LSI.vector_class.alloc(word_list.size)
|
|
55
55
|
else
|
|
56
56
|
Array.new(word_list.size, 0)
|
|
57
57
|
end
|
|
@@ -61,8 +61,8 @@ module Classifier
|
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
# Perform the scaling transform
|
|
64
|
-
total_words = Classifier::LSI.
|
|
65
|
-
vec_array = Classifier::LSI.
|
|
64
|
+
total_words = Classifier::LSI.native_available? ? vec.sum : vec.sum_with_identity
|
|
65
|
+
vec_array = Classifier::LSI.native_available? ? vec.to_a : vec
|
|
66
66
|
total_unique_words = vec_array.count { |word| word != 0 }
|
|
67
67
|
|
|
68
68
|
# Perform first-order association transform if this vector has more
|
|
@@ -84,7 +84,7 @@ module Classifier
|
|
|
84
84
|
vec = vec.collect { |val| Math.log(val + 1) / divisor }
|
|
85
85
|
end
|
|
86
86
|
|
|
87
|
-
if Classifier::LSI.
|
|
87
|
+
if Classifier::LSI.native_available?
|
|
88
88
|
@raw_norm = vec.normalize
|
|
89
89
|
@raw_vector = vec
|
|
90
90
|
else
|