Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

61
LINES

< > BotCompany Repo | #1022037 // ScoredSearcher (old). use ScoredSearcher_stable instead

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4950L/28K).

// A is the type of object being listed by the search
sclass ScoredSearcher<A> {
  int maxResults = 1000;
  LS preparedTerms;
  new Map<A, Double> scores;
  
  *() {}
  
  *(S query, O... _) {
    maxResults = optPar maxResults(_, maxResults);
    setQuery(query);
  }
  
  void setQuery(S query) {
    preparedTerms = scoredSearch_prepare(query);
  }
  
  void put(A object, S s) {
    putUnlessZero(scores, object, scoredSearch_score(s, preparedTerms));
  }
  
  void put(A object, Cl<S> fields) {
    add(object, scoreFields(fields));
  }
  
  // process fields with weights
  void putWithWeights(A object, Cl<Pair<S, Double>> fields) {
    scoredSearch_scoreWeighted2(fields, preparedTerms);
  }
  
  int scoreFields(Cl<S> fields) {
    ret scoredSearch_score(fields, preparedTerms);
  }
  
  int score(S text) { ret scoredSearch_score(text, preparedTerms); }
  
  void add(A object) {
    put(object, str(object));
  }
  
  void add(A object, Cl<S> fields) {
    put(object, fields);
  }
  
  void put(A object, double score) { add(object, score); }
  void add(A object, double score) {
    putUnlessZero(scores, object, score);
  }
  
  L<A> get() {
    ret takeFirst(maxResults, keysSortedByValuesDesc(scores));
  }
  
  L<Scored<A>> withScores() {
    ret map(get(), a -> scoredWithMapValue(scores, a));
  }
  
  A best() {
    ret keyWithHighestValue(scores);
  }
}

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: 373 / 972
Version history: 25 change(s)
Referenced in: #1032524 - ScoredSearcher_stable - keeps object order within same score
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)