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

76
LINES

< > BotCompany Repo | #1032524 // ScoredSearcher_stable - keeps object order within same score

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

Libraryless. Click here for Pure Java version (6119L/33K).

// A is the type of object being listed by the search
sclass ScoredSearcher_stable<A> {
  int maxResults = 1000;
  bool returnAll; // special mode to return all items
  LS preparedTerms;
  
  MultiMap<Double, A> byScore = descTreeMultiMap();
  
  *() {}
  
  *(S query, O... _) {
    maxResults = optPar maxResults(_, maxResults);
    setQuery(query);
  }
  
  void setQuery(S query) {
    preparedTerms = scoredSearch_prepare(query);
  }
  
  void put(A object, S s) {
    putScored(object, score(s));
  }
  
  void putScored(A object, double score) {
    if (score != 0)
      byScore.add(score, object);
  }
  
  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 returnAll ? 1
      : 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) {
    putScored(object, score);
  }
  
  L<A> get() {
    ret pairsB(takeFirst(maxResults, multiMapPairIterator(byScore)));
  }
  
  L<A> get_transformListWithinScore(IF1<L<A>> f) {
    ret pairsB(takeFirst(maxResults, multiMapPairIterator_transformValueList(byScore, f)));
  }
  
  L<Scored<A>> withScores() {
    ret map(takeFirst(maxResults, multiMapPairIterator(byScore)),
      p -> Scored(p.b, p.a));
  }
  
  A best() {
    ret firstValue(byScore);
  }
}

Author comment

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: 180 / 383
Version history: 4 change(s)
Referenced in: [show references]