prepareEntriesMigakuFormat function

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

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

Implementation

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

  final List<FileSystemEntity> entities = params.workingDirectory.listSync();
  final Iterable<File> files = entities.whereType<File>();

  for (File file in files) {
    List<dynamic> items = List.from(jsonDecode(file.readAsStringSync()));

    for (dynamic item in items) {
      Map<String, dynamic> map = Map<String, dynamic>.from(item);

      String term = (map['term'] as String).trim();
      String definition = map['definition'] as String;

      definition = definition
          .replaceAll('<br>', '\n')
          .replaceAll(RegExp('<[^<]+?>'), '');

      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));
      }
    }
  }

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

  return entriesByHeading;
}