-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gosentiwordnet.go
93 lines (72 loc) · 2.12 KB
/
gosentiwordnet.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package gosentiwordnet
import (
"bufio"
"bytes"
"log"
"strconv"
"strings"
"github.com/dinopuguh/gosentiwordnet/v2/data"
"github.com/dinopuguh/gosentiwordnet/v2/helpers"
)
const sentiWordnetAssetName = "rawdata/SentiWordNet_3.0.0.txt"
// SentimentAnalyzer represent the sentiment analyzer with sentiwordnet lexicon
type SentimentAnalyzer struct {
Lexicon map[string]Sentiment
}
// Sentiment reprensent sentiment score for each word
// containing positive, negative and objective
type Sentiment struct {
Positive float64
Negative float64
Objective float64
}
func (sa *SentimentAnalyzer) generateLexicon() {
sa.Lexicon = make(map[string]Sentiment)
asset, err := data.Asset(sentiWordnetAssetName)
if err != nil {
log.Panic(err.Error())
}
file := bytes.NewReader(asset)
scanner := bufio.NewScanner(file)
line := 1
for scanner.Scan() {
if line > 26 {
thisRawLine := scanner.Text()
thisSplitLine := strings.Split(thisRawLine, "\t")
posTag := thisSplitLine[0]
positive, _ := strconv.ParseFloat(thisSplitLine[2], 64)
negative, _ := strconv.ParseFloat(thisSplitLine[3], 64)
synsetTerms := thisSplitLine[4]
key := "(" + posTag + ") " + synsetTerms + " "
var sentiment Sentiment
sentiment.Positive = positive
sentiment.Negative = negative
sentiment.Objective = 1 - (positive + negative)
sa.Lexicon[key] = sentiment
}
line++
}
}
// GetSentimentScore count the sentiment score of word based on POS tag and the word usage.
// POS tag: part-of-speech tag of word
// Word usage: 1 for most common usage and a higher number would indicate lesser common usages
func (sa *SentimentAnalyzer) GetSentimentScore(word string, posTag string, usage string) (Sentiment, bool) {
var result Sentiment
posTag = "(" + posTag + ")"
synset := " " + word + "#" + usage + " "
match := false
for key, value := range sa.Lexicon {
if helpers.CheckSubstrings(key, posTag, synset) {
result = value
match = true
break
}
}
return result, match
}
// New generate the sentiment analyzer with sentiwordnet lexicon
func New() *SentimentAnalyzer {
var sa SentimentAnalyzer
sa.generateLexicon()
return &sa
}