parseDictionary function

List<String> parseDictionary(
  1. String contents, {
  2. bool addSpace = true,
})

Parses a PP-OCR character dictionary file: one token per line, blank lines preserved as a single space (some dicts encode the space as an empty line). A trailing space token is appended when the file does not already end with one, matching PP-OCR's use_space_char default.

Implementation

List<String> parseDictionary(String contents, {bool addSpace = true}) {
  final lines = contents.split('\n');
  // Drop a single trailing empty line from the final newline.
  if (lines.isNotEmpty && lines.last.isEmpty) lines.removeLast();
  final out = [
    for (final line in lines) line.endsWith('\r') ? line.substring(0, line.length - 1) : line,
  ];
  if (addSpace && (out.isEmpty || out.last != ' ')) out.add(' ');
  return out;
}