Class TextTranslation
- Namespace
- LMKit.Translation
- Assembly
- LM-Kit.NET.dll
Provides methods for translating text between languages and detecting the language of provided content.
public sealed class TextTranslation
- Inheritance
-
TextTranslation
- Inherited Members
Examples
Example: Basic text translation
using LMKit.Model;
using LMKit.Translation;
using LMKit.TextGeneration;
using System;
// Load the language model
LM model = LM.LoadFromModelID("llama-3.2-1b");
// Create the translator
TextTranslation translator = new TextTranslation(model);
// Translate text to French
string englishText = "Hello, how are you today?";
string frenchText = translator.Translate(englishText, Language.French);
Console.WriteLine($"Original: {englishText}");
Console.WriteLine($"Translated: {frenchText}");
Example: Language detection
using LMKit.Model;
using LMKit.Translation;
using LMKit.TextGeneration;
using System;
LM model = LM.LoadFromModelID("llama-3.2-1b");
TextTranslation translator = new TextTranslation(model);
// Detect language from text
string unknownText = "Bonjour, comment allez-vous?";
Language detected = translator.DetectLanguage(unknownText);
Console.WriteLine($"Detected language: {detected}");
Console.WriteLine($"Confidence: {translator.Confidence:P1}");
Example: Translate with specific language candidates
using LMKit.Model;
using LMKit.Translation;
using LMKit.TextGeneration;
using System;
using System.Collections.Generic;
LM model = LM.LoadFromModelID("llama-3.2-1b");
TextTranslation translator = new TextTranslation(model);
// Detect from a limited set of languages for faster detection
string text = "Guten Morgen!";
var candidates = new List<Language> { Language.English, Language.German, Language.French };
Language detected = translator.DetectLanguage(text, candidates);
Console.WriteLine($"Detected: {detected}"); // Output: German
// Translate to English
string translated = translator.Translate(text, Language.English);
Console.WriteLine($"Translation: {translated}"); // Output: Good morning!
Remarks
The TextTranslation class leverages advanced language models to perform text translation and language detection. It preserves the original layout and formatting of the input content while processing and translating. Language detection is supported for both plain text and for content stored in Attachment objects (such as images).
Key Features
- Translate text to any supported language via Translate(string, Language, CancellationToken)
- Detect language from text via DetectLanguage(string, IEnumerable<Language>, CancellationToken)
- Detect language from images and attachments
- Automatic text chunking for long documents
- Preserves original formatting during translation
- Confidence scoring for language detection
Supported Languages
The class supports all languages defined in the Language enumeration, including
English, French, German, Spanish, Chinese, Japanese, Korean, Arabic, Russian, and many more.
Constructors
- TextTranslation(LM)
Initializes a new instance of the TextTranslation class with the specified language model.
Properties
- Confidence
Gets the confidence score from the most recent language detection operation.
- Model
Gets the LM instance associated with this TextTranslation object.
Methods
- DetectLanguage(Attachment, IEnumerable<Language>, CancellationToken)
Synchronously detects the language of the content provided via an Attachment.
- DetectLanguage(ImageBuffer, IEnumerable<Language>, CancellationToken)
Detects the language of the content provided via an ImageBuffer.
- DetectLanguage(string, IEnumerable<Language>, CancellationToken)
Detects the language of the specified text synchronously.
- DetectLanguageAsync(Attachment, IEnumerable<Language>, CancellationToken)
Asynchronously detects the language of the content provided via an Attachment.
- DetectLanguageAsync(ImageBuffer, IEnumerable<Language>, CancellationToken)
Asynchronously detects the language of the content provided via an ImageBuffer.
- DetectLanguageAsync(string, IEnumerable<Language>, CancellationToken)
Asynchronously detects the language of the specified text.
- GetTrainingData(TrainingDataset, int, bool, int?)
Retrieves training data for fine-tuning language detection models from the specified dataset.
- Translate(string, Language, CancellationToken)
Translates the specified text into the target language synchronously while preserving the original layout.
- TranslateAsync(string, Language, CancellationToken)
Asynchronously translates the specified text into the target language while preserving its original formatting.
Events
- AfterTextCompletion
Occurs after a text completion operation (translation or language detection) has been performed.