@@ -5,27 +5,38 @@ import 'package:collection/collection.dart';
5
5
import 'package:database_builder/database_builder.dart' ;
6
6
import 'package:tuple/tuple.dart' ;
7
7
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
9
10
/// by those sorting criteria:
10
11
///
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:
14
25
/// 1. word frequency
15
26
List <List <JMdict >> sortJmdictList (
16
27
List <JMdict > entries, String query, String ? queryKana, String ? queryDeconjugated,
17
28
List <String > languages
18
29
){
19
30
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 > []) ;
25
36
/// 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 > []) ;
27
38
/// 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 > []) ;
29
40
30
41
31
42
// if no wildcard is used, iterate over the entries and create a ranking for each
@@ -63,9 +74,10 @@ List<List<JMdict>> sortJmdictList(
63
74
}
64
75
65
76
// 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
+
69
81
}
70
82
// if a wildcard was used just sort by frequency
71
83
else {
@@ -106,16 +118,17 @@ Tuple3<int, int, int> rankMatches(List<List<String>> matches,
106
118
// check for full match
107
119
if (allSearches[i] == matches[matchIndeces[0 ]][matchIndeces[1 ]] &&
108
120
(result == - 1 || result > i)){
109
- result = 0 ; // + i*allSearches.length;
121
+ result = 0 + i* allSearches.length;
110
122
}
111
123
// does the found dict entry start with the search term
112
124
else if (matches[matchIndeces[0 ]][matchIndeces[1 ]].startsWith (allSearches[i]) &&
113
125
(result == - 1 || result > i)){
114
- result = 1 ; // + i; allSearches.length;
126
+ result = 1 + i* allSearches.length;
115
127
}
116
128
// 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;
119
132
}
120
133
/// calculate the difference in length between the query and the result
121
134
lenDiff = matches[matchIndeces[0 ]][matchIndeces[1 ]].length - allSearches[i].length;
0 commit comments