Files
jadb/lib/_data_ingestion/tanos-jlpt/csv_parser.dart
h7x4 2ad1e038f1
All checks were successful
Build and test / build (push) Successful in 13m41s
tanos-jlpt: remove flatten from xml stream
This was earlier used to compensate for a double nesting bug. This has
been fixed in the latest version of the xml package.
2026-04-01 16:04:44 +09:00

74 lines
1.9 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:convert';
import 'dart:io';
import 'package:csv/csv.dart';
import 'package:jadb/_data_ingestion/tanos-jlpt/objects.dart';
import 'package:xml/xml_events.dart';
Future<List<JLPTRankedWord>> parseJLPTRankedWords(
Map<String, File> files,
) async {
final List<JLPTRankedWord> result = [];
final codec = Csv(
fieldDelimiter: ',',
lineDelimiter: '\n',
quoteMode: QuoteMode.strings,
escapeCharacter: '\\',
parseHeaders: false,
);
for (final entry in files.entries) {
final jlptLevel = entry.key;
final file = entry.value;
if (!file.existsSync()) {
throw Exception('File $jlptLevel does not exist');
}
final words = await file
.openRead()
.transform(utf8.decoder)
.transform(codec.decoder)
.map((row) {
if (row.length != 3) {
throw Exception('Invalid line in $jlptLevel: $row');
}
return row;
})
.map((row) => row.map((e) => e as String).toList())
.map((row) {
final kanji = row[0].isEmpty
? null
: row[0]
.replaceFirst(RegExp('^お・'), '')
.replaceAll(RegExp(r'.*'), '');
final readings = row[1]
.split(RegExp('[・/、(:?s+)]'))
.map((e) => e.trim())
.toList();
final meanings = row[2].split(',').expand(cleanMeaning).toList();
return JLPTRankedWord(
readings: readings,
kanji: kanji,
jlptLevel: jlptLevel,
meanings: meanings,
);
})
.toList();
result.addAll(words);
}
return result;
}
List<String> cleanMeaning(String meaning) {
final initialTrim = meaning.trim().replaceAll(RegExp(r'^\d.\s+'), '');
final woParens = initialTrim.replaceAll(RegExp(r'\s*\(.*?\)\s*'), '');
return {initialTrim, woParens}.toList();
}