applyCorrection function

String applyCorrection(
  1. String inputParagraph,
  2. bool applyDictionary
)

Applies dictionary-based correction to the extracted text.

This function improves recognition accuracy by comparing extracted text against a dictionary and correcting likely missed recognitions.

inputParagraph is the raw text extracted from the image, which may contain multiple lines. Returns the corrected text after dictionary-based processing.

Implementation

String applyCorrection(
  final String inputParagraph,
  final bool applyDictionary,
) {
  /// Map of commonly confused characters and their possible substitutions.
  /// Keys are characters that might be incorrectly recognized, and values are lists
  /// of possible correct characters to try as replacements.
  const Map<String, List<String>> correctionLetters = {
    '0': ['O', 'o', 'B', '8'],
    '5': ['S', 's'],
    'l': ['L', '1', 'i', '!'],
    'S': ['5'],
    'o': ['D', '0'],
    'O': ['D', '0'],
    '!': ['T', 'I', 'i', 'l', '1'],
    '@': ['A', 'a'],
  };
  final linesOfText = inputParagraph.split('\n');
  final List<String> correctedBlob = [];

  for (final String sentence in linesOfText) {
    String correctedSentence = sentenceFixZeroAnO(sentence);

    if (applyDictionary) {
      correctedBlob.add(
        applyDictionaryCorrectionOnSingleSentence(
          correctedSentence,
          correctionLetters,
        ),
      );
    } else {
      correctedBlob.add(correctedSentence);
    }
  }
  return correctedBlob.join('\n');
}