Showing posts with label sentiment. Show all posts
Showing posts with label sentiment. Show all posts

Wednesday, February 19, 2025

Azure AI services | Language service | Azure Cognitive Search | Sentiment analysis and opinion mining

Azure AI services | Language service | Azure Cognitive Search | Sentiment analysis and opinion mining

1. Create a Language resource in Azure and copy languagekey.

using Azure;
using System;
using Azure.AI.TextAnalytics;
using System.Collections.Generic;

namespace Example
{
    class Program
    {
        // This example requires environment variables named "LANGUAGE_KEY" and "LANGUAGE_ENDPOINT"

        // Environment.GetEnvironmentVariable("LANGUAGE_KEY");
        static string languageKey = "9BuOuIbUCOGGygz";
       
        //Environment.GetEnvironmentVariable("LANGUAGE_ENDPOINT");
        static string languageEndpoint = "https://sreelanguage.cognitiveservices.azure.com/";

        private static readonly AzureKeyCredential credentials = new AzureKeyCredential(languageKey);
        private static readonly Uri endpoint = new Uri(languageEndpoint);

        // Example method for detecting opinions text.
        static void SentimentAnalysisWithOpinionMiningExample(TextAnalyticsClient client)
        {
            var documents = new List<string>
            {
                "The food and service were unacceptable. The concierge was nice, however."
            };

            AnalyzeSentimentResultCollection reviews = client.AnalyzeSentimentBatch(documents,
            options: new AnalyzeSentimentOptions()
            {
                IncludeOpinionMining = true
            });

            foreach (AnalyzeSentimentResult review in reviews)
            {
                Console.WriteLine($"Document sentiment: {review.DocumentSentiment.Sentiment}\n");
                Console.WriteLine($"\tPositive score:
                {review.DocumentSentiment.ConfidenceScores.Positive:0.00}");
                Console.WriteLine($"\tNegative score:
                {review.DocumentSentiment.ConfidenceScores.Negative:0.00}");
                Console.WriteLine($"\tNeutral score:
                {review.DocumentSentiment.ConfidenceScores.Neutral:0.00}\n");

                foreach (SentenceSentiment sentence in review.DocumentSentiment.Sentences)
                {
                    Console.WriteLine($"\tText: \"{sentence.Text}\"");
                    Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}");
                    Console.WriteLine($"\tSentence positive score:
                    {sentence.ConfidenceScores.Positive:0.00}");
                    Console.WriteLine($"\tSentence negative score:
                    {sentence.ConfidenceScores.Negative:0.00}");
                    Console.WriteLine($"\tSentence neutral score:
                    {sentence.ConfidenceScores.Neutral:0.00}\n");

                    foreach (SentenceOpinion sentenceOpinion in sentence.Opinions)
                    {
                        Console.WriteLine($"\tTarget: {sentenceOpinion.Target.Text}, Value:
                        {sentenceOpinion.Target.Sentiment}");
                        Console.WriteLine($"\tTarget positive score:
                        {sentenceOpinion.Target.ConfidenceScores.Positive:0.00}");
                        Console.WriteLine($"\tTarget negative score:
                        {sentenceOpinion.Target.ConfidenceScores.Negative:0.00}");
                        foreach (AssessmentSentiment assessment in sentenceOpinion.Assessments)
                        {
                            Console.WriteLine($"\t\tRelated Assessment: {assessment.Text}, Value:
                            {assessment.Sentiment}");
                            Console.WriteLine($"\t\tRelated Assessment positive score:
                            {assessment.ConfidenceScores.Positive:0.00}");
                            Console.WriteLine($"\t\tRelated Assessment negative score:
                            {assessment.ConfidenceScores.Negative:0.00}");
                        }
                    }
                }
                Console.WriteLine($"\n");
            }
        }

        static void Main(string[] args)
        {
            var client = new TextAnalyticsClient(endpoint, credentials);
            SentimentAnalysisWithOpinionMiningExample(client);
            Console.Write("Press any key to exit.");
            Console.ReadKey();
        }
    }
}


OutPut:


Wednesday, January 29, 2025

Azure AI services - Use an Azure AI Services Container

 Azure AI services - Use an Azure AI Services Container:

Source : https://github.com/MicrosoftLearning/mslearn-ai-services

Create these two services in Azure.
1. Azure AI services multi-service account
2. Azure Container Instances

2. Azure Container Instances:
az container create --resource-group <your-resource-Group> --name whizcontaineryourname --image mcr.microsoft.com/azure-cognitive-services/textanalytics/sentiment:latest --os-type Linux --cpu 1 --memory 8 --dns-name-label whizdnnsyourname --ports 5000 --location eastus --restart-policy OnFailure --secure-environment-variables ApiKey=<your-Api-key> Billing=<endpoint> --environment-variables Eula=accept --ip-address Public

az container create --resource-group rg1 --name sreecontainer1 --image mcr.microsoft.com/azure-cognitive-services/textanalytics/sentiment:latest --os-type Linux --cpu 1 --memory 8 --dns-name-label sreednns1 --ports 5000 --location eastus --restart-policy OnFailure --secure-environment-variables ApiKey=AjShjnYv3s56NZNIAkHBni7RCPQCOGAIif Billing=https://sreemultiserviceaccount1.cognitiveservices.azure.com/ --environment-variables Eula=accept --ip-address Public

For Docker:
docker run --rm -it -p 5000:5000 --memory 8g --cpus 1 mcr.microsoft.com/azure-cognitive-services/textanalytics/sentiment:latest Eula=accept Billing=<yourEndpoint> ApiKey=<yourKey>
 
Testing:
curl -X POST "http://<your_ACI_IP_address_or_FQDN>:5000/text/analytics/v3.1/sentiment" -H "Content-Type: application/json" --data-ascii "{'documents':[{'id':1,'text':'The performance was amazing! The sound could have been clearer.'},{'id':2,'text':'The food and service were unacceptable. While the host was nice, the waiter was rude and food was cold.'}]}"

curl -X POST "http://sreednns1.eastus.azurecontainer.io:5000/text/analytics/v3.1/sentiment" -H "Content-Type: application/json" --data-ascii "{'documents':[{'id':1,'text':'The performance was amazing! The sound could have been clearer.'},{'id':2,'text':'The food and service were unacceptable. While the host was nice, the waiter was rude and food was cold.'}]}"

OutPut:

{  
  "documents": [  
    {  
      "id": "1",  
      "sentiment": "positive",  
      "confidenceScores": {  
        "positive": 0.99,  
        "neutral": 0.0,  
        "negative": 0.0  
      },  
      "sentences": [  
        {  
          "sentiment": "positive",  
          "confidenceScores": {  
            "positive": 0.99,  
            "neutral": 0.0,  
            "negative": 0.0  
          },  
          "offset": 0,  
          "length": 29,  
          "text": "The performance was amazing! "  
        },  
        {  
          "sentiment": "neutral",  
          "confidenceScores": {  
            "positive": 0.19,  
            "neutral": 0.47,  
            "negative": 0.34  
          },  
          "offset": 29,  
          "length": 34,  
          "text": "The sound could have been clearer."  
        }  
      ],  
      "warnings": []  
    },  
    {  
      "id": "2",  
      "sentiment": "negative",  
      "confidenceScores": {  
        "positive": 0.0,  
        "neutral": 0.01,  
        "negative": 0.98  
      },  
      "sentences": [  
        {  
          "sentiment": "negative",  
          "confidenceScores": {  
            "positive": 0.0,  
            "neutral": 0.01,  
            "negative": 0.99  
          },  
          "offset": 0,  
          "length": 40,  
          "text": "The food and service were unacceptable. "  
        },  
        {  
          "sentiment": "negative",  
          "confidenceScores": {  
            "positive": 0.0,  
            "neutral": 0.02,  
            "negative": 0.98  
          },  
          "offset": 40,  
          "length": 63,  
          "text": "While the host was nice, the waiter was rude and food was cold."  
        }  
      ],  
      "warnings": []  
    }  
  ],  
  "errors": []  
}


Featured Post

Reassign a Copilot Studio Agent Owner Using PowerShell and the Dataverse Web API

Reassign a Copilot Studio Agent Owner Using PowerShell and the Dataverse Web API Every Copilot Studio agent is a row in the Dataverse bot t...

Popular posts