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
textstringThe text to analyze. It must contain at least one letter; otherwise, Undefined is returned.
languagesIEnumerable<Language>An optional collection of languages to consider. If not provided, the full range of languages in the Language enumeration is used.
cancellationTokenCancellationTokenAn 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(ImageBuffer, IEnumerable<Language>, CancellationToken)
Asynchronously detects the language of the content provided via an ImageBuffer.
public Task<Language> DetectLanguageAsync(ImageBuffer image, IEnumerable<Language> languages = null, CancellationToken cancellationToken = default)
Parameters
imageImageBufferAn ImageBuffer containing the image to analyze. Cannot be
null.languagesIEnumerable<Language>An optional collection of Language values to constrain detection. If
null, all supported languages in the Language enumeration are considered.cancellationTokenCancellationTokenA CancellationToken that may be used to cancel the operation.
Returns
- Task<Language>
A Task<TResult> whose result is the detected Language, or Undefined if no confident detection could be made.
Exceptions
- ArgumentNullException
Thrown if
imageisnull.- InvalidModelException
Thrown if the current language model does not support vision input required for image-based detection.
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
contentAttachmentAn Attachment that may contain text or image data to be analyzed.
languagesIEnumerable<Language>An optional collection of languages to consider during detection. If not provided, the full range of languages in the Language enumeration is used.
cancellationTokenCancellationTokenAn 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
contentis null.