Exercise - Analyze text
Unit 6 of 7 · Module 16: Analyze text with Azure Language in Foundry Tools
Before you start
Short explainers for ideas this lesson builds on. Watch these first if they're new to you.
Read this lesson
Five reviews on the line
Time to build it. Wayfarer has five hotel reviews waiting in a folder. Fogline Inn in Hayes Valley, Pier Lantern Suites by the ferry pier, a guesthouse in Lisbon written in Portuguese, an inn in Kyoto, and a lodge in Cape Town written in Spanish. Your Python app will read each one and print its language, the entities it mentions, and any personal details, with a redacted copy.
Getting on board
First, in the Foundry portal, you create a new project. Under Advanced options you keep the default resource name, and choose your subscription, a resource group and a region. When it's ready, the project's Home page shows the key, the project endpoint and the OpenAI endpoint. Next, you clone the lab repo in VS Code and open the text analysis folder: the reviews, a dot env file, requirements dot t x t, and the code file. You create a virtual environment and run pip install dash r requirements dot t x t. Then you paste the endpoint into dot env, and trim off slash api slash projects and the project name, so it ends at dot com.
Three calls per review
In the code, you import DefaultAzureCredential and TextAnalyticsClient, and create the client with your endpoint. Sign in with az login, then run the app. At first it only prints each review, which it opens as UTF eight text. Then you add the three calls. Each review goes in as a list with one document, so you take result zero from what comes back. Detect language prints the language's name. Recognize entities prints each entity's text and category. Recognize PII entities prints each detail, then the redacted text. When you're done, delete the resource group, so nothing keeps costing money.
Facts from Microsoft's documentation: Quickstart: detect the language of text; How to perform language detection; How to perform named entity recognition; Identify and extract PII from text (CC BY 4.0), rewritten in Loop's own words and examples. Narration, animation, and quiz by Loop. The coding lab is based on Microsoft's lab files for the Azure Language and Speech exercises (MicrosoftLearning/mslearn-ai-language, MIT License; notice). Microsoft, Azure, and Microsoft Foundry are trademarks of Microsoft. Loop is independent and not affiliated with or endorsed by Microsoft.