WIP: generate matchspans for word search results
Some checks failed
Build database / evals (push) Successful in 10m57s
Run tests / evals (push) Has been cancelled

This commit is contained in:
2026-02-24 16:54:37 +09:00
parent 00b963bfed
commit 847144d228
3 changed files with 45 additions and 4 deletions

View File

@@ -0,0 +1,23 @@
enum WordSearchMatchSpanType { kanji, kana, sense }
///
class WordSearchMatchSpan {
/// Which type of item that this span is meant for.
final WordSearchMatchSpanType spanType;
///
final int index;
/// The start of the span (inclusive)
final int start;
/// The end of the span (inclusive)
final int end;
WordSearchMatchSpan({
required this.spanType,
required this.index,
required this.start,
required this.end,
});
}

View File

@@ -1,9 +1,11 @@
import 'package:jadb/models/common/jlpt_level.dart';
import 'package:jadb/models/jmdict/jmdict_kanji_info.dart';
import 'package:jadb/models/jmdict/jmdict_reading_info.dart';
import 'package:jadb/models/word_search/word_search_match_span.dart';
import 'package:jadb/models/word_search/word_search_ruby.dart';
import 'package:jadb/models/word_search/word_search_sense.dart';
import 'package:jadb/models/word_search/word_search_sources.dart';
import 'package:jadb/search/word_search/word_search.dart';
/// A class representing a single dictionary entry from a word search.
class WordSearchResult {
@@ -34,11 +36,15 @@ class WordSearchResult {
/// A class listing the sources used to make up the data for this word search result.
final WordSearchSources sources;
// TODO: Create a list containing pointers to the matched parts of the word (either kanjiInfo, readingInfo, senses),
// as well as spans for the subpart of the string that matched. This will be used for highlighting, and displaying
// alternative kanji/kana forms later on.
/// A list of spans, specifying which part of this word result matched the search keyword.
///
/// Note that this is considered ephemeral data - it does not originate from the dictionary,
/// and unlike the rest of the class it varies based on external information (the searchword).
/// It will *NOT* be exported to JSON, but can be reinferred by invoking [inferMatchSpans] with
/// the original searchword.
List<WordSearchMatchSpan>? matchSpans;
const WordSearchResult({
WordSearchResult({
required this.score,
required this.entryId,
required this.isCommon,
@@ -48,6 +54,7 @@ class WordSearchResult {
required this.senses,
required this.jlptLevel,
required this.sources,
this.matchSpans,
});
Map<String, dynamic> toJson() => {
@@ -97,6 +104,13 @@ class WordSearchResult {
sources: WordSearchSources.empty(),
);
void inferMatchSpans(
String searchword, {
SearchMode searchMode = SearchMode.Auto,
}) {
throw UnimplementedError();
}
String _formatJapaneseWord(WordSearchRuby word) =>
word.furigana == null ? word.base : '${word.base} (${word.furigana})';

View File

@@ -52,6 +52,10 @@ Future<List<WordSearchResult>?> searchWordWithDbConnection(
linearWordQueryData: linearWordQueryData,
);
for (final resultEntry in result) {
resultEntry.inferMatchSpans(word, searchMode: searchMode);
}
return result;
}