Skip to content

Commit 730fc35

Browse files
authored
Merge pull request #1 from dinopuguh/v2
🚀 Gosentiwordnet v2 release
2 parents 9db2044 + a7154f9 commit 730fc35

9 files changed

+171
-8
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage.txt
2+
coverage.out
3+
coverage.html

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: go
2+
go:
3+
- 1.14.x
4+
5+
script:
6+
- go test -v -coverprofile=coverage.txt -covermode=count
7+
8+
after_success:
9+
- bash <(curl -s https://codecov.io/bash)
10+
- rm -rf coverage.txt

FUNDING.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# These are supported funding model platforms
2+
3+
github: [dinopuguh] # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel

README.md

+60-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,60 @@
1-
go-sentiwordnet
1+
# 💬 GoSentiwordnet
2+
3+
Sentiment analyzer using [sentiwordnet](https://github.com/aesuli/SentiWordNet) lexicon in Go. This library produce sentiment score for each word, including positive, negative, and objective score.
4+
5+
## ⚙ Installation
6+
7+
First of all, [download](https://golang.org/dl/) and install Go `1.14` or higher is required.
8+
9+
Install this library using the [`go get`](https://golang.org/cmd/go/#hdr-Add_dependencies_to_current_module_and_install_them) command:
10+
11+
```bash
12+
$ go get github.com/dinopuguh/gosentiwordnet
13+
```
14+
15+
16+
17+
## ⚡ Quickstart
18+
19+
```go
20+
package main
21+
22+
import (
23+
"fmt"
24+
25+
goswn "github.com/dinopuguh/gosentiwordnet"
26+
)
27+
28+
func main() {
29+
sa := goswn.New()
30+
31+
scores, exist := sa.GetSentimentScore("love", "v", "2")
32+
if exist {
33+
fmt.Println("💬 Sentiment score:", scores) // => 💬 Sentiment score: {1 0 0}
34+
}
35+
}
36+
```
37+
38+
The `GetSentimentScore` required 3 parameters(word, pos-tag, and word usage):
39+
40+
1. **Word**: the word want to process
41+
2. **POS tag**: part-of-speech tag of the word
42+
3. **Word usage**: 1 for most common usage and a higher number would indicate lesser common usages
43+
44+
45+
46+
## 👍 Contributing
47+
48+
If you want to say **thank you** and/or support the active development of `Gosentiwordnet`:
49+
50+
1. Add a [GitHub Star](https://github.com/dinopuguh/gosentiwordnet/stargazers) to the project.
51+
2. Write a review or tutorial on [Medium](https://medium.com/), [Dev.to](https://dev.to/) or personal blog.
52+
3. Be a part of our [sponsors](https://github.com/sponsors/dinopuguh) to support this project.
53+
54+
55+
56+
## 💻 Contributors
57+
58+
- Dino Puguh (initial works)
59+
60+
Open for any pull requests to develop this project.

example/main.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
goswn "github.com/dinopuguh/gosentiwordnet"
7+
)
8+
9+
// Token represent the required parameter for using gosentiwordnet
10+
type Token struct {
11+
Word string // the word want to process
12+
PosTag string // part-of-speech tag of word
13+
Usage string // word usage (1 for most common usage and a higher number would indicate lesser common usages)
14+
}
15+
16+
func main() {
17+
sa := goswn.New()
18+
19+
tokens := []Token{
20+
Token{Word: "love", PosTag: "v", Usage: "2"},
21+
Token{Word: "neat", PosTag: "a", Usage: "4"},
22+
Token{Word: "overacting", PosTag: "n", Usage: "1"},
23+
}
24+
25+
for _, token := range tokens {
26+
scores, exist := sa.GetSentimentScore(token.Word, token.PosTag, token.Usage)
27+
if exist {
28+
fmt.Printf("💬 Sentiment score of %s: %v\n", token.Word, scores)
29+
// 💬 Sentiment score: {positive_score negative_score objective_score}
30+
}
31+
}
32+
}

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/dinopuguh/gosentiwordnet
2+
3+
go 1.14
4+
5+
require github.com/stretchr/testify v1.6.1

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
6+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
7+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
10+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

gosentiwordnet.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import (
1313

1414
const sentiWordnetAssetName = "rawdata/SentiWordNet_3.0.0.txt"
1515

16+
// SentimentAnalyzer represent the sentiment analyzer with sentiwordnet lexicon
1617
type SentimentAnalyzer struct {
1718
Lexicon map[string]Sentiment
1819
}
1920

21+
// Sentiment reprensent sentiment score for each word
22+
// containing positive, negative and objective
2023
type Sentiment struct {
2124
Positive float64
2225
Negative float64
@@ -56,13 +59,12 @@ func (sa *SentimentAnalyzer) generateLexicon() {
5659

5760
line++
5861
}
59-
60-
if err := scanner.Err(); err != nil {
61-
log.Fatal(err.Error())
62-
}
6362
}
6463

65-
func (sa *SentimentAnalyzer) GetSentimentScore(word string, posTag string, usage string) (bool, Sentiment) {
64+
// GetSentimentScore count the sentiment score of word based on POS tag and the word usage.
65+
// POS tag: part-of-speech tag of word
66+
// Word usage: 1 for most common usage and a higher number would indicate lesser common usages
67+
func (sa *SentimentAnalyzer) GetSentimentScore(word string, posTag string, usage string) (Sentiment, bool) {
6668
var result Sentiment
6769

6870
posTag = "(" + posTag + ")"
@@ -78,10 +80,11 @@ func (sa *SentimentAnalyzer) GetSentimentScore(word string, posTag string, usage
7880
}
7981
}
8082

81-
return match, result
83+
return result, match
8284
}
8385

84-
func NewGoSentiwordnet() *SentimentAnalyzer {
86+
// New generate the sentiment analyzer with sentiwordnet lexicon
87+
func New() *SentimentAnalyzer {
8588
var sa SentimentAnalyzer
8689

8790
sa.generateLexicon()

gosentiwordnet_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package gosentiwordnet_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/dinopuguh/gosentiwordnet"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type SentimentTestCase struct {
11+
Word string
12+
PosTag string
13+
Usage string
14+
Scores gosentiwordnet.Sentiment
15+
}
16+
17+
func generateTestCases() []SentimentTestCase {
18+
return []SentimentTestCase{
19+
SentimentTestCase{Word: "love", PosTag: "v", Usage: "2", Scores: gosentiwordnet.Sentiment{Positive: 1, Negative: 0, Objective: 0}},
20+
SentimentTestCase{Word: "neat", PosTag: "a", Usage: "4", Scores: gosentiwordnet.Sentiment{Positive: 0.625, Negative: 0, Objective: 0.375}},
21+
SentimentTestCase{Word: "overacting", PosTag: "n", Usage: "1", Scores: gosentiwordnet.Sentiment{Positive: 0, Negative: 0.875, Objective: 0.125}},
22+
SentimentTestCase{Word: "finely", PosTag: "r", Usage: "2", Scores: gosentiwordnet.Sentiment{Positive: 0.625, Negative: 0, Objective: 0.375}},
23+
}
24+
}
25+
26+
func TestSentimentAnalysis(t *testing.T) {
27+
sa := gosentiwordnet.New()
28+
for _, testCase := range generateTestCases() {
29+
scores, match := sa.GetSentimentScore(testCase.Word, testCase.PosTag, testCase.Usage)
30+
if match {
31+
assert.Equalf(t, testCase.Scores, scores, testCase.Word)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)