Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

232 рядки
8.6 KiB

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