1 package org.apache.lucene.search.highlight; 2 17 18 import java.util.HashMap ; 19 import java.util.HashSet ; 20 import org.apache.lucene.analysis.Token; 21 import org.apache.lucene.index.IndexReader; 22 import org.apache.lucene.search.Query; 23 24 28 public class QueryScorer implements Scorer 31 { 32 TextFragment currentTextFragment=null; 33 HashSet <String > uniqueTermsInFragment; 34 float totalScore=0; 35 float maxTermWeight=0; 36 private HashMap <String , WeightedTerm> termsToFind; 37 38 39 44 public QueryScorer(Query query) 45 { 46 this(QueryTermExtractor.getTerms(query)); 47 } 48 49 57 public QueryScorer(Query query, IndexReader reader, String fieldName) 58 { 59 this(QueryTermExtractor.getIdfWeightedTerms(query, reader, fieldName)); 60 } 61 62 public QueryScorer(WeightedTerm []weightedTerms ) 63 { 64 termsToFind = new HashMap <String , WeightedTerm>(); 65 for (int i = 0; i < weightedTerms.length; i++) 66 { 67 termsToFind.put(weightedTerms[i].term,weightedTerms[i]); 68 maxTermWeight=Math.max(maxTermWeight,weightedTerms[i].getWeight()); 69 } 70 } 71 72 73 76 public void startFragment(TextFragment newFragment) 77 { 78 uniqueTermsInFragment = new HashSet <String >(); 79 currentTextFragment=newFragment; 80 totalScore=0; 81 82 } 83 84 87 public float getTokenScore(Token token) 88 { 89 String termText=token.termText(); 90 91 WeightedTerm queryTerm=(WeightedTerm) termsToFind.get(termText); 92 if(queryTerm==null) 93 { 94 return 0; 96 } 97 if(!uniqueTermsInFragment.contains(termText)) 99 { 100 totalScore+=queryTerm.getWeight(); 101 uniqueTermsInFragment.add(termText); 102 } 103 return queryTerm.getWeight(); 104 } 105 106 107 110 public float getFragmentScore() 111 { 112 return totalScore; 113 } 114 115 116 119 public void allFragmentsProcessed() 120 { 121 } 123 124 128 public float getMaxTermWeight() 129 { 130 return maxTermWeight; 131 } 132 } 133 | Popular Tags |