Google.Cloud.Language.V1

Google.Cloud.Language.V1 is a.NET client library for the Google Cloud Natural Language API.

Note: This documentation is for version 3.3.0 of the library. Some samples may not work with other versions.

Installation

Install the Google.Cloud.Language.V1 package from NuGet. Add it to your project in the normal way (for example by right-clicking on the project in Visual Studio and choosing "Manage NuGet Packages...").

Authentication

When running on Google Cloud Platform, no action needs to be taken to authenticate.

Otherwise, the simplest way of authenticating your API calls is to download a service account JSON file then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to it. The credentials will automatically be used to authenticate. See the Getting Started With Authentication guide for more details.

Getting started

All operations are performed through LanguageServiceClient.

Create a client instance by calling the static Create or CreateAsync methods. Alternatively, use the builder class associated with each client class (e.g. LanguageServiceClientBuilder for LanguageServiceClient) as an easy way of specifying custom credentials, settings, or a custom endpoint. Clients are thread-safe, and we recommend using a single instance across your entire application unless you have a particular need to configure multiple client objects separately.

Using the REST (HTTP/1.1) transport

This library defaults to performing RPCs using gRPC using the binary Protocol Buffer wire format. However, it also supports HTTP/1.1 and JSON, for situations where gRPC doesn't work as desired. (This is typically due to an incompatible proxy or other network issue.) To create a client using HTTP/1.1, specify a RestGrpcAdapter reference for the GrpcAdapter property in the client builder. Sample code:

var client = new LanguageServiceClientBuilder
{
    GrpcAdapter = RestGrpcAdapter.Default
}.Build();

For more details, see the transport selection page.

Creating a Document

All the methods on the client require a Document to analyze. The Document class provides convenient factory methods Document.FromPlainText and Document.FromHtml to construct instances from text available in your application, or you can specify a Google Cloud Storage URI.

Sample code

Analyze sentiment in a document

LanguageServiceClient client = LanguageServiceClient.Create();
Document document = Document.FromPlainText("You're simply the best - better than all the rest.");
AnalyzeSentimentResponse response = client.AnalyzeSentiment(document);
Console.WriteLine($"Detected language: {response.Language}");
Console.WriteLine($"Sentiment score: {response.DocumentSentiment.Score}");
Console.WriteLine($"Sentiment magnitude: {response.DocumentSentiment.Magnitude}");

Analyze entities in a document

LanguageServiceClient client = LanguageServiceClient.Create();
Document document = Document.FromPlainText("Richard of York gave battle in vain.");
AnalyzeEntitiesResponse response = client.AnalyzeEntities(document);
Console.WriteLine($"Detected language: {response.Language}");
Console.WriteLine("Detected entities:");
foreach (Entity entity in response.Entities)
{
    Console.WriteLine($"  {entity.Name} ({(int) (entity.Salience * 100)}%)");
}

Analyze syntax in a document

LanguageServiceClient client = LanguageServiceClient.Create();
Document document = Document.FromPlainText(
    "This is a simple sentence. This sentence, while not very complicated, is still more complicated than the first.");
AnalyzeSyntaxResponse response = client.AnalyzeSyntax(document);
Console.WriteLine($"Detected language: {response.Language}");
Console.WriteLine($"Number of sentences: {response.Sentences.Count}");
Console.WriteLine($"Number of tokens: {response.Tokens.Count}");

Multiple operations: analyze syntax and entities

LanguageServiceClient client = LanguageServiceClient.Create();
Document document = Document.FromPlainText("Richard of York gave battle in vain.");
// "Features" selects which features you wish to enable. Here we want to extract syntax
// and entities - you can also extract document sentiment.
AnnotateTextResponse response = client.AnnotateText(document,
    new Features { ExtractSyntax = true, ExtractEntities = true });
Console.WriteLine($"Detected language: {response.Language}");
// The Sentences and Tokens properties provide all kinds of information
// about the parsed text.
Console.WriteLine($"Number of sentences: {response.Sentences.Count}");
Console.WriteLine($"Number of tokens: {response.Tokens.Count}");
Console.WriteLine("Detected entities:");
foreach (Entity entity in response.Entities)
{
    Console.WriteLine($"  {entity.Name} ({(int)(entity.Salience * 100)}%)");
}

Using an API key

The Cloud Language API supports API keys as an alternative to regular authentication. The Google.Cloud.Language.V1 library can be configured to supply the API key in the X-Goog-Api-Key header. Note that when specifying an API key, the gRPC channel should use plain SSL without including any Cloud authentication information, as demonstrated in the sample below.

// Create a LanguageServiceSettings that applies the X-Goog-Api-Key
// header on every request
LanguageServiceSettings settings = new LanguageServiceSettings
{
    CallSettings = CallSettings.FromHeader("X-Goog-Api-Key", apiKey)
};

// Create a LanguageServiceClient which uses the settings to apply
// the API key to every request. The ChannelCredentials are set to
// just SSL; we don't authenticate at the channel level when
// applying API keys.
LanguageServiceClient client = new LanguageServiceClientBuilder
{
    ChannelCredentials = ChannelCredentials.SecureSsl,
    Settings = settings
}.Build();

// After creating the client with an API key configured, we can use it
// as normal.
Document document = Document.FromPlainText("This is a simple sentence.");
AnalyzeSyntaxResponse response = client.AnalyzeSyntax(document);

Console.WriteLine($"Detected language: {response.Language}");
Console.WriteLine($"Number of sentences: {response.Sentences.Count}");
Console.WriteLine($"Number of tokens: {response.Tokens.Count}");