Introduction
Azure Language supports analysis of text, including language detection, sentiment analysis, key phrase extraction, and entity recognition.
Requirements
- Microsoft Azure subscription
- VS code
Provision an Azure AI Language resource
If you don’t already have one in your subscription, you’ll need to provision an Azure AI Language service resource in your Azure subscription.
- Go to your Azure portal and click create a resource
- Search for language service and select create under language service.
- Select continue to create your resource
- Provision your resource using the following settings:
- Subscription: Your Azure subscription.
- Resource group: Choose or create a resource group.
- Region:Choose any available region
- Name: Enter a unique name.
- Pricing tier: Select F0 (free), or S (standard) if F is not available.
- Responsible AI Notice: Agree.
- Select Review+create
- Wait for validation to complete and click create.
- Wait for deployment to complete, and then go to the deployed resource.
- View the Keys and Endpoint page in the Resource Management section. You will need the information on this page later in the exercise.
Clone the repository for this course
Open your VS Code and login to your Azure subscription by running the command:
az login
Follow the command to sign into your Azure account.
Enter the following commands to clone the GitHub repo for this exercise:
git clone https://github.com/microsoftlearning/mslearn-ai-language mslearn-ai-language
- After the repo has been cloned, navigate to the folder containing the application code files:
cd mslearn-ai-language/Labfiles/01-analyze-text
Configure your application
Applications for both C# and Python have been provided, as well as a sample text file you’ll use to test the summarization. Both apps feature the same functionality. We will be using C# for this project. First, you’ll complete some key parts of the application to enable it to use your Azure AI Language resource.
- Run the command cd C-Sharp/text-analysis depending on your language preference. Each folder contains the language-specific files for an app into which you’re going to integrate Azure AI Language text analytics functionality.
- Install the Azure AI Language Text Analytics SDK package by running the appropriate command for your language preference:
For C#:
dotnet add package Azure.AI.TextAnalytics --version 5.3.0
- Edit the appsettings.json by expanding text-analysis.
- Update the configuration values to include the endpoint and a key from the Azure Language resource you created (available on the Keys and Endpoint page for your Azure AI Language resource in the Azure portal)
- After you’ve replaced the placeholders, within the code editor, use the CTRL+S command or Right-click > Save to save your changes
- Open Programs.cs file and at the top, under the existing namespace references, find the comment Import namespaces. Then, under this comment, add the following language-specific code to import the namespaces you will need to use the Text Analytics SDK:
// import namespaces
using Azure;
using Azure.AI.TextAnalytics;
- In the Main function, note that code to load the Azure AI Language service endpoint and key from the configuration file has already been provided. Then find the comment Create client using endpoint and key, and add the following code to create a client for the Text Analysis API:
// Create client using endpoint and key
AzureKeyCredential credentials = new AzureKeyCredential(aiSvcKey);
Uri endpoint = new Uri(aiSvcEndpoint);
TextAnalyticsClient aiClient = new TextAnalyticsClient(endpoint, credentials);
Save your changes, then enter the following command to run the program:
dotnet run
Observe the output as the code should run without error, displaying the contents of each review text file in the reviews folder. The application successfully creates a client for the Text Analytics API but doesn’t make use of it. We’ll fix that in the next section.
Add code to detect language
Now that you have created a client for the API, let’s use it to detect the language in which each review is written.
- With Programs.cs file opened, find the comment Get language in the Main function for your program. Then, under this comment, add the code necessary to detect the language in each review document:
// Get language
DetectedLanguage detectedLanguage = aiClient.DetectLanguage(text);
Console.WriteLine($"\nLanguage: {detectedLanguage.Name}");
- Save your changes and re-run the program.
- Observe the output, noting that this time the language for each review is identified.
Add code to evaluate sentiment
Sentiment analysis is a commonly used technique to classify text as positive or negative (or possible neutral or mixed). It’s commonly used to analyze social media posts, product reviews, and other items where the sentiment of the text may provide useful insights.
- Still with Programs.cs file opened, find the comment Get sentiment. Then, under this comment, add the code necessary to detect the sentiment of each review document:
// Get sentiment
DocumentSentiment sentimentAnalysis = aiClient.AnalyzeSentiment(text);
Console.WriteLine($"\nSentiment: {sentimentAnalysis.Sentiment}");
- Save your changes and re-run the program.
- Observe the output, noting that the sentiment of the reviews is detected.
Add code to identify key phrases
It can be useful to identify key phrases in a body of text to help determine the main topics that it discusses.
- Still with Programs.cs file opened, find the comment Get key phrases. Then, under this comment, add the code necessary to detect the key phrases of each review document:
// Get key phrases
KeyPhraseCollection phrases = aiClient.ExtractKeyPhrases(text);
if (phrases.Count > 0)
{
Console.WriteLine("\nKey Phrases:");
foreach(string phrase in phrases)
{
Console.WriteLine($"\t{phrase}");
}
}
- Save your changes and re-run the program.
- Observe the output, noting that each document contains key phrases that give some insights into what the review is about.
Add code to extract entities
Often, documents or other bodies of text mention people, places, time periods, or other entities. The text Analytics API can detect multiple categories (and subcategories) of entity in your text.
- Still with Programs.cs file opened, find the comment Get entities phrases. Then, under this comment, add the code necessary to identify entities that are mentioned in each review:
// Get entities
CategorizedEntityCollection entities = aiClient.RecognizeEntities(text);
if (entities.Count > 0)
{
Console.WriteLine("\nEntities:");
foreach(CategorizedEntity entity in entities)
{
Console.WriteLine($"\t{entity.Text} ({entity.Category})");
}
}
- Save your changes and re-run the program.
- Observe the output, noting the entities that have been detected in the text.
Add code to extract linked entities
In addition to categorized entities, the Text Analytics API can detect entities for which there are known links to data sources, such as Wikipedia.
- Still with Programs.cs file opened, find the comment Get linked entities. Then, under this comment, add the code necessary to identify linked entities that are mentioned in each review:
// Get linked entities
LinkedEntityCollection linkedEntities = aiClient.RecognizeLinkedEntities(text);
if (linkedEntities.Count > 0)
{
Console.WriteLine("\nLinks:");
foreach(LinkedEntity linkedEntity in linkedEntities)
{
Console.WriteLine($"\t{linkedEntity.Name} ({linkedEntity.Url})");
}
}
- Save your changes and re-run the program.
- Observe the output, noting the linked entities that are identified.
Azure AI Language opens up a world of possibilities for analyzing text—whether you're assessing customer sentiment, extracting key insights, or identifying important entities. With just a few lines of code, you can integrate powerful NLP capabilities into your applications and unlock deeper understanding from unstructured data.
Now that you've seen how easy it is to get started, why not experiment further? Try analyzing your own datasets, fine-tuning results, or even combining these features with other Azure AI services to build even smarter solutions.
The future of intelligent applications starts here—happy coding!
credits: Analyze text with Azure AI Language
Thanks for staying till the end
Top comments (0)