Class: Inferx
- Inherits:
-
Object
- Object
- Inferx
- Defined in:
- lib/inferx.rb,
lib/inferx/version.rb,
lib/inferx/category.rb,
lib/inferx/categories.rb,
lib/inferx/category/complementary.rb
Defined Under Namespace
Classes: Categories, Category
Constant Summary collapse
- VERSION =
'0.2.4'
Instance Attribute Summary collapse
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
Instance Method Summary collapse
-
#classifications(words) ⇒ Hash<String, Float>
Get a score for each category according to a set of words.
-
#classify(words) ⇒ String
Classify words to any one category.
-
#initialize(options = {}) ⇒ Inferx
constructor
A new instance of Inferx.
-
#score(category, words) ⇒ Float
Get a score of a category according to a set of words.
Constructor Details
#initialize(options = {}) ⇒ Inferx
Returns a new instance of Inferx.
14 15 16 17 |
# File 'lib/inferx.rb', line 14 def initialize( = {}) @complementary = !![:complementary] @categories = Categories.new(Redis.new(), ) end |
Instance Attribute Details
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
19 20 21 |
# File 'lib/inferx.rb', line 19 def categories @categories end |
Instance Method Details
#classifications(words) ⇒ Hash<String, Float>
Get a score for each category according to a set of words.
39 40 41 42 |
# File 'lib/inferx.rb', line 39 def classifications(words) words = words.uniq Hash[@categories.map { |category| [category.name, score(category, words)] }] end |
#classify(words) ⇒ String
Classify words to any one category.
51 52 53 54 55 |
# File 'lib/inferx.rb', line 51 def classify(words) method_name = @complementary ? :min_by : :max_by category = classifications(words).__send__(method_name) { |score| score[1] } category ? category[0] : nil end |
#score(category, words) ⇒ Float
Get a score of a category according to a set of words.
26 27 28 29 30 31 |
# File 'lib/inferx.rb', line 26 def score(category, words) size = category.size.to_f return -Float::INFINITY unless size > 0 scores = category.scores(words) scores.inject(0.0) { |s, score| s + Math.log((score || 0.1) / size) } end |