forked from GoogleCloudPlatform/golang-samples
/
analyze.go
104 lines (90 loc) · 2.55 KB
/
analyze.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
94
95
96
97
98
99
100
101
102
103
104
// 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.
// Command analyze performs sentiment, entity, and syntax analysis
// on a string of text via the Cloud Natural Language API.
package main
import (
"fmt"
"log"
"os"
"strings"
"github.com/golang/protobuf/proto"
"golang.org/x/net/context"
// [START imports]
language "cloud.google.com/go/language/apiv1"
languagepb "google.golang.org/genproto/googleapis/cloud/language/v1"
// [END imports]
)
func main() {
if len(os.Args) < 2 {
usage("Missing command.")
}
// [START init]
ctx := context.Background()
client, err := language.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
// [END init]
text := strings.Join(os.Args[2:], " ")
if text == "" {
usage("Missing text.")
}
switch os.Args[1] {
case "entities":
printResp(analyzeEntities(ctx, client, text))
case "sentiment":
printResp(analyzeSentiment(ctx, client, text))
case "syntax":
printResp(analyzeSyntax(ctx, client, text))
default:
usage("Unknown command.")
}
}
func usage(msg string) {
fmt.Fprintln(os.Stderr, msg)
fmt.Fprintln(os.Stderr, "usage: analyze [entities|sentiment|syntax] <text>")
os.Exit(2)
}
func analyzeEntities(ctx context.Context, client *language.Client, text string) (*languagepb.AnalyzeEntitiesResponse, error) {
return client.AnalyzeEntities(ctx, &languagepb.AnalyzeEntitiesRequest{
Document: &languagepb.Document{
Source: &languagepb.Document_Content{
Content: text,
},
Type: languagepb.Document_PLAIN_TEXT,
},
EncodingType: languagepb.EncodingType_UTF8,
})
}
func analyzeSentiment(ctx context.Context, client *language.Client, text string) (*languagepb.AnalyzeSentimentResponse, error) {
return client.AnalyzeSentiment(ctx, &languagepb.AnalyzeSentimentRequest{
Document: &languagepb.Document{
Source: &languagepb.Document_Content{
Content: text,
},
Type: languagepb.Document_PLAIN_TEXT,
},
})
}
func analyzeSyntax(ctx context.Context, client *language.Client, text string) (*languagepb.AnnotateTextResponse, error) {
return client.AnnotateText(ctx, &languagepb.AnnotateTextRequest{
Document: &languagepb.Document{
Source: &languagepb.Document_Content{
Content: text,
},
Type: languagepb.Document_PLAIN_TEXT,
},
Features: &languagepb.AnnotateTextRequest_Features{
ExtractSyntax: true,
},
EncodingType: languagepb.EncodingType_UTF8,
})
}
func printResp(v proto.Message, err error) {
if err != nil {
log.Fatal(err)
}
proto.MarshalText(os.Stdout, v)
}