halkyon/halkyon_learn_curator/test/mecab_test.dart
Tizian 「ティツィアーン」 1d2011a676
feat(dictionary): Japanese cut into words and looked up on the device
Once the person agrees, the device holds MeCab's IPADIC and the
instance's JMdict and cuts lines as Jisho.org does, so their text never
leaves it (ADR 0012). MeCab comes through mecab_for_dart, whose build
hook compiles it; its tokens are grouped by Ve's rules, written anew in
Dart, with に kept a word of its own. Each word is found as it stands or
as a form of a word, else by its dictionary form, else as the longest
words it holds from token to token; longer words over token boundaries
are offered as before.

The curator packs Ubuntu's compiled IPADIC beside the dictionary files
(curate ipadic, and every update run). The server exports its JMdict
from its tables, so the device finds the same keys and keeps the same
versions, and serves both at /device-dictionaries/, named in
dictionary.deviceFiles('ja') with their hashes; Caddy forwards the path.

The app checks each file against its hash, unpacks IPADIC, builds a
SQLite database from the export on an isolate, and asks the instance
for newer files every 30 days, downloading only what changed. The Watch
screen offers the download once per device with its size (34 MB, some
150 MB once set up); Settings downloads and removes them. Without them,
or should cutting on the device fail, lines go to the instance as
before. MeCab's, IPADIC's and Ve's licences join the licence list.

Every build now needs a C++ compiler; mecab_for_dart holds code_assets
at 1.x.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-10-17 19:46:30 +02:00

86 lines
2.6 KiB
Dart

import 'dart:io';
import 'package:archive/archive.dart';
import 'package:halkyon_learn_curator/src/mecab.dart';
import 'package:test/test.dart';
void main() {
late Directory compiled;
late Directory into;
setUp(() async {
compiled = await Directory.systemTemp.createTemp('compiled');
into = await Directory.systemTemp.createTemp('dictionaries');
for (final name in [
'char.bin',
'dicrc',
'matrix.bin',
'sys.dic',
'unk.dic',
]) {
await File('${compiled.path}/$name').writeAsString('$name of IPADIC');
}
});
tearDown(() async {
await compiled.delete(recursive: true);
await into.delete(recursive: true);
});
test('packs the files MeCab reads, the licence and an empty mecabrc into '
'one gzipped tar beside the dictionary files', () async {
final licence = File('${compiled.path}/copyright')
..writeAsStringSync('Copyright 2000, 2001, 2002, 2003 Nara Institute');
final packed = await packIpadic(compiled, into, licence: licence);
expect(packed?.path, '${into.path}/$ipadicFile');
final files = {
for (final file in TarDecoder().decodeBytes(
const GZipDecoder().decodeBytes(packed!.readAsBytesSync()),
))
file.name: String.fromCharCodes(file.content),
};
expect(files.keys, {
'char.bin',
'dicrc',
'matrix.bin',
'sys.dic',
'unk.dic',
'COPYING',
'mecabrc',
});
expect(files['sys.dic'], 'sys.dic of IPADIC');
expect(files['COPYING'], startsWith('Copyright'));
expect(files['mecabrc'], isEmpty);
// Nothing the server would load as a dictionary.
expect(
into.listSync().where(
(file) =>
file.path.endsWith('.jsonl') || file.path.endsWith('.jsonl.gz'),
),
isEmpty,
);
});
test('leaves the pack as it is when the dictionary is the same, so devices '
'download it again only once it changed', () async {
final first = await packIpadic(compiled, into);
final bytes = first!.readAsBytesSync();
expect(await packIpadic(compiled, into), isNull);
expect(File('${into.path}/$ipadicFile').readAsBytesSync(), bytes);
await File('${compiled.path}/sys.dic').writeAsString('a newer IPADIC');
expect(await packIpadic(compiled, into), isNotNull);
});
test('refuses a dictionary missing a file MeCab reads', () async {
await File('${compiled.path}/matrix.bin').delete();
await expectLater(
packIpadic(compiled, into),
throwsA(isA<FileSystemException>()),
);
expect(File('${into.path}/$ipadicFile').existsSync(), isFalse);
});
}