prepareEntriesAbbyyLingvoFormat function

Future<Map<DictionaryHeading, List<DictionaryEntry>>> prepareEntriesAbbyyLingvoFormat(
  1. PrepareDictionaryParams params
)

Top-level function for use in compute. See DictionaryFormat for details.

Implementation

Future<Map<DictionaryHeading, List<DictionaryEntry>>>
    prepareEntriesAbbyyLingvoFormat(PrepareDictionaryParams params) async {
  Map<DictionaryHeading, List<DictionaryEntry>> entriesByHeading = {};

  String dictionaryFilePath =
      path.join(params.workingDirectory.path, 'dictionary.dsl');
  File dictionaryFile = File(dictionaryFilePath);

  String text = dictionaryFile
      .readAsStringSync()
      .replaceAll('<br>', '\n')
      .replaceAll('[', '<')
      .replaceAll(']', '>')
      .replaceAll('{{', '<')
      .replaceAll('}}', '>')
      .replaceAll('<m0>', '')
      .replaceAll('<m1>', ' ')
      .replaceAll('<m2>', '  ')
      .replaceAll('<m3>', '   ')
      .replaceAll('\\<', '<')
      .replaceAll('\\>', '>')
      .replaceAll('<<', '')
      .replaceAll('>>', '')
      .replaceAll(RegExp('<[^<]+?>'), '');

  List<String> lines = text.split('\n');

  String term = '';

  final buffer = StringBuffer();

  for (String line in lines) {
    if (line.startsWith('#')) {
      continue;
    }

    if (line.characters.isNotEmpty &&
        line.characters.first.codeUnits.first == 9) {
      buffer.writeln(line);
    } else {
      String definition = buffer.toString();
      buffer.clear();

      if (term.isNotEmpty && definition.isNotEmpty) {
        DictionaryHeading heading = DictionaryHeading(
          term: term,
        );
        DictionaryEntry entry = DictionaryEntry(
          definitions: [definition],
          entryTagNames: [],
          headingTagNames: [],
          popularity: 0,
        );

        entriesByHeading.putIfAbsent(heading, () => []);
        entriesByHeading[heading]!.add(entry);

        if (entriesByHeading.length % 1000 == 0) {
          params.send(t.import_found_entry(count: entriesByHeading.length));
        }
      }

      term = line.trim();
    }
  }

  params.send(t.import_found_entry(count: entriesByHeading.length));

  return entriesByHeading;
}