-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
64 lines (51 loc) · 1.48 KB
/
main.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
package stringsem
import (
language "cloud.google.com/go/language/apiv1"
"context"
languagepb "google.golang.org/genproto/googleapis/cloud/language/v1"
"log"
"fmt"
)
func IsGood(msg string) bool {
score, magnitude := getSentiment(msg)
fmt.Printf("Score: %f, Magnitude: %f\n",score,magnitude)
if score >= 0.5 && magnitude >= 0.5 {
fmt.Println("good")
return true
} else {
fmt.Println("bad")
return false
}
}
func IsHappy(msg []string) bool {
happyScore := float32(0.0)
for _, item := range msg {
score, magnitude := getSentiment(item)
happyScore += score*magnitude
}
if happyScore >= 0 {
return true
} else {
return false
}
}
func getSentiment(msg string) (float32, float32) {
ctx := context.Background()
client, err := language.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v",err)
}
sentiment, err := client.AnalyzeSentiment(ctx, &languagepb.AnalyzeSentimentRequest{
Document: &languagepb.Document{
Source: &languagepb.Document_Content{
Content: msg,
},
Type: languagepb.Document_PLAIN_TEXT,
},
EncodingType: languagepb.EncodingType_UTF8,
})
if err != nil {
log.Fatalf("Failed to analyze msg: %v", err)
}
return sentiment.DocumentSentiment.Score, sentiment.DocumentSentiment.Magnitude
}