Skip to content

Commit 2a8323e

Browse files
committed
udated sorting for the new search strategy
1 parent 896edb9 commit 2a8323e

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

lib/application/dictionary/dictionary_search.dart

+31-18
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,38 @@ import 'package:collection/collection.dart';
55
import 'package:database_builder/database_builder.dart';
66
import 'package:tuple/tuple.dart';
77

8-
/// Sorts a list of Jmdict entries given a query text. The order is determined
8+
/// Sorts a list of Jmdict entries given a query text.
9+
/// If it is a search *without* wildcards the order is determined
910
/// by those sorting criteria:
1011
///
11-
/// 1. Full Match > Match at the beginning > Match somwhere in the word
12-
/// Those three categories are sorted individually and merged in the end
13-
/// 2. sort inside each category based on <br/>
12+
/// 1. level sorting criteria:
13+
/// 1 - Matches original search term
14+
/// 2 - Matches search term converted to kana
15+
/// 3 - Matches deconjugated search term
16+
/// 2. level sorting criteria:
17+
/// x.1 - full match
18+
/// x.2 - match at the beginning
19+
/// x.3 - matches anywhere
20+
/// 3. level sorting criteria
21+
/// 1. word frequency
22+
///
23+
/// If it is a search *with* a wildcard the order is determined by
24+
/// First level sorting criteria:
1425
/// 1. word frequency
1526
List<List<JMdict>> sortJmdictList(
1627
List<JMdict> entries, String query, String? queryKana, String? queryDeconjugated,
1728
List<String> languages
1829
){
1930

20-
/// lists with three sub lists
21-
/// 0 - full matchs
22-
/// 1 - matchs starting at the word beginning
23-
/// 2 - other matches
24-
List<List<JMdict>> matches = [[], [], []];
31+
/// number of citeria used to sort the list
32+
int n = 3 * 3;
33+
34+
/// lists with n sub lists to sort search results
35+
List<List<JMdict>> matches = List.generate(n, (i) => <JMdict>[]);
2536
/// where in the meanings of this entry did the search match
26-
List<List<int>> matchIndices = [[], [], []];
37+
List<List<int>> matchIndices = List.generate(n, (i) => <int>[]);
2738
/// how many characters are the query and the matched result apart
28-
List<List<int>> lenDifferences = [[], [], []];
39+
List<List<int>> lenDifferences = List.generate(n, (i) => <int>[]);
2940

3041

3142
// if no wildcard is used, iterate over the entries and create a ranking for each
@@ -63,9 +74,10 @@ List<List<JMdict>> sortJmdictList(
6374
}
6475

6576
// sort the results
66-
matches[0] = sortEntries(matches[0], matchIndices[0], lenDifferences[0]);
67-
matches[1] = sortEntries(matches[1], matchIndices[1], lenDifferences[1]);
68-
matches[2] = sortEntries(matches[2], matchIndices[2], lenDifferences[2]);
77+
for (var i = 0; i < n; i++) {
78+
matches[i] = sortEntries(matches[i], matchIndices[i], lenDifferences[i]);
79+
}
80+
6981
}
7082
// if a wildcard was used just sort by frequency
7183
else {
@@ -106,16 +118,17 @@ Tuple3<int, int, int> rankMatches(List<List<String>> matches,
106118
// check for full match
107119
if(allSearches[i] == matches[matchIndeces[0]][matchIndeces[1]] &&
108120
(result == -1 || result > i)){
109-
result = 0;// + i*allSearches.length;
121+
result = 0 + i*allSearches.length;
110122
}
111123
// does the found dict entry start with the search term
112124
else if(matches[matchIndeces[0]][matchIndeces[1]].startsWith(allSearches[i]) &&
113125
(result == -1 || result > i)){
114-
result = 1;// + i;allSearches.length;
126+
result = 1 + i*allSearches.length;
115127
}
116128
// the query matches somwhere in the entry
117-
else if (result == -1 || result > i){
118-
result = 2;// + i*allSearches.length;
129+
else if (matches[matchIndeces[0]][matchIndeces[1]].contains(allSearches[i]) &&
130+
(result == -1 || result > i)){
131+
result = 2 + i*allSearches.length;
119132
}
120133
/// calculate the difference in length between the query and the result
121134
lenDiff = matches[matchIndeces[0]][matchIndeces[1]].length - allSearches[i].length;

0 commit comments

Comments
 (0)