preparePitchesYomichanFormat function

Future<Map<DictionaryHeading, List<DictionaryPitch>>> preparePitchesYomichanFormat(
  1. PrepareDictionaryParams params
)

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

Implementation

Future<Map<DictionaryHeading, List<DictionaryPitch>>>
    preparePitchesYomichanFormat(PrepareDictionaryParams params) async {
  Map<DictionaryHeading, List<DictionaryPitch>> pitchesByHeading = {};
  final List<FileSystemEntity> entities = params.workingDirectory.listSync();
  final Iterable<File> files = entities.whereType<File>();

  for (File file in files) {
    String filename = path.basename(file.path);
    if (!filename.startsWith('term_meta_bank')) {
      continue;
    }

    String json = file.readAsStringSync();
    List<dynamic> items = jsonDecode(json);

    for (List<dynamic> item in items) {
      String term = item[0] as String;
      String type = item[1] as String;

      if (type == 'pitch') {
        Map<String, dynamic> data = Map<String, dynamic>.from(item[2]);
        String reading = data['reading'] ?? '';
        DictionaryHeading heading = DictionaryHeading(
          term: term,
          reading: reading,
        );
        pitchesByHeading.putIfAbsent(heading, () => []);

        List<Map<String, dynamic>> distinctPitchJsons =
            List<Map<String, dynamic>>.from(data['pitches']);
        for (Map<String, dynamic> distinctPitch in distinctPitchJsons) {
          int downstep = distinctPitch['position'];
          DictionaryPitch pitch = DictionaryPitch(downstep: downstep);

          pitchesByHeading[heading]!.add(pitch);
        }
      } else {
        continue;
      }
    }

    if (pitchesByHeading.isNotEmpty) {
      params.send(t.import_found_pitch(count: pitchesByHeading.length));
    }
  }

  return pitchesByHeading;
}