Encamina.Enmarcha.AI.LanguagesDetection.Azure 8.1.2-preview-13

This is a prerelease version of Encamina.Enmarcha.AI.LanguagesDetection.Azure.
There is a newer version of this package available.
See the version list below for details.
dotnet add package Encamina.Enmarcha.AI.LanguagesDetection.Azure --version 8.1.2-preview-13                
NuGet\Install-Package Encamina.Enmarcha.AI.LanguagesDetection.Azure -Version 8.1.2-preview-13                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Encamina.Enmarcha.AI.LanguagesDetection.Azure" Version="8.1.2-preview-13" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Encamina.Enmarcha.AI.LanguagesDetection.Azure --version 8.1.2-preview-13                
#r "nuget: Encamina.Enmarcha.AI.LanguagesDetection.Azure, 8.1.2-preview-13"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Encamina.Enmarcha.AI.LanguagesDetection.Azure as a Cake Addin
#addin nuget:?package=Encamina.Enmarcha.AI.LanguagesDetection.Azure&version=8.1.2-preview-13&prerelease

// Install Encamina.Enmarcha.AI.LanguagesDetection.Azure as a Cake Tool
#tool nuget:?package=Encamina.Enmarcha.AI.LanguagesDetection.Azure&version=8.1.2-preview-13&prerelease                

AI - Azure Languages Detection

Nuget package

Azure Languages Detection is a wrapper project for Azure Cognitive Services Text Analytic client library and Azure AI Translator API REST. Its main functionality is to simplify and abstract the usage of the library, primarily focused on detect language.

Setup

Nuget package

First, install NuGet. Then, install Encamina.Enmarcha.AI.LanguagesDetection.Azure from the package manager console:

PM> Install-Package Encamina.Enmarcha.AI.LanguagesDetection.Azure

.NET CLI:

First, install .NET CLI. Then, install Encamina.Enmarcha.AI.LanguagesDetection.Azure from the .NET CLI:

dotnet add package Encamina.Enmarcha.AI.LanguagesDetection.Azure

How to use

There are two implementations for ILanguageDetectionService, one utilizing the Azure Cognitive Services Text Analytic client library, and the other using the Azure AI Translator API REST.

Azure Text Analytics

First, you need to add the TextAnalyticsLanguageDetectionConfigurations data to your project configuration. You can achieve this by using any configuration provider. The followng code is an example of how the settings would appear using the appsettings.json file:

  {
    // ...
    "TextAnalyticsLanguageDetectionConfigurations": {
        "TextAnalyticsLanguageDetectionServiceOptions": [
          {
            "Name": "DefaultAnalyticsLanguageDetection", // Name of this configuration
            "EndpointUrl": "https://example.cognitiveservices.azure.com/", // Text Analytic endpoint's url
            "KeyCredential": "<API-KEY>", // Text Analytic (security) key
            "ConfidenceThreshold": 0.65, // Minimum threshold score for text analytic, value ranges from 0 to 1
            "IncludeStatistics": true // A flag to indicate whether if the service should return statistics with the results of the operation
        /// results of the operation
          }
        ]
      },
    // ...
  }

Next, in Program.cs or a similar entry point file in your project, add the following code.

// Entry point
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
   // ...
});

// ...

// Or others configuration providers...
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

builder.Services.AddDefaultCognitiveServiceProvider()
                .AddAzureTextAnalyticsLanguageDetectionServices(builder.Configuration);

The extension methods AddDefaultCognitiveServiceProvider and AddAzureTextAnalyticsLanguageDetectionServices manage the configuration to create instances of ICognitiveServiceProvider. With this, you can retrieve instances of ILanguageDetectionService (whose implementation is TextAnalyticsLanguageDetectionService). As seen in the configuration JSON, TextAnalyticsLanguageDetectionConfigurations is an array, allowing you to generate different Text Analytics Language Detection configurations and retrieve information based on the Name value. Now, you can inject ICognitiveServiceProvider through the constructor for use.

public class MyClass
{
    private readonly ILanguageDetectionService languageDetectionService;

    public MyClass(ICognitiveServiceProvider cognitiveServiceProvider)
    {
        // The value "DefaultAnalyticsLanguageDetection" is the name specified in the JSON from the previous code.
        // This is just an example code; avoid hardcoding strings :)
        languageDetectionService = cognitiveServiceProvider.GetLanguageDetectionService("DefaultAnalyticsLanguageDetection");
    }

    public async Task<CultureInfo> GetLanguageAsync(string userInput, CancellationToken cancellationToken)
    {
        var languageDetectionRequest = new LanguageDetectionRequest() { Text = new[] { new Text(value: userInput) } };

        var languageDetectionResult = await languageDetectionService.DetectLanguageAsync(languageDetectionRequest, CancellationToken.None);

        return languageDetectionResult.DetectedLanguages.MaxBy(d => d.ConfidenceScore).Language;
    }
}

Azure Translator

First, you need to add the TranslatorLanguageDetectionConfigurations to your project configuration. You can achieve this by using any configuration provider. The followng code is an example of how the settings would appear using the appsettings.json file:

  {
    // ...
    "TranslatorLanguageDetectionConfigurations": {
      "TranslatorLanguageDetectionService": [
        {
          "Name": "DefaultAnalyticsLanguageDetection", // Name of this configuration
          "EndpointUrl": "https://example.cognitiveservices.azure.com/", // Translator endpoint's url
          "KeyCredential": "<API-KEY>", // Translator Analytic (security) key
          "ConfidenceThreshold": 0.65, // Minimum threshold score for language detection, value ranges from 0 to 1
          "DetectOnlyTranslatableLanguages": true, // A flag to indicate whether any language detection result should only include languages that can be translated
          "RegionName": "westus" // Azure region name of the translator resource.
        }
      ]
    },
    // ...
  }

Next, in Program.cs or a similar entry point file in your project, add the following code.

// Entry point
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
   // ...
});

// ...

// Or others configuration providers...
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); 

builder.Services.AddDefaultCognitiveServiceProvider()
                .AddAzureTranslatorLanguageDetectionServices(builder.Configuration);

The extension methods AddDefaultCognitiveServiceProvider and AddAzureTranslatorLanguageDetectionServices manage the configuration to create instances of ICognitiveServiceProvider. With this, you can retrieve instances of ILanguageDetectionService (whose implementation is TranslatorLanguageDetectionService). As seen in the configuration JSON, TranslatorLanguageDetectionConfigurations is an array, allowing you to generate different Translator Language Detection configurations and retrieve the appropriate one based on the Name. Now, you can inject ICognitiveServiceProvider through the constructor for use.

public class MyClass
{
    private readonly ILanguageDetectionService languageDetectionService;

    public MyClass(ILanguageDetectionService languageDetectionService)
    {
        // The value "DefaultAnalyticsLanguageDetection" is the name specified in the JSON from the previous code.
        // This is just an example code; avoid hardcoding strings :)
        languageDetectionService = cognitiveServiceProvider.GetLanguageDetectionService("DefaultAnalyticsLanguageDetection");
    }

    public async Task<CultureInfo> GetLanguageAsync(string userInput, CancellationToken cancellationToken)
    {
        var languageDetectionRequest = new LanguageDetectionRequest() { Text = new[] { new Text(value: userInput) } };

        var languageDetectionResult = await languageDetectionService.DetectLanguageAsync(languageDetectionRequest, CancellationToken.None);

        return languageDetectionResult.DetectedLanguages.MaxBy(d => d.ConfidenceScore).Language;
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.2.0 73 10/22/2024
8.2.0-preview-01-m01 88 9/17/2024
8.1.9-preview-02 55 10/22/2024
8.1.9-preview-01 62 10/4/2024
8.1.8 81 9/23/2024
8.1.8-preview-07 69 9/12/2024
8.1.8-preview-06 94 9/11/2024
8.1.8-preview-05 87 9/10/2024
8.1.8-preview-04 84 8/16/2024
8.1.8-preview-03 97 8/13/2024
8.1.8-preview-02 98 8/13/2024
8.1.8-preview-01 101 8/12/2024
8.1.7 99 8/7/2024
8.1.7-preview-09 75 7/3/2024
8.1.7-preview-08 95 7/2/2024
8.1.7-preview-07 78 6/10/2024
8.1.7-preview-06 77 6/10/2024
8.1.7-preview-05 89 6/6/2024
8.1.7-preview-04 80 6/6/2024
8.1.7-preview-03 77 5/24/2024
8.1.7-preview-02 93 5/10/2024
8.1.7-preview-01 91 5/8/2024
8.1.6 115 5/7/2024
8.1.6-preview-08 59 5/2/2024
8.1.6-preview-07 90 4/29/2024
8.1.6-preview-06 86 4/26/2024
8.1.6-preview-05 72 4/24/2024
8.1.6-preview-04 70 4/22/2024
8.1.6-preview-03 94 4/22/2024
8.1.6-preview-02 90 4/17/2024
8.1.6-preview-01 82 4/15/2024
8.1.5 135 4/15/2024
8.1.5-preview-15 79 4/10/2024
8.1.5-preview-14 91 3/20/2024
8.1.5-preview-13 86 3/18/2024
8.1.5-preview-12 110 3/13/2024
8.1.5-preview-11 79 3/13/2024
8.1.5-preview-10 81 3/13/2024
8.1.5-preview-09 99 3/12/2024
8.1.5-preview-08 91 3/12/2024
8.1.5-preview-07 75 3/8/2024
8.1.5-preview-06 72 3/8/2024
8.1.5-preview-05 95 3/7/2024
8.1.5-preview-04 94 3/7/2024
8.1.5-preview-03 99 3/7/2024
8.1.5-preview-02 94 2/28/2024
8.1.5-preview-01 90 2/19/2024
8.1.4 126 2/15/2024
8.1.3 117 2/13/2024
8.1.3-preview-07 75 2/13/2024
8.1.3-preview-06 97 2/12/2024
8.1.3-preview-05 80 2/9/2024
8.1.3-preview-04 88 2/8/2024
8.1.3-preview-03 87 2/7/2024
8.1.3-preview-02 82 2/2/2024
8.1.3-preview-01 72 2/2/2024
8.1.2 114 2/1/2024
8.1.2-preview-9 92 1/22/2024
8.1.2-preview-8 69 1/19/2024
8.1.2-preview-7 64 1/19/2024
8.1.2-preview-6 82 1/19/2024
8.1.2-preview-5 93 1/19/2024
8.1.2-preview-4 77 1/19/2024
8.1.2-preview-3 82 1/18/2024
8.1.2-preview-2 70 1/18/2024
8.1.2-preview-16 69 1/31/2024
8.1.2-preview-15 80 1/31/2024
8.1.2-preview-14 71 1/25/2024
8.1.2-preview-13 91 1/25/2024
8.1.2-preview-12 77 1/23/2024
8.1.2-preview-11 90 1/23/2024
8.1.2-preview-10 75 1/22/2024
8.1.2-preview-1 92 1/18/2024
8.1.1 112 1/18/2024
8.1.0 88 1/18/2024
8.0.3 138 12/29/2023
8.0.1 104 12/14/2023
8.0.0 129 12/7/2023
6.0.4.3 121 12/29/2023
6.0.4.2 116 12/20/2023
6.0.4.1 83 12/19/2023
6.0.4 128 12/4/2023
6.0.3.20 117 11/27/2023
6.0.3.19 109 11/22/2023