Table of Contents

Method DetectLanguageAsync

Namespace
LMKit.Translation
Assembly
LM-Kit.NET.dll

DetectLanguageAsync(string, IEnumerable<Language>, CancellationToken)

Asynchronously detects the language of the specified plain text.

public Task<Language> DetectLanguageAsync(string text, IEnumerable<Language> languages = null, CancellationToken cancellationToken = default)

Parameters

text string

The text for which to detect the language.

languages IEnumerable<Language>

An optional collection of languages to consider during the operation. If omitted, the method defaults to utilizing the full range of languages specified within the Language enumeration, thus encompassing all supported languages.

cancellationToken CancellationToken

Optional. A CancellationToken for handling cancellation requests.

Returns

Task<Language>

A task representing the asynchronous operation, containing the detected language as a member of the Language enumeration. If the language cannot be determined, the Undefined member will be returned.

Examples

using LMKit.Translation;
using LMKit.Model;
using System;
using System.Threading.Tasks;

// Initialize the language model (LLM)
LLM languageModel = new LLM("path/to/your/model");

// Create an instance of TextTranslation using the LLM
TextTranslation textTranslation = new TextTranslation(languageModel);

// Text for language detection
string text = "Guten Morgen, wie geht es Ihnen?";

// Detect the language asynchronously
Language detectedLanguage = await textTranslation.DetectLanguageAsync(text);

Console.WriteLine($"Detected Language: {detectedLanguage}");
// Output: "Detected Language: German"

Remarks

This method analyzes the text content and identifies its language, supporting both single and multiline texts with various formatting. It leverages various language detection algorithms to determine the language from a set of possible languages.