Add this skill
npx mdskills install sickn33/azure-ai-textanalytics-pyComprehensive Azure text analytics guide with excellent examples for all major NLP operations
1---2name: azure-ai-textanalytics-py3description: |4 Azure AI Text Analytics SDK for sentiment analysis, entity recognition, key phrases, language detection, PII, and healthcare NLP. Use for natural language processing on text.5 Triggers: "text analytics", "sentiment analysis", "entity recognition", "key phrase", "PII detection", "TextAnalyticsClient".6package: azure-ai-textanalytics7---89# Azure AI Text Analytics SDK for Python1011Client library for Azure AI Language service NLP capabilities including sentiment, entities, key phrases, and more.1213## Installation1415```bash16pip install azure-ai-textanalytics17```1819## Environment Variables2021```bash22AZURE_LANGUAGE_ENDPOINT=https://<resource>.cognitiveservices.azure.com23AZURE_LANGUAGE_KEY=<your-api-key> # If using API key24```2526## Authentication2728### API Key2930```python31import os32from azure.core.credentials import AzureKeyCredential33from azure.ai.textanalytics import TextAnalyticsClient3435endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"]36key = os.environ["AZURE_LANGUAGE_KEY"]3738client = TextAnalyticsClient(endpoint, AzureKeyCredential(key))39```4041### Entra ID (Recommended)4243```python44from azure.ai.textanalytics import TextAnalyticsClient45from azure.identity import DefaultAzureCredential4647client = TextAnalyticsClient(48 endpoint=os.environ["AZURE_LANGUAGE_ENDPOINT"],49 credential=DefaultAzureCredential()50)51```5253## Sentiment Analysis5455```python56documents = [57 "I had a wonderful trip to Seattle last week!",58 "The food was terrible and the service was slow."59]6061result = client.analyze_sentiment(documents, show_opinion_mining=True)6263for doc in result:64 if not doc.is_error:65 print(f"Sentiment: {doc.sentiment}")66 print(f"Scores: pos={doc.confidence_scores.positive:.2f}, "67 f"neg={doc.confidence_scores.negative:.2f}, "68 f"neu={doc.confidence_scores.neutral:.2f}")6970 # Opinion mining (aspect-based sentiment)71 for sentence in doc.sentences:72 for opinion in sentence.mined_opinions:73 target = opinion.target74 print(f" Target: '{target.text}' - {target.sentiment}")75 for assessment in opinion.assessments:76 print(f" Assessment: '{assessment.text}' - {assessment.sentiment}")77```7879## Entity Recognition8081```python82documents = ["Microsoft was founded by Bill Gates and Paul Allen in Albuquerque."]8384result = client.recognize_entities(documents)8586for doc in result:87 if not doc.is_error:88 for entity in doc.entities:89 print(f"Entity: {entity.text}")90 print(f" Category: {entity.category}")91 print(f" Subcategory: {entity.subcategory}")92 print(f" Confidence: {entity.confidence_score:.2f}")93```9495## PII Detection9697```python98documents = ["My SSN is 123-45-6789 and my email is john@example.com"]99100result = client.recognize_pii_entities(documents)101102for doc in result:103 if not doc.is_error:104 print(f"Redacted: {doc.redacted_text}")105 for entity in doc.entities:106 print(f"PII: {entity.text} ({entity.category})")107```108109## Key Phrase Extraction110111```python112documents = ["Azure AI provides powerful machine learning capabilities for developers."]113114result = client.extract_key_phrases(documents)115116for doc in result:117 if not doc.is_error:118 print(f"Key phrases: {doc.key_phrases}")119```120121## Language Detection122123```python124documents = ["Ce document est en francais.", "This is written in English."]125126result = client.detect_language(documents)127128for doc in result:129 if not doc.is_error:130 print(f"Language: {doc.primary_language.name} ({doc.primary_language.iso6391_name})")131 print(f"Confidence: {doc.primary_language.confidence_score:.2f}")132```133134## Healthcare Text Analytics135136```python137documents = ["Patient has diabetes and was prescribed metformin 500mg twice daily."]138139poller = client.begin_analyze_healthcare_entities(documents)140result = poller.result()141142for doc in result:143 if not doc.is_error:144 for entity in doc.entities:145 print(f"Entity: {entity.text}")146 print(f" Category: {entity.category}")147 print(f" Normalized: {entity.normalized_text}")148149 # Entity links (UMLS, etc.)150 for link in entity.data_sources:151 print(f" Link: {link.name} - {link.entity_id}")152```153154## Multiple Analysis (Batch)155156```python157from azure.ai.textanalytics import (158 RecognizeEntitiesAction,159 ExtractKeyPhrasesAction,160 AnalyzeSentimentAction161)162163documents = ["Microsoft announced new Azure AI features at Build conference."]164165poller = client.begin_analyze_actions(166 documents,167 actions=[168 RecognizeEntitiesAction(),169 ExtractKeyPhrasesAction(),170 AnalyzeSentimentAction()171 ]172)173174results = poller.result()175for doc_results in results:176 for result in doc_results:177 if result.kind == "EntityRecognition":178 print(f"Entities: {[e.text for e in result.entities]}")179 elif result.kind == "KeyPhraseExtraction":180 print(f"Key phrases: {result.key_phrases}")181 elif result.kind == "SentimentAnalysis":182 print(f"Sentiment: {result.sentiment}")183```184185## Async Client186187```python188from azure.ai.textanalytics.aio import TextAnalyticsClient189from azure.identity.aio import DefaultAzureCredential190191async def analyze():192 async with TextAnalyticsClient(193 endpoint=endpoint,194 credential=DefaultAzureCredential()195 ) as client:196 result = await client.analyze_sentiment(documents)197 # Process results...198```199200## Client Types201202| Client | Purpose |203|--------|---------|204| `TextAnalyticsClient` | All text analytics operations |205| `TextAnalyticsClient` (aio) | Async version |206207## Available Operations208209| Method | Description |210|--------|-------------|211| `analyze_sentiment` | Sentiment analysis with opinion mining |212| `recognize_entities` | Named entity recognition |213| `recognize_pii_entities` | PII detection and redaction |214| `recognize_linked_entities` | Entity linking to Wikipedia |215| `extract_key_phrases` | Key phrase extraction |216| `detect_language` | Language detection |217| `begin_analyze_healthcare_entities` | Healthcare NLP (long-running) |218| `begin_analyze_actions` | Multiple analyses in batch |219220## Best Practices2212221. **Use batch operations** for multiple documents (up to 10 per request)2232. **Enable opinion mining** for detailed aspect-based sentiment2243. **Use async client** for high-throughput scenarios2254. **Handle document errors** — results list may contain errors for some docs2265. **Specify language** when known to improve accuracy2276. **Use context manager** or close client explicitly228
Full transparency — inspect the skill content before installing.