-
Notifications
You must be signed in to change notification settings - Fork 1
/
measure.go
51 lines (45 loc) · 1.07 KB
/
measure.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
package sentimental
import (
"github.com/k4orta/sentimental/afinn"
)
func Measure(raw string) int {
lookup := afinn.GetAFINN()
score := 0
for _, token := range tokenize(raw) {
if points, exists := (*lookup)[token]; exists {
score += points
}
}
return score
}
type Stat struct {
Score int
Comparative float32
Tokens []string
WordCount int
Positive []string
Negative []string
}
// A version of the measure function that returns more data
func FullMeasure(raw string) Stat {
lookup := afinn.GetAFINN()
result := Stat{}
tokens := tokenize(raw)
for _, token := range tokens {
if points, exists := (*lookup)[token]; exists {
if points > 0 {
result.Positive = append(result.Positive, token)
} else if points < 0 {
result.Negative = append(result.Negative, token)
}
result.Score += points
result.Tokens = append(result.Tokens, token)
}
}
result.WordCount = len(tokens)
// Make sure we don't divide by zero
if len(result.Tokens) > 0 {
result.Comparative = float32(result.Score) / float32(len(result.Tokens))
}
return result
}