You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

233 lines
8.6 KiB

  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Code generated by gapic-generator. DO NOT EDIT.
  15. package language
  16. import (
  17. "context"
  18. "time"
  19. gax "github.com/googleapis/gax-go/v2"
  20. "google.golang.org/api/option"
  21. "google.golang.org/api/transport"
  22. languagepb "google.golang.org/genproto/googleapis/cloud/language/v1beta2"
  23. "google.golang.org/grpc"
  24. "google.golang.org/grpc/codes"
  25. "google.golang.org/grpc/metadata"
  26. )
  27. // CallOptions contains the retry settings for each method of Client.
  28. type CallOptions struct {
  29. AnalyzeSentiment []gax.CallOption
  30. AnalyzeEntities []gax.CallOption
  31. AnalyzeEntitySentiment []gax.CallOption
  32. AnalyzeSyntax []gax.CallOption
  33. ClassifyText []gax.CallOption
  34. AnnotateText []gax.CallOption
  35. }
  36. func defaultClientOptions() []option.ClientOption {
  37. return []option.ClientOption{
  38. option.WithEndpoint("language.googleapis.com:443"),
  39. option.WithScopes(DefaultAuthScopes()...),
  40. }
  41. }
  42. func defaultCallOptions() *CallOptions {
  43. retry := map[[2]string][]gax.CallOption{
  44. {"default", "idempotent"}: {
  45. gax.WithRetry(func() gax.Retryer {
  46. return gax.OnCodes([]codes.Code{
  47. codes.DeadlineExceeded,
  48. codes.Unavailable,
  49. }, gax.Backoff{
  50. Initial: 100 * time.Millisecond,
  51. Max: 60000 * time.Millisecond,
  52. Multiplier: 1.3,
  53. })
  54. }),
  55. },
  56. }
  57. return &CallOptions{
  58. AnalyzeSentiment: retry[[2]string{"default", "idempotent"}],
  59. AnalyzeEntities: retry[[2]string{"default", "idempotent"}],
  60. AnalyzeEntitySentiment: retry[[2]string{"default", "idempotent"}],
  61. AnalyzeSyntax: retry[[2]string{"default", "idempotent"}],
  62. ClassifyText: retry[[2]string{"default", "idempotent"}],
  63. AnnotateText: retry[[2]string{"default", "idempotent"}],
  64. }
  65. }
  66. // Client is a client for interacting with Google Cloud Natural Language API.
  67. //
  68. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  69. type Client struct {
  70. // The connection to the service.
  71. conn *grpc.ClientConn
  72. // The gRPC API client.
  73. client languagepb.LanguageServiceClient
  74. // The call options for this service.
  75. CallOptions *CallOptions
  76. // The x-goog-* metadata to be sent with each request.
  77. xGoogMetadata metadata.MD
  78. }
  79. // NewClient creates a new language service client.
  80. //
  81. // Provides text analysis operations such as sentiment analysis and entity
  82. // recognition.
  83. func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
  84. conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
  85. if err != nil {
  86. return nil, err
  87. }
  88. c := &Client{
  89. conn: conn,
  90. CallOptions: defaultCallOptions(),
  91. client: languagepb.NewLanguageServiceClient(conn),
  92. }
  93. c.setGoogleClientInfo()
  94. return c, nil
  95. }
  96. // Connection returns the client's connection to the API service.
  97. func (c *Client) Connection() *grpc.ClientConn {
  98. return c.conn
  99. }
  100. // Close closes the connection to the API service. The user should invoke this when
  101. // the client is no longer required.
  102. func (c *Client) Close() error {
  103. return c.conn.Close()
  104. }
  105. // setGoogleClientInfo sets the name and version of the application in
  106. // the `x-goog-api-client` header passed on each request. Intended for
  107. // use by Google-written clients.
  108. func (c *Client) setGoogleClientInfo(keyval ...string) {
  109. kv := append([]string{"gl-go", versionGo()}, keyval...)
  110. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  111. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  112. }
  113. // AnalyzeSentiment analyzes the sentiment of the provided text.
  114. func (c *Client) AnalyzeSentiment(ctx context.Context, req *languagepb.AnalyzeSentimentRequest, opts ...gax.CallOption) (*languagepb.AnalyzeSentimentResponse, error) {
  115. ctx = insertMetadata(ctx, c.xGoogMetadata)
  116. opts = append(c.CallOptions.AnalyzeSentiment[0:len(c.CallOptions.AnalyzeSentiment):len(c.CallOptions.AnalyzeSentiment)], opts...)
  117. var resp *languagepb.AnalyzeSentimentResponse
  118. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  119. var err error
  120. resp, err = c.client.AnalyzeSentiment(ctx, req, settings.GRPC...)
  121. return err
  122. }, opts...)
  123. if err != nil {
  124. return nil, err
  125. }
  126. return resp, nil
  127. }
  128. // AnalyzeEntities finds named entities (currently proper names and common nouns) in the text
  129. // along with entity types, salience, mentions for each entity, and
  130. // other properties.
  131. func (c *Client) AnalyzeEntities(ctx context.Context, req *languagepb.AnalyzeEntitiesRequest, opts ...gax.CallOption) (*languagepb.AnalyzeEntitiesResponse, error) {
  132. ctx = insertMetadata(ctx, c.xGoogMetadata)
  133. opts = append(c.CallOptions.AnalyzeEntities[0:len(c.CallOptions.AnalyzeEntities):len(c.CallOptions.AnalyzeEntities)], opts...)
  134. var resp *languagepb.AnalyzeEntitiesResponse
  135. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  136. var err error
  137. resp, err = c.client.AnalyzeEntities(ctx, req, settings.GRPC...)
  138. return err
  139. }, opts...)
  140. if err != nil {
  141. return nil, err
  142. }
  143. return resp, nil
  144. }
  145. // AnalyzeEntitySentiment finds entities, similar to
  146. // [AnalyzeEntities][google.cloud.language.v1beta2.LanguageService.AnalyzeEntities]
  147. // in the text and analyzes sentiment associated with each entity and its
  148. // mentions.
  149. func (c *Client) AnalyzeEntitySentiment(ctx context.Context, req *languagepb.AnalyzeEntitySentimentRequest, opts ...gax.CallOption) (*languagepb.AnalyzeEntitySentimentResponse, error) {
  150. ctx = insertMetadata(ctx, c.xGoogMetadata)
  151. opts = append(c.CallOptions.AnalyzeEntitySentiment[0:len(c.CallOptions.AnalyzeEntitySentiment):len(c.CallOptions.AnalyzeEntitySentiment)], opts...)
  152. var resp *languagepb.AnalyzeEntitySentimentResponse
  153. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  154. var err error
  155. resp, err = c.client.AnalyzeEntitySentiment(ctx, req, settings.GRPC...)
  156. return err
  157. }, opts...)
  158. if err != nil {
  159. return nil, err
  160. }
  161. return resp, nil
  162. }
  163. // AnalyzeSyntax analyzes the syntax of the text and provides sentence boundaries and
  164. // tokenization along with part of speech tags, dependency trees, and other
  165. // properties.
  166. func (c *Client) AnalyzeSyntax(ctx context.Context, req *languagepb.AnalyzeSyntaxRequest, opts ...gax.CallOption) (*languagepb.AnalyzeSyntaxResponse, error) {
  167. ctx = insertMetadata(ctx, c.xGoogMetadata)
  168. opts = append(c.CallOptions.AnalyzeSyntax[0:len(c.CallOptions.AnalyzeSyntax):len(c.CallOptions.AnalyzeSyntax)], opts...)
  169. var resp *languagepb.AnalyzeSyntaxResponse
  170. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  171. var err error
  172. resp, err = c.client.AnalyzeSyntax(ctx, req, settings.GRPC...)
  173. return err
  174. }, opts...)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return resp, nil
  179. }
  180. // ClassifyText classifies a document into categories.
  181. func (c *Client) ClassifyText(ctx context.Context, req *languagepb.ClassifyTextRequest, opts ...gax.CallOption) (*languagepb.ClassifyTextResponse, error) {
  182. ctx = insertMetadata(ctx, c.xGoogMetadata)
  183. opts = append(c.CallOptions.ClassifyText[0:len(c.CallOptions.ClassifyText):len(c.CallOptions.ClassifyText)], opts...)
  184. var resp *languagepb.ClassifyTextResponse
  185. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  186. var err error
  187. resp, err = c.client.ClassifyText(ctx, req, settings.GRPC...)
  188. return err
  189. }, opts...)
  190. if err != nil {
  191. return nil, err
  192. }
  193. return resp, nil
  194. }
  195. // AnnotateText a convenience method that provides all syntax, sentiment, entity, and
  196. // classification features in one call.
  197. func (c *Client) AnnotateText(ctx context.Context, req *languagepb.AnnotateTextRequest, opts ...gax.CallOption) (*languagepb.AnnotateTextResponse, error) {
  198. ctx = insertMetadata(ctx, c.xGoogMetadata)
  199. opts = append(c.CallOptions.AnnotateText[0:len(c.CallOptions.AnnotateText):len(c.CallOptions.AnnotateText)], opts...)
  200. var resp *languagepb.AnnotateTextResponse
  201. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  202. var err error
  203. resp, err = c.client.AnnotateText(ctx, req, settings.GRPC...)
  204. return err
  205. }, opts...)
  206. if err != nil {
  207. return nil, err
  208. }
  209. return resp, nil
  210. }