KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > spans > SpanWeight


1 package org.apache.lucene.search.spans;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Collection JavaDoc;
23
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.Term;
26
27 import org.apache.lucene.search.Query;
28 import org.apache.lucene.search.Weight;
29 import org.apache.lucene.search.Searcher;
30 import org.apache.lucene.search.Scorer;
31 import org.apache.lucene.search.Explanation;
32 import org.apache.lucene.search.Similarity;
33
34 class SpanWeight implements Weight {
35   private Similarity similarity;
36   private float value;
37   private float idf;
38   private float queryNorm;
39   private float queryWeight;
40
41   private Collection JavaDoc terms;
42   private SpanQuery query;
43
44   public SpanWeight(SpanQuery query, Searcher searcher)
45     throws IOException JavaDoc {
46     this.similarity = query.getSimilarity(searcher);
47     this.query = query;
48     this.terms = query.getTerms();
49
50     idf = this.query.getSimilarity(searcher).idf(terms, searcher);
51   }
52
53   public Query getQuery() { return query; }
54   public float getValue() { return value; }
55
56   public float sumOfSquaredWeights() throws IOException JavaDoc {
57     queryWeight = idf * query.getBoost(); // compute query weight
58
return queryWeight * queryWeight; // square it
59
}
60
61   public void normalize(float queryNorm) {
62     this.queryNorm = queryNorm;
63     queryWeight *= queryNorm; // normalize query weight
64
value = queryWeight * idf; // idf for document
65
}
66
67   public Scorer scorer(IndexReader reader) throws IOException JavaDoc {
68     return new SpanScorer(query.getSpans(reader), this,
69                           similarity,
70                           reader.norms(query.getField()));
71   }
72
73   public Explanation explain(IndexReader reader, int doc)
74     throws IOException JavaDoc {
75
76     Explanation result = new Explanation();
77     result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
78     String JavaDoc field = ((SpanQuery)getQuery()).getField();
79
80     StringBuffer JavaDoc docFreqs = new StringBuffer JavaDoc();
81     Iterator JavaDoc i = terms.iterator();
82     while (i.hasNext()) {
83       Term term = (Term)i.next();
84       docFreqs.append(term.text());
85       docFreqs.append("=");
86       docFreqs.append(reader.docFreq(term));
87
88       if (i.hasNext()) {
89         docFreqs.append(" ");
90       }
91     }
92
93     Explanation idfExpl =
94       new Explanation(idf, "idf(" + field + ": " + docFreqs + ")");
95
96     // explain query weight
97
Explanation queryExpl = new Explanation();
98     queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");
99
100     Explanation boostExpl = new Explanation(getQuery().getBoost(), "boost");
101     if (getQuery().getBoost() != 1.0f)
102       queryExpl.addDetail(boostExpl);
103     queryExpl.addDetail(idfExpl);
104
105     Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
106     queryExpl.addDetail(queryNormExpl);
107
108     queryExpl.setValue(boostExpl.getValue() *
109                        idfExpl.getValue() *
110                        queryNormExpl.getValue());
111
112     result.addDetail(queryExpl);
113
114     // explain field weight
115
Explanation fieldExpl = new Explanation();
116     fieldExpl.setDescription("fieldWeight("+field+":"+query.toString(field)+
117                              " in "+doc+"), product of:");
118
119     Explanation tfExpl = scorer(reader).explain(doc);
120     fieldExpl.addDetail(tfExpl);
121     fieldExpl.addDetail(idfExpl);
122
123     Explanation fieldNormExpl = new Explanation();
124     byte[] fieldNorms = reader.norms(field);
125     float fieldNorm =
126       fieldNorms!=null ? Similarity.decodeNorm(fieldNorms[doc]) : 0.0f;
127     fieldNormExpl.setValue(fieldNorm);
128     fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");
129     fieldExpl.addDetail(fieldNormExpl);
130
131     fieldExpl.setValue(tfExpl.getValue() *
132                        idfExpl.getValue() *
133                        fieldNormExpl.getValue());
134
135     result.addDetail(fieldExpl);
136
137     // combine them
138
result.setValue(queryExpl.getValue() * fieldExpl.getValue());
139
140     if (queryExpl.getValue() == 1.0f)
141       return fieldExpl;
142
143     return result;
144   }
145 }
146
Popular Tags