Class: NBClass::Classifier
- Inherits:
-
Object
- Object
- NBClass::Classifier
show all
- Defined in:
- lib/nb_class/classifier.rb
Constant Summary
collapse
- SMALL_PROBABILITY =
0.000001
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Classifier.
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/nb_class/classifier.rb', line 10
def initialize
@category_examples = {}
@occurrences = {}
@global_occurrences = {}
@probabilities = {}
@global_probabilities = {}
@category_occurrences = {}
@total_elements = 0
@total_phrases = 0;
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/nb_class/classifier.rb', line 103
def method_missing(name, *args, &block)
@category_examples[name] = PhraseArray.new
define_singleton_method name do
@category_examples[name]
end
public_send(name)
end
|
Instance Method Details
#categories ⇒ Object
21
22
23
|
# File 'lib/nb_class/classifier.rb', line 21
def categories
@category_examples.keys
end
|
#category_probability(category) ⇒ Object
84
85
86
|
# File 'lib/nb_class/classifier.rb', line 84
def category_probability(category)
@category_occurrences[category].to_f / @total_phrases.to_f
end
|
#category_probability_given_elements(params) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/nb_class/classifier.rb', line 88
def category_probability_given_elements(params)
category = params[:category]
elements = params[:elements]
probability = 1.0
elements.each do |element|
element_probability_given_category = element_probability_given_category(category: category, element: element)
element_global_probability = element_probability(element)
element_probability = element_probability_given_category / element_global_probability
probability *= element_probability
end
category_probability = category_probability(category)
probability *= category_probability
probability
end
|
#classify(elements) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/nb_class/classifier.rb', line 39
def classify(elements)
elements = Utils.break_phrase_in_word_array(elements)
max_category = nil
max_probability = 0.0
@occurrences.keys.each do |key|
probability = category_probability_given_elements(category: key,elements: elements)
if probability > max_probability
max_probability = probability
max_category = key
end
end
max_category
end
|
#element_occurrence(params) ⇒ Object
53
54
55
56
57
58
59
60
61
|
# File 'lib/nb_class/classifier.rb', line 53
def element_occurrence(params)
category = params[:category]
element = params[:element]
if category
@occurrences[category][element]
else
@global_occurrences[element]
end
end
|
#element_probability(element) ⇒ Object
75
76
77
78
79
80
81
82
|
# File 'lib/nb_class/classifier.rb', line 75
def element_probability(element)
element_occurrence = @global_occurrences[element]
unless element_occurrence.nil?
element_occurrence.to_f / @total_elements
else
SMALL_PROBABILITY
end
end
|
#element_probability_given_category(params) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/nb_class/classifier.rb', line 63
def element_probability_given_category(params)
category = params[:category]
element = params[:element]
element_occurrence_in_category = @occurrences[category][element]
total_elements_in_category = @occurrences[category].map{ |item| item[1] }.inject{ |sum, item| sum + item }
unless element_occurrence_in_category.nil?
element_occurrence_in_category.to_f / total_elements_in_category.to_f
else
SMALL_PROBABILITY
end
end
|
#train ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/nb_class/classifier.rb', line 25
def train
@category_examples.keys.each do |category|
@category_examples[category].each do |phrase|
phrase.each do |word|
count_element_occurrence_in_category(category,word)
count_element_occurrence_in_global(word)
@total_elements += 1
end
count_category_occurrence(category)
@total_phrases += 1
end
end
end
|