autocorrectWords static method

String autocorrectWords(
  1. String text, {
  2. Map<String, String>? customDictionary,
})

Performs dictionary-assisted orthographic correction on known high-frequency Arabic words and user-registered words, fixing Hamzat al-Qat' vs Wasl, Taa' Marboota vs Haa', and Alif Maqsoora vs Yaa', including attached prefixes (ال, و, ب, ل).

Optionally accepts a customDictionary for local, per-call overrides.

Example:

AppArabicOrthography.autocorrectWords('يرجى التحقق من اعدادات الخدمه وارسال الرساله');
// Output: 'يرجى التحقق من إعدادات الخدمة وإرسال الرسالة'

Implementation

static String autocorrectWords(
  String text, {
  Map<String, String>? customDictionary,
}) {
  if (text.isEmpty) return text;

  // Splits text into tokens while preserving separators and punctuation, keeping Tashkeel with word
  final matches = _tokenRegex.allMatches(text);

  final buffer = StringBuffer();

  for (final match in matches) {
    final token = match.group(0)!;

    // Only inspect Arabic word tokens
    if (_arabicWordTokenRegex.hasMatch(token)) {
      buffer.write(_correctSingleWord(token, customDictionary));
    } else {
      buffer.write(token);
    }
  }

  // Collapse any accidental duplicate Tashkeel marks
  var result = buffer.toString();
  result = result.replaceAllMapped(
    _duplicateTashkeelRegex,
    (m) => m[1]!,
  );

  return result;
}