...

Source file src/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics/client.go

Documentation: github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v2.1/textanalytics

     1  // Package textanalytics implements the Azure ARM Textanalytics service API version v2.1.
     2  //
     3  // The Text Analytics API is a suite of text analytics web services built with best-in-class Microsoft machine learning
     4  // algorithms. The API can be used to analyze unstructured text for tasks such as sentiment analysis, key phrase
     5  // extraction and language detection. No training data is needed to use this API; just bring your text data. This API
     6  // uses advanced natural language processing techniques to deliver best in class predictions. Further documentation can
     7  // be found in https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/overview
     8  package textanalytics
     9  
    10  // Copyright (c) Microsoft Corporation. All rights reserved.
    11  // Licensed under the MIT License. See License.txt in the project root for license information.
    12  //
    13  // Code generated by Microsoft (R) AutoRest Code Generator.
    14  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
    15  
    16  import (
    17  	"context"
    18  	"github.com/Azure/go-autorest/autorest"
    19  	"github.com/Azure/go-autorest/autorest/azure"
    20  	"github.com/Azure/go-autorest/tracing"
    21  	"net/http"
    22  )
    23  
    24  // BaseClient is the base client for Textanalytics.
    25  type BaseClient struct {
    26  	autorest.Client
    27  	Endpoint string
    28  }
    29  
    30  // New creates an instance of the BaseClient client.
    31  func New(endpoint string) BaseClient {
    32  	return NewWithoutDefaults(endpoint)
    33  }
    34  
    35  // NewWithoutDefaults creates an instance of the BaseClient client.
    36  func NewWithoutDefaults(endpoint string) BaseClient {
    37  	return BaseClient{
    38  		Client:   autorest.NewClientWithUserAgent(UserAgent()),
    39  		Endpoint: endpoint,
    40  	}
    41  }
    42  
    43  // DetectLanguage scores close to 1 indicate 100% certainty that the identified language is true. A total of 120
    44  // languages are supported.
    45  // Parameters:
    46  // showStats - (optional) if set to true, response will contain input and document level statistics.
    47  // languageBatchInput - collection of documents to analyze.
    48  func (client BaseClient) DetectLanguage(ctx context.Context, showStats *bool, languageBatchInput *LanguageBatchInput) (result LanguageBatchResult, err error) {
    49  	if tracing.IsEnabled() {
    50  		ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.DetectLanguage")
    51  		defer func() {
    52  			sc := -1
    53  			if result.Response.Response != nil {
    54  				sc = result.Response.Response.StatusCode
    55  			}
    56  			tracing.EndSpan(ctx, sc, err)
    57  		}()
    58  	}
    59  	req, err := client.DetectLanguagePreparer(ctx, showStats, languageBatchInput)
    60  	if err != nil {
    61  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "DetectLanguage", nil, "Failure preparing request")
    62  		return
    63  	}
    64  
    65  	resp, err := client.DetectLanguageSender(req)
    66  	if err != nil {
    67  		result.Response = autorest.Response{Response: resp}
    68  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "DetectLanguage", resp, "Failure sending request")
    69  		return
    70  	}
    71  
    72  	result, err = client.DetectLanguageResponder(resp)
    73  	if err != nil {
    74  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "DetectLanguage", resp, "Failure responding to request")
    75  		return
    76  	}
    77  
    78  	return
    79  }
    80  
    81  // DetectLanguagePreparer prepares the DetectLanguage request.
    82  func (client BaseClient) DetectLanguagePreparer(ctx context.Context, showStats *bool, languageBatchInput *LanguageBatchInput) (*http.Request, error) {
    83  	urlParameters := map[string]interface{}{
    84  		"Endpoint": client.Endpoint,
    85  	}
    86  
    87  	queryParameters := map[string]interface{}{}
    88  	if showStats != nil {
    89  		queryParameters["showStats"] = autorest.Encode("query", *showStats)
    90  	}
    91  
    92  	preparer := autorest.CreatePreparer(
    93  		autorest.AsContentType("application/json; charset=utf-8"),
    94  		autorest.AsPost(),
    95  		autorest.WithCustomBaseURL("{Endpoint}/text/analytics/v2.1", urlParameters),
    96  		autorest.WithPath("/languages"),
    97  		autorest.WithQueryParameters(queryParameters))
    98  	if languageBatchInput != nil {
    99  		preparer = autorest.DecoratePreparer(preparer,
   100  			autorest.WithJSON(languageBatchInput))
   101  	}
   102  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   103  }
   104  
   105  // DetectLanguageSender sends the DetectLanguage request. The method will close the
   106  // http.Response Body if it receives an error.
   107  func (client BaseClient) DetectLanguageSender(req *http.Request) (*http.Response, error) {
   108  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   109  }
   110  
   111  // DetectLanguageResponder handles the response to the DetectLanguage request. The method always
   112  // closes the http.Response Body.
   113  func (client BaseClient) DetectLanguageResponder(resp *http.Response) (result LanguageBatchResult, err error) {
   114  	err = autorest.Respond(
   115  		resp,
   116  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   117  		autorest.ByUnmarshallingJSON(&result),
   118  		autorest.ByClosing())
   119  	result.Response = autorest.Response{Response: resp}
   120  	return
   121  }
   122  
   123  // Entities to get even more information on each recognized entity we recommend using the Bing Entity Search API by
   124  // querying for the recognized entities names. See the <a
   125  // href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/text-analytics-supported-languages">Supported
   126  // languages in Text Analytics API</a> for the list of enabled languages.
   127  // Parameters:
   128  // showStats - (optional) if set to true, response will contain input and document level statistics.
   129  // multiLanguageBatchInput - collection of documents to analyze.
   130  func (client BaseClient) Entities(ctx context.Context, showStats *bool, multiLanguageBatchInput *MultiLanguageBatchInput) (result EntitiesBatchResult, err error) {
   131  	if tracing.IsEnabled() {
   132  		ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.Entities")
   133  		defer func() {
   134  			sc := -1
   135  			if result.Response.Response != nil {
   136  				sc = result.Response.Response.StatusCode
   137  			}
   138  			tracing.EndSpan(ctx, sc, err)
   139  		}()
   140  	}
   141  	req, err := client.EntitiesPreparer(ctx, showStats, multiLanguageBatchInput)
   142  	if err != nil {
   143  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "Entities", nil, "Failure preparing request")
   144  		return
   145  	}
   146  
   147  	resp, err := client.EntitiesSender(req)
   148  	if err != nil {
   149  		result.Response = autorest.Response{Response: resp}
   150  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "Entities", resp, "Failure sending request")
   151  		return
   152  	}
   153  
   154  	result, err = client.EntitiesResponder(resp)
   155  	if err != nil {
   156  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "Entities", resp, "Failure responding to request")
   157  		return
   158  	}
   159  
   160  	return
   161  }
   162  
   163  // EntitiesPreparer prepares the Entities request.
   164  func (client BaseClient) EntitiesPreparer(ctx context.Context, showStats *bool, multiLanguageBatchInput *MultiLanguageBatchInput) (*http.Request, error) {
   165  	urlParameters := map[string]interface{}{
   166  		"Endpoint": client.Endpoint,
   167  	}
   168  
   169  	queryParameters := map[string]interface{}{}
   170  	if showStats != nil {
   171  		queryParameters["showStats"] = autorest.Encode("query", *showStats)
   172  	}
   173  
   174  	preparer := autorest.CreatePreparer(
   175  		autorest.AsContentType("application/json; charset=utf-8"),
   176  		autorest.AsPost(),
   177  		autorest.WithCustomBaseURL("{Endpoint}/text/analytics/v2.1", urlParameters),
   178  		autorest.WithPath("/entities"),
   179  		autorest.WithQueryParameters(queryParameters))
   180  	if multiLanguageBatchInput != nil {
   181  		preparer = autorest.DecoratePreparer(preparer,
   182  			autorest.WithJSON(multiLanguageBatchInput))
   183  	}
   184  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   185  }
   186  
   187  // EntitiesSender sends the Entities request. The method will close the
   188  // http.Response Body if it receives an error.
   189  func (client BaseClient) EntitiesSender(req *http.Request) (*http.Response, error) {
   190  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   191  }
   192  
   193  // EntitiesResponder handles the response to the Entities request. The method always
   194  // closes the http.Response Body.
   195  func (client BaseClient) EntitiesResponder(resp *http.Response) (result EntitiesBatchResult, err error) {
   196  	err = autorest.Respond(
   197  		resp,
   198  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   199  		autorest.ByUnmarshallingJSON(&result),
   200  		autorest.ByClosing())
   201  	result.Response = autorest.Response{Response: resp}
   202  	return
   203  }
   204  
   205  // KeyPhrases see the <a
   206  // href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/overview#supported-languages">Text
   207  // Analytics Documentation</a> for details about the languages that are supported by key phrase extraction.
   208  // Parameters:
   209  // showStats - (optional) if set to true, response will contain input and document level statistics.
   210  // multiLanguageBatchInput - collection of documents to analyze. Documents can now contain a language field to
   211  // indicate the text language
   212  func (client BaseClient) KeyPhrases(ctx context.Context, showStats *bool, multiLanguageBatchInput *MultiLanguageBatchInput) (result KeyPhraseBatchResult, err error) {
   213  	if tracing.IsEnabled() {
   214  		ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.KeyPhrases")
   215  		defer func() {
   216  			sc := -1
   217  			if result.Response.Response != nil {
   218  				sc = result.Response.Response.StatusCode
   219  			}
   220  			tracing.EndSpan(ctx, sc, err)
   221  		}()
   222  	}
   223  	req, err := client.KeyPhrasesPreparer(ctx, showStats, multiLanguageBatchInput)
   224  	if err != nil {
   225  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "KeyPhrases", nil, "Failure preparing request")
   226  		return
   227  	}
   228  
   229  	resp, err := client.KeyPhrasesSender(req)
   230  	if err != nil {
   231  		result.Response = autorest.Response{Response: resp}
   232  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "KeyPhrases", resp, "Failure sending request")
   233  		return
   234  	}
   235  
   236  	result, err = client.KeyPhrasesResponder(resp)
   237  	if err != nil {
   238  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "KeyPhrases", resp, "Failure responding to request")
   239  		return
   240  	}
   241  
   242  	return
   243  }
   244  
   245  // KeyPhrasesPreparer prepares the KeyPhrases request.
   246  func (client BaseClient) KeyPhrasesPreparer(ctx context.Context, showStats *bool, multiLanguageBatchInput *MultiLanguageBatchInput) (*http.Request, error) {
   247  	urlParameters := map[string]interface{}{
   248  		"Endpoint": client.Endpoint,
   249  	}
   250  
   251  	queryParameters := map[string]interface{}{}
   252  	if showStats != nil {
   253  		queryParameters["showStats"] = autorest.Encode("query", *showStats)
   254  	}
   255  
   256  	preparer := autorest.CreatePreparer(
   257  		autorest.AsContentType("application/json; charset=utf-8"),
   258  		autorest.AsPost(),
   259  		autorest.WithCustomBaseURL("{Endpoint}/text/analytics/v2.1", urlParameters),
   260  		autorest.WithPath("/keyPhrases"),
   261  		autorest.WithQueryParameters(queryParameters))
   262  	if multiLanguageBatchInput != nil {
   263  		preparer = autorest.DecoratePreparer(preparer,
   264  			autorest.WithJSON(multiLanguageBatchInput))
   265  	}
   266  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   267  }
   268  
   269  // KeyPhrasesSender sends the KeyPhrases request. The method will close the
   270  // http.Response Body if it receives an error.
   271  func (client BaseClient) KeyPhrasesSender(req *http.Request) (*http.Response, error) {
   272  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   273  }
   274  
   275  // KeyPhrasesResponder handles the response to the KeyPhrases request. The method always
   276  // closes the http.Response Body.
   277  func (client BaseClient) KeyPhrasesResponder(resp *http.Response) (result KeyPhraseBatchResult, err error) {
   278  	err = autorest.Respond(
   279  		resp,
   280  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   281  		autorest.ByUnmarshallingJSON(&result),
   282  		autorest.ByClosing())
   283  	result.Response = autorest.Response{Response: resp}
   284  	return
   285  }
   286  
   287  // Sentiment scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment. A
   288  // score of 0.5 indicates the lack of sentiment (e.g. a factoid statement). See the <a
   289  // href="https://docs.microsoft.com/en-us/azure/cognitive-services/text-analytics/overview#supported-languages">Text
   290  // Analytics Documentation</a> for details about the languages that are supported by sentiment analysis.
   291  // Parameters:
   292  // showStats - (optional) if set to true, response will contain input and document level statistics.
   293  // multiLanguageBatchInput - collection of documents to analyze.
   294  func (client BaseClient) Sentiment(ctx context.Context, showStats *bool, multiLanguageBatchInput *MultiLanguageBatchInput) (result SentimentBatchResult, err error) {
   295  	if tracing.IsEnabled() {
   296  		ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.Sentiment")
   297  		defer func() {
   298  			sc := -1
   299  			if result.Response.Response != nil {
   300  				sc = result.Response.Response.StatusCode
   301  			}
   302  			tracing.EndSpan(ctx, sc, err)
   303  		}()
   304  	}
   305  	req, err := client.SentimentPreparer(ctx, showStats, multiLanguageBatchInput)
   306  	if err != nil {
   307  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "Sentiment", nil, "Failure preparing request")
   308  		return
   309  	}
   310  
   311  	resp, err := client.SentimentSender(req)
   312  	if err != nil {
   313  		result.Response = autorest.Response{Response: resp}
   314  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "Sentiment", resp, "Failure sending request")
   315  		return
   316  	}
   317  
   318  	result, err = client.SentimentResponder(resp)
   319  	if err != nil {
   320  		err = autorest.NewErrorWithError(err, "textanalytics.BaseClient", "Sentiment", resp, "Failure responding to request")
   321  		return
   322  	}
   323  
   324  	return
   325  }
   326  
   327  // SentimentPreparer prepares the Sentiment request.
   328  func (client BaseClient) SentimentPreparer(ctx context.Context, showStats *bool, multiLanguageBatchInput *MultiLanguageBatchInput) (*http.Request, error) {
   329  	urlParameters := map[string]interface{}{
   330  		"Endpoint": client.Endpoint,
   331  	}
   332  
   333  	queryParameters := map[string]interface{}{}
   334  	if showStats != nil {
   335  		queryParameters["showStats"] = autorest.Encode("query", *showStats)
   336  	}
   337  
   338  	preparer := autorest.CreatePreparer(
   339  		autorest.AsContentType("application/json; charset=utf-8"),
   340  		autorest.AsPost(),
   341  		autorest.WithCustomBaseURL("{Endpoint}/text/analytics/v2.1", urlParameters),
   342  		autorest.WithPath("/sentiment"),
   343  		autorest.WithQueryParameters(queryParameters))
   344  	if multiLanguageBatchInput != nil {
   345  		preparer = autorest.DecoratePreparer(preparer,
   346  			autorest.WithJSON(multiLanguageBatchInput))
   347  	}
   348  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   349  }
   350  
   351  // SentimentSender sends the Sentiment request. The method will close the
   352  // http.Response Body if it receives an error.
   353  func (client BaseClient) SentimentSender(req *http.Request) (*http.Response, error) {
   354  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   355  }
   356  
   357  // SentimentResponder handles the response to the Sentiment request. The method always
   358  // closes the http.Response Body.
   359  func (client BaseClient) SentimentResponder(resp *http.Response) (result SentimentBatchResult, err error) {
   360  	err = autorest.Respond(
   361  		resp,
   362  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   363  		autorest.ByUnmarshallingJSON(&result),
   364  		autorest.ByClosing())
   365  	result.Response = autorest.Response{Response: resp}
   366  	return
   367  }
   368  

View as plain text