prepareFrequenciesYomichanFormat function
- PrepareDictionaryParams params
Top-level function for use in compute. See DictionaryFormat for details.
Implementation
Future<Map<DictionaryHeading, List<DictionaryFrequency>>>
prepareFrequenciesYomichanFormat(PrepareDictionaryParams params) async {
Map<DictionaryHeading, List<DictionaryFrequency>> frequenciesByHeading = {};
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;
late DictionaryHeading heading;
late DictionaryFrequency frequency;
if (type == 'freq') {
if (item[2] is double) {
double number = item[2] as double;
if (number % 1 == 0) {
heading = DictionaryHeading(term: term);
frequency = DictionaryFrequency(
value: number,
displayValue: '${number.toInt()}',
);
} else {
heading = DictionaryHeading(term: term);
frequency = DictionaryFrequency(
value: number,
displayValue: '$number',
);
}
} else if (item[2] is int) {
int number = item[2] as int;
heading = DictionaryHeading(term: term);
DictionaryFrequency(
value: number.toDouble(),
displayValue: '$number',
);
frequency = DictionaryFrequency(
value: number.toDouble(),
displayValue: '$number',
);
} else if (item[2] is Map) {
Map<String, dynamic> data = Map<String, dynamic>.from(item[2]);
if (data['reading'] != null && data['frequency'] is Map) {
Map<String, dynamic> frequencyData =
Map<String, dynamic>.from(data['frequency']);
String reading = data['reading'] ?? '';
heading = DictionaryHeading(
term: term,
reading: reading,
);
num number = frequencyData['value'] ?? 0;
frequency = DictionaryFrequency(
value: number.toDouble(),
displayValue: frequencyData['displayValue'],
);
} else if (data['displayValue'] != null) {
String reading = data['reading'] ?? '';
heading = DictionaryHeading(
term: term,
reading: reading,
);
num number = data['value'] ?? 0;
frequency = DictionaryFrequency(
value: number.toDouble(),
displayValue: data['displayValue'],
);
} else if (data['value'] != null) {
String reading = data['reading'] ?? '';
heading = DictionaryHeading(
term: term,
reading: reading,
);
num number = data['value'] ?? 0;
frequency = DictionaryFrequency(
value: number.toDouble(),
displayValue: number.toInt().toString(),
);
} else if (data['frequency'] is num) {
num frequencyValue = data['frequency'];
String reading = data['reading'] ?? '';
heading = DictionaryHeading(
term: term,
reading: reading,
);
if (frequencyValue % 1 == 0) {
frequency = DictionaryFrequency(
value: frequencyValue.toDouble(),
displayValue: frequencyValue.toInt().toString(),
);
} else {
frequency = DictionaryFrequency(
value: frequencyValue.toDouble(),
displayValue: frequencyValue.toDouble().toString(),
);
}
}
} else {
heading = DictionaryHeading(term: term);
frequency = DictionaryFrequency(
value: 0,
displayValue: item[2].toString(),
);
}
frequenciesByHeading.putIfAbsent(heading, () => []);
frequenciesByHeading[heading]!.add(frequency);
} else {
continue;
}
}
if (frequenciesByHeading.isNotEmpty) {
params.send(t.import_found_frequency(count: frequenciesByHeading.length));
}
}
return frequenciesByHeading;
}