prepareEntriesYomichanFormat function

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

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

Implementation

Future<Map<DictionaryHeading, List<DictionaryEntry>>>
    prepareEntriesYomichanFormat(PrepareDictionaryParams params) async {
  int structuredContentCount = 0;
  Map<DictionaryHeading, List<DictionaryEntry>> entriesByHeading = {};

  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_bank')) {
      List<dynamic> items = jsonDecode(file.readAsStringSync());

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

        double popularity = (item[4] as num).toDouble();
        List<String> entryTagNames = (item[2] as String).split(' ');
        List<String> headingTagNames = (item[7] as String).split(' ');

        List<String> definitions = [];

        if (item[5] is List) {
          List<dynamic> meaningsList = List.from(item[5]);
          definitions = meaningsList.map((e) {
            if (e is Map) {
              Map<String, dynamic> data = Map<String, dynamic>.from(e);
              if (data['type'] == 'image') {
                return '';
              } else if (data['type'] == 'structured-content') {
                structuredContentCount++;
                return '';
              } else {
                return e.toString().trim();
              }
            } else {
              return e.toString().trim();
            }
          }).toList();
        } else if (item[5] is Map) {
          Map<String, dynamic> data = Map<String, dynamic>.from(item[5]);
          if (data['type'] != 'image' && data['type'] != 'structured-content') {
            definitions.add(item[5].toString().trim());
          }
        } else {
          definitions.add(item[5].toString().trim());
        }

        definitions = definitions.where((e) => e.isNotEmpty).toList();

        if (definitions.isNotEmpty) {
          DictionaryHeading heading = DictionaryHeading(
            reading: reading,
            term: term,
          );
          DictionaryEntry entry = DictionaryEntry(
            definitions: definitions,
            entryTagNames: entryTagNames,
            headingTagNames: headingTagNames,
            popularity: popularity,
          );

          entriesByHeading.putIfAbsent(heading, () => []);
          entriesByHeading[heading]!.add(entry);
        }
      }
    } else if (filename.startsWith('kanji_bank')) {
      List<dynamic> items = jsonDecode(file.readAsStringSync());

      for (List<dynamic> item in items) {
        String term = item[0] as String;
        List<String> onyomis = (item[1] as String).split(' ');
        List<String> kunyomis = (item[2] as String).split(' ');
        List<String> headingTagNames = (item[3] as String).split(' ');
        List<String> meanings = List<String>.from(item[4]);

        StringBuffer buffer = StringBuffer();
        if (onyomis.join().trim().isNotEmpty) {
          buffer.write('音読み\n');
          for (String onyomi in onyomis) {
            buffer.write('  • $onyomi\n');
          }
          buffer.write('\n');
        }
        if (kunyomis.join().trim().isNotEmpty) {
          buffer.write('訓読み\n');
          for (String kun in kunyomis) {
            buffer.write('  • $kun\n');
          }
          buffer.write('\n');
        }
        if (meanings.isNotEmpty) {
          buffer.write('意味\n');
          for (String meaning in meanings) {
            buffer.write('  • $meaning\n');
          }
          buffer.write('\n');
        }

        String definition = buffer.toString().trim();

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

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

    if (entriesByHeading.isNotEmpty) {
      params.send(t.import_found_entry(count: entriesByHeading.length));
    }
  }

  if (structuredContentCount != 0) {
    params.sendAlert(
        message: t.structured_content_first(i: structuredContentCount));
    params.sendAlert(message: t.structured_content_second);
  }

  return entriesByHeading;
}