From dcf6469eba047037a2bacd45fa182952f4f08091 Mon Sep 17 00:00:00 2001 From: elianiva Date: Wed, 8 Jun 2022 08:59:27 +0700 Subject: [PATCH] feat: better sorting rule --- .../lib/repository/jmdict_repository.dart | 71 ++++++++++++------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/jisho_lens/lib/repository/jmdict_repository.dart b/jisho_lens/lib/repository/jmdict_repository.dart index 8a4f44e..149441c 100644 --- a/jisho_lens/lib/repository/jmdict_repository.dart +++ b/jisho_lens/lib/repository/jmdict_repository.dart @@ -8,26 +8,8 @@ import 'package:jisho_lens/models/vocab.dart'; import 'package:jisho_lens/services/sqlite_client.dart'; class JMDictRepository { - Future importDbFile(String? sourcePath) async { - if (sourcePath == null) { - throw const FileSystemException("File path is null"); - } - - final sourceFile = File(sourcePath); - final dbPath = await SqliteClient.instance.path; - await sourceFile.copy(dbPath); - } - - Future removeDbFile() async { - final dbPath = await SqliteClient.instance.path; - final dbFile = File(dbPath); - final isDbFileExists = await dbFile.exists(); - if (!isDbFileExists) { - throw const FileSystemException("The database file doesn't exist."); - } - - await dbFile.delete(); - } + final _nonNumberPattern = RegExp(r'\D+'); + static final _latinLettersPattern = RegExp(r'[a-zA-Z]'); static const _glossariesPredicateFuzzy = ''' JMdictSense.Glossaries in ( @@ -51,7 +33,26 @@ class JMDictRepository { OR JMdictKanji.KanjiText = ? '''; - static final _latinLettersRE = RegExp(r'[a-zA-Z]'); + Future importDbFile(String? sourcePath) async { + if (sourcePath == null) { + throw const FileSystemException("File path is null"); + } + + final sourceFile = File(sourcePath); + final dbPath = await SqliteClient.instance.path; + await sourceFile.copy(dbPath); + } + + Future removeDbFile() async { + final dbPath = await SqliteClient.instance.path; + final dbFile = File(dbPath); + final isDbFileExists = await dbFile.exists(); + if (!isDbFileExists) { + throw const FileSystemException("The database file doesn't exist."); + } + + await dbFile.delete(); + } Future findByKeyword({ required String keyword, @@ -60,7 +61,7 @@ class JMDictRepository { final db = await SqliteClient.instance.db; if (db == null) return null; - final isSearchingKana = _latinLettersRE.hasMatch(keyword) == false; + final isSearchingKana = _latinLettersPattern.hasMatch(keyword) == false; final stopwatch = Stopwatch(); stopwatch.start(); @@ -89,9 +90,7 @@ class JMDictRepository { (${isSearchingKana ? (fuzzy ? _kanaPredicateFuzzy : _kanaPredicateExact) : _glossariesPredicateFuzzy}) ORDER BY -- Show lesser kanji first because it's more likely to be what the user is looking for - LENGTH(JMdictKanji.KanjiText) ASC, - -- More "priorities" means more frequent usage - LENGTH(JmdictKanji.Priorities) DESC; + LENGTH(JMdictKanji.KanjiText) ASC; ''', isSearchingKana ? [keyword, keyword] : [keyword]); stopwatch.stop(); @@ -194,6 +193,28 @@ class JMDictRepository { ); }).toList(); + // can't do this sorting in the database because it's a bit complex + // sort by frequency. + // e.g ichi1,nf01 will ge higher priority than ichi2,nf07 + // ichi2,nf07 will be higher than ichi1 + results.sort((a, b) { + // resolve same length priorities by their ranking + if (a.priorities.length == b.priorities.length) { + final aScore = a.priorities.fold(0, (prev, p) { + return int.parse(p.replaceAll(_nonNumberPattern, "")); + }); + final bScore = b.priorities.fold(0, (prev, p) { + return int.parse(p.replaceAll(_nonNumberPattern, "")); + }); + + // higher rank will appear first + return aScore - bScore; + } + + // more priorities label will appear first + return b.priorities.length - a.priorities.length; + }); + return results; } }