forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (45 loc) · 1.34 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
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// [START language_quickstart]
// Sample language-quickstart uses the Google Cloud Natural API to analyze the
// sentiment of "Hello, world!".
package main
import (
"fmt"
"log"
// Imports the Google Cloud Natural Language API client package.
language "cloud.google.com/go/language/apiv1"
"golang.org/x/net/context"
languagepb "google.golang.org/genproto/googleapis/cloud/language/v1"
)
func main() {
ctx := context.Background()
// Creates a client.
client, err := language.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Sets the text to analyze.
text := "Hello, world!"
// Detects the sentiment of the text.
sentiment, err := client.AnalyzeSentiment(ctx, &languagepb.AnalyzeSentimentRequest{
Document: &languagepb.Document{
Source: &languagepb.Document_Content{
Content: text,
},
Type: languagepb.Document_PLAIN_TEXT,
},
EncodingType: languagepb.EncodingType_UTF8,
})
if err != nil {
log.Fatalf("Failed to analyze text: %v", err)
}
fmt.Printf("Text: %v\n", text)
if sentiment.DocumentSentiment.Score >= 0 {
fmt.Println("Sentiment: positive")
} else {
fmt.Println("Sentiment: negative")
}
}
// [END language_quickstart]