Class: Groupie
- Inherits:
-
Object
show all
- Defined in:
- lib/groupie.rb,
lib/groupie/group.rb,
lib/groupie/core_ext/string.rb
Defined Under Namespace
Modules: CoreExt
Classes: Group
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Groupie.
7
8
9
|
# File 'lib/groupie.rb', line 7
def initialize
@groups = {}
end
|
Class Method Details
.version ⇒ Object
83
84
85
|
# File 'lib/groupie.rb', line 83
def self.version
File.read(File.join(File.dirname(File.expand_path(__FILE__)), "..", "VERSION")).strip
end
|
Instance Method Details
#[](group) ⇒ Object
11
12
13
|
# File 'lib/groupie.rb', line 11
def [](group)
@groups[group] ||= Group.new(group)
end
|
#classify(entry, strategy = :sum) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/groupie.rb', line 24
def classify(entry, strategy=:sum)
results = {}
total_count = @groups.inject(0) do |sum, name_group|
group = name_group.last
count = group.count(entry)
case strategy
when :sum
sum += count
when :sqrt, :unique
sum += Math::sqrt(count)
when :log
sum += Math::log10(count) if count > 0
else
raise "Invalid strategy: #{strategy}"
end
next sum
end
return results if 0 == total_count
@groups.each do |name, group|
count = group.count(entry)
case strategy
when :sum
when :sqrt, :unique
count = Math::sqrt(count)
when :log
count = Math::log10(count) if count > 0
else
raise "Invalid strategy: #{strategy}"
end
results[name] = count > 0 ? count.to_f / total_count : 0.0
end
return results
end
|
#classify_text(words, strategy = :sum) ⇒ Object
Classify a text by taking the average of all word classifications.
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/groupie.rb', line 61
def classify_text(words, strategy=:sum)
hits = 0
if strategy==:unique
words = words & unique_words
end
group_score_sums = words.inject({}) do |results, word|
word_results = classify(word, strategy)
next results if word_results.empty?
hits += 1
results.merge(word_results) do |key, old, new|
old + new
end
end
averages={}
group_score_sums.each do |group, sum|
averages[group] = hits > 0 ? sum / hits : 0
end
averages
end
|
#unique_words ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/groupie.rb', line 15
def unique_words
@unique_words ||= (
total_count = @groups.values.map {|group| group.word_counts}.inject{|total, counts| total.merge(counts){|key,o,n| o+n}}
median_index = [total_count.values.size * 3 / 4 - 1, 1].max
median_frequency = total_count.values.sort[median_index]
total_count.select{|word, count| count <= median_frequency}.map(&:first)
)
end
|