Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Search / Spans / SpanWeight.cs
1 /* 
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  * http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 using System;
19
20 using IndexReader = Mono.Lucene.Net.Index.IndexReader;
21 using Mono.Lucene.Net.Search;
22 using IDFExplanation = Mono.Lucene.Net.Search.Explanation.IDFExplanation;
23
24 namespace Mono.Lucene.Net.Search.Spans
25 {
26         
27         /// <summary> Expert-only.  Public for use by other weight implementations</summary>
28         [Serializable]
29         public class SpanWeight:Weight
30         {
31                 protected internal Similarity similarity;
32                 protected internal float value_Renamed;
33                 protected internal float idf;
34                 protected internal float queryNorm;
35                 protected internal float queryWeight;
36                 
37                 protected internal System.Collections.Hashtable terms;
38                 protected internal SpanQuery query;
39                 private IDFExplanation idfExp;
40                 
41                 public SpanWeight(SpanQuery query, Searcher searcher)
42                 {
43                         this.similarity = query.GetSimilarity(searcher);
44                         this.query = query;
45                         terms = new System.Collections.Hashtable();
46                         query.ExtractTerms(terms);
47                         idfExp = similarity.idfExplain(new System.Collections.ArrayList(terms.Values), searcher);
48                         idf = idfExp.GetIdf();
49                 }
50                 
51                 public override Query GetQuery()
52                 {
53                         return query;
54                 }
55                 public override float GetValue()
56                 {
57                         return value_Renamed;
58                 }
59                 
60                 public override float SumOfSquaredWeights()
61                 {
62                         queryWeight = idf * query.GetBoost(); // compute query weight
63                         return queryWeight * queryWeight; // square it
64                 }
65                 
66                 public override void  Normalize(float queryNorm)
67                 {
68                         this.queryNorm = queryNorm;
69                         queryWeight *= queryNorm; // normalize query weight
70                         value_Renamed = queryWeight * idf; // idf for document
71                 }
72                 
73                 public override Scorer Scorer(IndexReader reader, bool scoreDocsInOrder, bool topScorer)
74                 {
75                         return new SpanScorer(query.GetSpans(reader), this, similarity, reader.Norms(query.GetField()));
76                 }
77                 
78                 public override Explanation Explain(IndexReader reader, int doc)
79                 {
80                         
81                         ComplexExplanation result = new ComplexExplanation();
82                         result.SetDescription("weight(" + GetQuery() + " in " + doc + "), product of:");
83                         System.String field = ((SpanQuery) GetQuery()).GetField();
84                         
85                         Explanation idfExpl = new Explanation(idf, "idf(" + field + ": " + idfExp.Explain() + ")");
86                         
87                         // explain query weight
88                         Explanation queryExpl = new Explanation();
89                         queryExpl.SetDescription("queryWeight(" + GetQuery() + "), product of:");
90                         
91                         Explanation boostExpl = new Explanation(GetQuery().GetBoost(), "boost");
92                         if (GetQuery().GetBoost() != 1.0f)
93                                 queryExpl.AddDetail(boostExpl);
94                         queryExpl.AddDetail(idfExpl);
95                         
96                         Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
97                         queryExpl.AddDetail(queryNormExpl);
98                         
99                         queryExpl.SetValue(boostExpl.GetValue() * idfExpl.GetValue() * queryNormExpl.GetValue());
100                         
101                         result.AddDetail(queryExpl);
102                         
103                         // explain field weight
104                         ComplexExplanation fieldExpl = new ComplexExplanation();
105                         fieldExpl.SetDescription("fieldWeight(" + field + ":" + query.ToString(field) + " in " + doc + "), product of:");
106                         
107                         Explanation tfExpl = Scorer(reader, true, false).Explain(doc);
108                         fieldExpl.AddDetail(tfExpl);
109                         fieldExpl.AddDetail(idfExpl);
110                         
111                         Explanation fieldNormExpl = new Explanation();
112                         byte[] fieldNorms = reader.Norms(field);
113                         float fieldNorm = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]):1.0f;
114                         fieldNormExpl.SetValue(fieldNorm);
115                         fieldNormExpl.SetDescription("fieldNorm(field=" + field + ", doc=" + doc + ")");
116                         fieldExpl.AddDetail(fieldNormExpl);
117                         
118                         fieldExpl.SetMatch(tfExpl.IsMatch());
119                         fieldExpl.SetValue(tfExpl.GetValue() * idfExpl.GetValue() * fieldNormExpl.GetValue());
120                         
121                         result.AddDetail(fieldExpl);
122                         System.Boolean? tempAux = fieldExpl.GetMatch();
123                         result.SetMatch(tempAux);
124                         
125                         // combine them
126                         result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());
127                         
128                         if (queryExpl.GetValue() == 1.0f)
129                                 return fieldExpl;
130                         
131                         return result;
132                 }
133         }
134 }