tonality_analyser 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +3 -2
- data/lib/tonality_analyser/engine.rb +10 -29
- data/lib/tonality_analyser/version.rb +1 -1
- data/spec/tonality_analyser_spec.rb +12 -0
- metadata +1 -1
data/Rakefile
CHANGED
@@ -1,42 +1,29 @@
|
|
1
1
|
module TonalityAnalyser
|
2
2
|
|
3
|
-
# Refactor: work to Redis !
|
4
|
-
# Redis ! Redis ! Redis ! Redis ! Redis ! :)
|
5
3
|
class Engine
|
6
4
|
TONALITIES = [:pos, :neg]
|
7
5
|
attr_reader :counted_words, :probabilites
|
8
6
|
def initialize
|
9
|
-
@total_words = {}
|
10
|
-
@total_words[:all] = 0
|
11
|
-
@total_words[:pos] = 0
|
12
|
-
@total_words[:neg] = 0
|
13
7
|
@counted_words = {}
|
14
8
|
@counted_words[:pos] = {}
|
15
9
|
@counted_words[:neg] = {}
|
16
10
|
@probabilites = {}
|
17
11
|
@probabilites[:pos] = {}
|
18
12
|
@probabilites[:neg] = {}
|
19
|
-
@spec_probabilites = {}
|
20
|
-
@spec_probabilites[:pos] = {}
|
21
|
-
@spec_probabilites[:neg] = {}
|
22
13
|
end
|
23
14
|
def train(words, tonality)
|
24
15
|
raise "Invalid tonality '#{tonality}'" unless TONALITIES.include?(tonality)
|
25
16
|
words.split.each do |w|
|
26
17
|
word = Helpers::Text.normalize(w)
|
27
|
-
@total_words[:all] += 1
|
28
18
|
@counted_words[tonality][word] = @counted_words[tonality].include?(word) ? @counted_words[tonality][word]+1 : 1
|
29
19
|
end
|
30
20
|
end
|
31
21
|
def compute_probabilities!
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
@counted_words[:neg].each do |word, count|
|
38
|
-
@probabilites[:neg][word] = @counted_words[:neg][word].to_f / (@counted_words[:pos][word].to_f + @counted_words[:neg][word].to_f)
|
39
|
-
@spec_probabilites[:neg][word] = @probabilites[:neg][word]
|
22
|
+
TONALITIES.each {|t| compute_probabilities_for(t) }
|
23
|
+
end
|
24
|
+
def compute_probabilities_for(tonality)
|
25
|
+
@counted_words[tonality].each do |word, count|
|
26
|
+
@probabilites[tonality][word] = @counted_words[tonality][word].to_f / TONALITIES.each.inject(0) { |sum, t| sum += @counted_words[t][word].to_f }
|
40
27
|
end
|
41
28
|
end
|
42
29
|
def analysis(text, tonality)
|
@@ -44,18 +31,12 @@ module TonalityAnalyser
|
|
44
31
|
|
45
32
|
words = Helpers::Text.clean_words_from(text)
|
46
33
|
words.each do |word|
|
47
|
-
@
|
48
|
-
num *=
|
49
|
-
|
50
|
-
|
51
|
-
words.each do |word|
|
52
|
-
@probabilites[tonality][word] ||= 0.01
|
53
|
-
den1 *= @probabilites[tonality][word]
|
54
|
-
end
|
55
|
-
words.each do |word|
|
56
|
-
den2 *= (1 - @probabilites[tonality][word])
|
34
|
+
p = @probabilites[tonality][word] || 0.01
|
35
|
+
num *= p
|
36
|
+
den1 *= p
|
37
|
+
den2 *= (1 - p)
|
57
38
|
end
|
58
|
-
proba_pol = num / (den1 + den2)
|
39
|
+
proba_pol = num*0.5 / (den1 + den2)
|
59
40
|
proba_pol = 0.0 if proba_pol.nan?
|
60
41
|
proba_pol
|
61
42
|
end
|
@@ -12,4 +12,16 @@ describe TonalityAnalyser::Engine do
|
|
12
12
|
e.tonality('want') == :pos
|
13
13
|
e.tonality('Thanks') == :pos
|
14
14
|
end
|
15
|
+
|
16
|
+
it 'propose tonality' do
|
17
|
+
e = TonalityAnalyser::Engine.new
|
18
|
+
e.train "c'est super !", :pos
|
19
|
+
e.train "encore un beau ", :pos
|
20
|
+
e.train "nul nul nul", :neg
|
21
|
+
e.train "pas tres beau projet", :neg
|
22
|
+
e.compute_probabilities!
|
23
|
+
e.tonality('super beau projet').should == :pos
|
24
|
+
e.tonality('c\'est nul').should == :neg
|
25
|
+
end
|
26
|
+
|
15
27
|
end
|