lib/word_search: calculate isCommon

This commit is contained in:
2025-05-14 20:59:57 +02:00
parent 8299572225
commit b07fc8f4b3
3 changed files with 36 additions and 0 deletions

View File

@@ -10,6 +10,9 @@ class WordSearchResult {
/// The ID of the entry in the database.
final int entryId;
/// Whether the word is common or not.
final bool isCommon;
/// The variants of the word in Japanese.
final List<WordSearchRuby> japanese;
@@ -30,6 +33,7 @@ class WordSearchResult {
const WordSearchResult({
required this.entryId,
required this.isCommon,
required this.japanese,
required this.kanjiInfo,
required this.readingInfo,
@@ -40,6 +44,7 @@ class WordSearchResult {
Map<String, dynamic> toJson() => {
'entryId': entryId,
'isCommon': isCommon,
'japanese': japanese.map((e) => e.toJson()).toList(),
'kanjiInfo':
kanjiInfo.map((key, value) => MapEntry(key, value.toJson())),
@@ -53,6 +58,7 @@ class WordSearchResult {
factory WordSearchResult.fromJson(Map<String, dynamic> json) =>
WordSearchResult(
entryId: json['entryId'] as int,
isCommon: json['isCommon'] as bool,
japanese: (json['japanese'] as List<dynamic>)
.map((e) => WordSearchRuby.fromJson(e))
.toList(),

View File

@@ -81,11 +81,19 @@ Future<List<WordSearchResult>?> searchWordWithDbConnection(
where: 'entryId IN (${entryIds.join(',')})',
);
late final List<Map<String, Object?>> commonEntries;
final Future<List<Map<String, Object?>>> commonEntries_query =
connection.query(
'JMdict_EntryCommon',
where: 'entryId IN (${entryIds.join(',')})',
);
await Future.wait([
senses_query.then((value) => senses = value),
readingElements_query.then((value) => readingElements = value),
kanjiElements_query.then((value) => kanjiElements = value),
jlptTags_query.then((value) => jlptTags = value),
commonEntries_query.then((value) => commonEntries = value),
]);
// Sense queries
@@ -267,6 +275,7 @@ Future<List<WordSearchResult>?> searchWordWithDbConnection(
readingElements: readingElements,
kanjiElements: kanjiElements,
jlptTags: jlptTags,
commonEntries: commonEntries,
senses: senses,
senseAntonyms: senseAntonyms,
senseDialects: senseDialects,
@@ -291,6 +300,7 @@ List<WordSearchResult> _regroupWordSearchResults({
required List<Map<String, Object?>> readingElements,
required List<Map<String, Object?>> kanjiElements,
required List<Map<String, Object?>> jlptTags,
required List<Map<String, Object?>> commonEntries,
required List<Map<String, Object?>> senses,
required List<Map<String, Object?>> senseAntonyms,
required List<Map<String, Object?>> senseDialects,
@@ -310,6 +320,9 @@ List<WordSearchResult> _regroupWordSearchResults({
}) {
final List<WordSearchResult> results = [];
final commonEntryIds =
commonEntries.map((entry) => entry['entryId'] as int).toSet();
for (final entryId in entryIds) {
final List<Map<String, Object?>> entryReadingElements = readingElements
.where((element) => element['entryId'] == entryId)
@@ -328,6 +341,8 @@ List<WordSearchResult> _regroupWordSearchResults({
.firstOrNull ??
JlptLevel.none;
final isCommon = commonEntryIds.contains(entryId);
final List<Map<String, Object?>> entrySenses =
senses.where((element) => element['entryId'] == entryId).toList();
@@ -359,6 +374,7 @@ List<WordSearchResult> _regroupWordSearchResults({
results.add(
WordSearchResult(
entryId: entryId,
isCommon: isCommon,
japanese: entryReadingElementsGrouped.rubys,
kanjiInfo: entryReadingElementsGrouped.kanjiInfos,
readingInfo: entryReadingElementsGrouped.readingInfos,

View File

@@ -281,3 +281,17 @@ SELECT
FROM "JMdict_Entry"
LEFT JOIN "JMdict_KanjiElement" USING("entryId")
LEFT JOIN "JMdict_ReadingElement" USING("entryId");
CREATE VIEW "JMdict_EntryCommon"("entryId")
AS
SELECT DISTINCT "entryId"
FROM "JMdict_KanjiElement"
FULL OUTER JOIN "JMdict_ReadingElement" USING("entryId")
WHERE "JMdict_ReadingElement"."news" = 1
OR "JMdict_ReadingElement"."ichi" = 1
OR "JMdict_ReadingElement"."spec" = 1
OR "JMdict_ReadingElement"."gai" = 1
OR "JMdict_KanjiElement"."news" = 1
OR "JMdict_KanjiElement"."ichi" = 1
OR "JMdict_KanjiElement"."spec" = 1
OR "JMdict_KanjiElement"."gai" = 1;