Class: PositronicBrain::Classifier::Base
- Inherits:
-
Base
- Object
- Base
- PositronicBrain::Classifier::Base
show all
- Defined in:
- lib/positronic_brain/classifier/classifier.rb
Instance Attribute Summary
Attributes inherited from Base
#namespace, #persistence
Instance Method Summary
collapse
-
#category_given_item_product(category, item, options = {}) ⇒ Object
-
#classify(item, default = nil) ⇒ Object
-
#extract_features(item) ⇒ Object
-
#initialize(namespace, options = {}, &extract_block) ⇒ Base
constructor
-
#item_given_category_product(item, category, options = {}) ⇒ Object
-
#normalized_prob_category_given_feature(category, feature, options = {}) ⇒ Object
-
#prob_category(category) ⇒ Object
-
#prob_category_given_feature(category, feature, options = {}) ⇒ Object
-
#prob_feature_given_category(feature, category, options = {}) ⇒ Object
-
#train(item, category) ⇒ Object
Methods inherited from Base
#dump_path, initialize_persistence, #inspect, persistence, persistence_class, persistence_options
Constructor Details
#initialize(namespace, options = {}, &extract_block) ⇒ Base
Returns a new instance of Base.
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 14
def initialize(namespace, options = {}, &)
options = options.dup
@assumed_probability = options.delete(:assumed_probability) || 0.5
@default_category = options.delete(:default_category)
@minimum_score = options.delete(:minimum_score) || 0.0
@assumed_weight = options.delete(:assumed_weight) || 1
@extract_block = if block_given?
super namespace, options
end
|
Instance Method Details
#category_given_item_product(category, item, options = {}) ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 57
def category_given_item_product(category, item, options={})
prod = 1.0
(item).each do |feature|
prod *= normalized_prob_category_given_feature(category, feature, options)
end
prod
end
|
#classify(item, default = nil) ⇒ Object
31
32
33
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 31
def classify(item, default = nil)
raise NotImplementedError
end
|
27
28
29
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 27
def (item)
item.downcase.split(/\P{Word}+/).uniq
end
|
#item_given_category_product(item, category, options = {}) ⇒ Object
49
50
51
52
53
54
55
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 49
def item_given_category_product(item, category, options={})
prod = 1.0
(item).each do |feature|
prod *= prob_feature_given_category(feature, category, options)
end
prod
end
|
#normalized_prob_category_given_feature(category, feature, options = {}) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 78
def normalized_prob_category_given_feature(category, feature, options={})
feature_given_category = prob_feature_given_category feature, category
all_feature_given_categories = categories.map do |cat|
prob_feature_given_category feature, cat
end.sum
if options[:weighted]
fc = feature_count feature
weighted feature_given_category/all_feature_given_categories, fc
else
return 0.0 if fc == 0.0
feature_given_category/all_feature_given_categories
end
end
|
#prob_category(category) ⇒ Object
42
43
44
45
46
47
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 42
def prob_category(category)
tc = total_count
return 0.0 if total_count == 0.0
category_count(category)/tc
end
|
#prob_category_given_feature(category, feature, options = {}) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 93
def prob_category_given_feature(category, feature, options={})
fc = feature_count feature
feature_in_category = feature_in_category_count feature, category
if options[:weighted]
weighted feature_in_category/fc, fc
else
return 0.0 if fc == 0.0
feature_in_category/fc
end
end
|
#prob_feature_given_category(feature, category, options = {}) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 65
def prob_feature_given_category(feature, category, options={})
cc = category_count category
feature_in_category = feature_in_category_count feature, category
if options[:weighted]
fc = feature_count feature
weighted feature_in_category/cc, fc
else
return 0.0 if cc == 0.0
feature_in_category/cc
end
end
|
#train(item, category) ⇒ Object
35
36
37
38
39
40
|
# File 'lib/positronic_brain/classifier/classifier.rb', line 35
def train(item, category)
(item).each do |feature|
increment_feature feature, category
end
increment_category category
end
|