Sha256: 7b7b1d92ec4da81e3f1e8a134e34c7bd67ff12ed96a37347c288117a2f3cc083

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

lib_dir = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
require 'groupie/group'
require 'groupie/core_ext/string'

class Groupie
  def initialize
    @groups = {}
  end

  def [](group)
    @groups[group] ||= Group.new(group)
  end

  def classify(entry)
    results = {}
    total_count = @groups.inject(0) do |sum, name_group|
      group = name_group.last
      sum + group.count(entry)
    end
    return results if 0 == total_count

    @groups.each do |name, group|
      count = group.count(entry)
      results[name] = count > 0 ? count.to_f / total_count : 0.0
    end
    return results
  end

  # Classify a text by taking the average of all word classifications.
  def classify_text(words)
    group_score_sums = words.inject({}) do |results, word|
      word_results = classify(word)
      results.merge(word_results) do |key, old, new|
        old + new
      end
    end

    words_count = words.size.to_f
    averages={}
    group_score_sums.each do |group, sum|
      averages[group] = sum / words_count
    end

    averages
  end
  
  def self.version
    File.read(File.join(File.dirname(File.expand_path(__FILE__)), "..", "VERSION")).strip
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
groupie-0.1.1 lib/groupie.rb
groupie-0.1.0 lib/groupie.rb