Libraryless. Click here for Pure Java version (6119L/33K).
1 | // A is the type of object being listed by the search |
2 | sclass ScoredSearcher_stable<A> { |
3 | int maxResults = 1000; |
4 | bool returnAll; // special mode to return all items |
5 | LS preparedTerms; |
6 | |
7 | MultiMap<Double, A> byScore = descTreeMultiMap(); |
8 | |
9 | *() {} |
10 | |
11 | *(S query, O... _) { |
12 | maxResults = optPar maxResults(_, maxResults); |
13 | setQuery(query); |
14 | } |
15 | |
16 | void setQuery(S query) { |
17 | preparedTerms = scoredSearch_prepare(query); |
18 | } |
19 | |
20 | void put(A object, S s) { |
21 | putScored(object, score(s)); |
22 | } |
23 | |
24 | void putScored(A object, double score) { |
25 | if (score != 0) |
26 | byScore.add(score, object); |
27 | } |
28 | |
29 | void put(A object, Cl<S> fields) { |
30 | add(object, scoreFields(fields)); |
31 | } |
32 | |
33 | // process fields with weights |
34 | void putWithWeights(A object, Cl<Pair<S, Double>> fields) { |
35 | scoredSearch_scoreWeighted2(fields, preparedTerms); |
36 | } |
37 | |
38 | int scoreFields(Cl<S> fields) { |
39 | ret scoredSearch_score(fields, preparedTerms); |
40 | } |
41 | |
42 | int score(S text) { |
43 | ret returnAll ? 1 |
44 | : scoredSearch_score(text, preparedTerms); |
45 | } |
46 | |
47 | void add(A object) { |
48 | put(object, str(object)); |
49 | } |
50 | |
51 | void add(A object, Cl<S> fields) { |
52 | put(object, fields); |
53 | } |
54 | |
55 | void put(A object, double score) { add(object, score); } |
56 | void add(A object, double score) { |
57 | putScored(object, score); |
58 | } |
59 | |
60 | L<A> get() { |
61 | ret pairsB(takeFirst(maxResults, multiMapPairIterator(byScore))); |
62 | } |
63 | |
64 | L<A> get_transformListWithinScore(IF1<L<A>> f) { |
65 | ret pairsB(takeFirst(maxResults, multiMapPairIterator_transformValueList(byScore, f))); |
66 | } |
67 | |
68 | L<Scored<A>> withScores() { |
69 | ret map(takeFirst(maxResults, multiMapPairIterator(byScore)), |
70 | p -> Scored(p.b, p.a)); |
71 | } |
72 | |
73 | A best() { |
74 | ret firstValue(byScore); |
75 | } |
76 | } |
Began life as a copy of #1022037
download show line numbers debug dex old transpilations
Travelled to 5 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx
No comments. add comment
Snippet ID: | #1032524 |
Snippet name: | ScoredSearcher_stable - keeps object order within same score |
Eternal ID of this version: | #1032524/5 |
Text MD5: | 2fa6a9bc5b978b695ab49fe696928738 |
Transpilation MD5: | f307c40f2745a410f36eff9ee549c418 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-01-29 06:02:07 |
Source code size: | 1805 bytes / 76 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 182 / 386 |
Version history: | 4 change(s) |
Referenced in: | [show references] |