Method DetectLanguageAsync
- Namespace
- LMKit.Translation
- Assembly
- LM-Kit.NET.dll
DetectLanguageAsync(string, IEnumerable<Language>, CancellationToken)
Asynchronously detects the language of the specified text.
public Task<Language> DetectLanguageAsync(string text, IEnumerable<Language> languages = null, CancellationToken cancellationToken = default)
Parameters
text
stringThe text to analyze. It must contain at least one letter; otherwise, Undefined is returned.
languages
IEnumerable<Language>An optional collection of languages to consider. If not provided, the full range of languages in the Language enumeration is used.
cancellationToken
CancellationTokenAn optional token for cancellation requests.
Returns
- Task<Language>
A task that represents the asynchronous operation. The task result is a member of the Language enumeration indicating the detected language, or Undefined if detection fails.
Examples
// Initialize the language model.
LM languageModel = new LM("path/to/your/model");
// Create an instance of TextTranslation.
TextTranslation translator = new TextTranslation(languageModel);
// Sample text for language detection.
string text = "Guten Morgen, wie geht es Ihnen?";
// Asynchronously detect the language.
Language detectedLanguage = await translator.DetectLanguageAsync(text);
Console.WriteLine($"Detected Language: {detectedLanguage}");
Remarks
The method processes the text by tokenizing, filtering non-letter tokens, and evaluating language probabilities via a categorization engine. It also applies additional refiners for languages with special characteristics (e.g., CJK and Cyrillic scripts).
DetectLanguageAsync(Attachment, IEnumerable<Language>, CancellationToken)
Asynchronously detects the language of the content provided via an Attachment.
public Task<Language> DetectLanguageAsync(Attachment content, IEnumerable<Language> languages = null, CancellationToken cancellationToken = default)
Parameters
content
AttachmentAn Attachment that may contain text or image data to be analyzed.
languages
IEnumerable<Language>An optional collection of languages to consider during detection. If not provided, the full range of languages in the Language enumeration is used.
cancellationToken
CancellationTokenAn optional token for cancellation requests.
Returns
- Task<Language>
A task representing the asynchronous operation. The task result is a member of the Language enumeration indicating the detected language, or Undefined if detection fails.
Remarks
When analyzing an Attachment, if the content includes image data, any embedded text is first extracted (via integrated OCR) and then processed to determine the language.
Exceptions
- InvalidModelException
Thrown if the current language model does not support vision input, which is required for analyzing image attachments.
- ArgumentNullException
Thrown if the provided
content
is null.