Libraryless. Click here for Pure Java version (4950L/28K).
1 | // A is the type of object being listed by the search |
2 | sclass ScoredSearcher<A> { |
3 | int maxResults = 1000; |
4 | LS preparedTerms; |
5 | new Map<A, Double> scores; |
6 | |
7 | *() {} |
8 | |
9 | *(S query, O... _) { |
10 | maxResults = optPar maxResults(_, maxResults); |
11 | setQuery(query); |
12 | } |
13 | |
14 | void setQuery(S query) { |
15 | preparedTerms = scoredSearch_prepare(query); |
16 | } |
17 | |
18 | void put(A object, S s) { |
19 | putUnlessZero(scores, object, scoredSearch_score(s, preparedTerms)); |
20 | } |
21 | |
22 | void put(A object, Cl<S> fields) { |
23 | add(object, scoreFields(fields)); |
24 | } |
25 | |
26 | // process fields with weights |
27 | void putWithWeights(A object, Cl<Pair<S, Double>> fields) { |
28 | scoredSearch_scoreWeighted2(fields, preparedTerms); |
29 | } |
30 | |
31 | int scoreFields(Cl<S> fields) { |
32 | ret scoredSearch_score(fields, preparedTerms); |
33 | } |
34 | |
35 | int score(S text) { ret scoredSearch_score(text, preparedTerms); } |
36 | |
37 | void add(A object) { |
38 | put(object, str(object)); |
39 | } |
40 | |
41 | void add(A object, Cl<S> fields) { |
42 | put(object, fields); |
43 | } |
44 | |
45 | void put(A object, double score) { add(object, score); } |
46 | void add(A object, double score) { |
47 | putUnlessZero(scores, object, score); |
48 | } |
49 | |
50 | L<A> get() { |
51 | ret takeFirst(maxResults, keysSortedByValuesDesc(scores)); |
52 | } |
53 | |
54 | L<Scored<A>> withScores() { |
55 | ret map(get(), a -> scoredWithMapValue(scores, a)); |
56 | } |
57 | |
58 | A best() { |
59 | ret keyWithHighestValue(scores); |
60 | } |
61 | } |
download show line numbers debug dex old transpilations
Travelled to 10 computer(s): bhatertpkbcr, cfunsshuasjs, ekrmjmnbrukm, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1022037 |
Snippet name: | ScoredSearcher (old). use ScoredSearcher_stable instead |
Eternal ID of this version: | #1022037/26 |
Text MD5: | d51bc0c472326f5a9b0da75f84cf1a7b |
Transpilation MD5: | 20755f203d9e5c97b28ebc71e294fc18 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-06 15:24:29 |
Source code size: | 1434 bytes / 61 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 372 / 972 |
Version history: | 25 change(s) |
Referenced in: | [show references] |