Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active July 29, 2022 16:41
OCR Image to Text and Spelling Correction in C# | OCR Spell Checker in C#
// This code example demonstrates how to get a list of misspelled word from recognized text.
// Path to the image to recognize
string imagePath = @"C:\Files\OCR\sample.png";
// Create OCR API
AsposeOcr api = new AsposeOcr();
// Initialize recognition settings
RecognitionSettings settings = new RecognitionSettings();
// Recognize image
RecognitionResult result = api.RecognizeImage(imagePath, settings);
// Get list of misspelled words with suggestions
List<SpellCheckError> errorsList = result.GetSpellCheckErrorList(SpellCheckLanguage.Eng);
foreach (var word in errorsList)
{
Console.WriteLine($"Misspelled Word - {word.Word}");
foreach (var suggest in word.SuggestedWords)
{
Console.WriteLine($"Suggested word - {suggest.Word}");
}
Console.WriteLine();
}
// This code example demonstrates how to save the recognized text with spelling correction.
// Path to the image to recognize
string imagePath = @"C:\Files\OCR\sample.png";
string resultPath = @"C:\Files\OCR\MyResult.txt";
// Create OCR API
AsposeOcr api = new AsposeOcr();
// Initialize recognition settings
RecognitionSettings settings = new RecognitionSettings();
// Recognize text from an image
RecognitionResult result = api.RecognizeImage(imagePath, new RecognitionSettings());
// Save the corrected text
result.Save(resultPath, SaveFormat.Text, true, SpellCheckLanguage.Eng);
// This code example demonstrates how to autocorrect spelling errors from a recognized text.
// Path to the image to recognize
string imagePath = @"C:\Files\OCR\sample.png";
// Create OCR API
AsposeOcr api = new AsposeOcr();
// Initialize recognition settings
RecognitionSettings settings = new RecognitionSettings();
// Recognize text from an image
RecognitionResult result = api.RecognizeImage(imagePath, new RecognitionSettings());
// Get corrected result
string correctedResult = result.GetSpellCheckCorrectedText(SpellCheckLanguage.Eng);
// Show results
Console.WriteLine(correctedResult);
// This code example demonstrates how to run spell check on custom text string.
// Text for spell check
string textToCorrect = "This is sample text wth errrors";
// Create OCR API
AsposeOcr api = new AsposeOcr();
// Run Spell Check to correct errors
string correctedText = api.CorrectSpelling(textToCorrect, SpellCheckLanguage.Eng);
// Show results
Console.WriteLine(correctedText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment