Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
Explanation.cs
Go to the documentation of this file.
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 using System.Collections.Generic;
20 
21 namespace Lucene.Net.Search
22 {
23 
24  /// <summary>Expert: Describes the score computation for document and query. </summary>
25  [Serializable]
26  public class Explanation
27  {
28  private float value; // the value of this node
29  private System.String description; // what it represents
30  private List<Explanation> details; // sub-explanations
31 
32  public Explanation()
33  {
34  }
35 
36  public Explanation(float value, System.String description)
37  {
38  this.value = value;
39  this.description = description;
40  }
41 
42  /// <summary> Indicates whether or not this Explanation models a good match.
43  ///
44  /// <p/>
45  /// By default, an Explanation represents a "match" if the value is positive.
46  /// <p/>
47  /// </summary>
48  /// <seealso cref="Value">
49  /// </seealso>
50  public virtual bool IsMatch
51  {
52  get { return (0.0f < Value); }
53  }
54 
55 
56  /// <summary>The value assigned to this explanation node. </summary>
57  public virtual float Value
58  {
59  get { return value; }
60  set { this.value = value; }
61  }
62 
63  /// <summary>A description of this explanation node. </summary>
64  public virtual string Description
65  {
66  get { return description; }
67  set { this.description = value; }
68  }
69 
70  /// <summary> A short one line summary which should contain all high level
71  /// information about this Explanation, without the "Details"
72  /// </summary>
73  protected internal virtual string Summary
74  {
75  get { return Value + " = " + Description; }
76  }
77 
78  /// <summary>The sub-nodes of this explanation node. </summary>
79  public virtual Explanation[] GetDetails()
80  {
81  if (details == null)
82  return null;
83  return details.ToArray();
84  }
85 
86  /// <summary>Adds a sub-node to this explanation node. </summary>
87  public virtual void AddDetail(Explanation detail)
88  {
89  if (details == null)
90  details = new List<Explanation>();
91  details.Add(detail);
92  }
93 
94  /// <summary>Render an explanation as text. </summary>
95  public override System.String ToString()
96  {
97  return ToString(0);
98  }
99 
100  protected internal virtual System.String ToString(int depth)
101  {
102  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
103  for (int i = 0; i < depth; i++)
104  {
105  buffer.Append(" ");
106  }
107  buffer.Append(Summary);
108  buffer.Append("\n");
109 
110  Explanation[] details = GetDetails();
111  if (details != null)
112  {
113  for (int i = 0; i < details.Length; i++)
114  {
115  buffer.Append(details[i].ToString(depth + 1));
116  }
117  }
118 
119  return buffer.ToString();
120  }
121 
122  /// <summary>Render an explanation as HTML. </summary>
123  public virtual System.String ToHtml()
124  {
125  System.Text.StringBuilder buffer = new System.Text.StringBuilder();
126  buffer.Append("<list>\n");
127 
128  buffer.Append("<item>");
129  buffer.Append(Summary);
130  buffer.Append("<br />\n");
131 
132  Explanation[] details = GetDetails();
133  if (details != null)
134  {
135  for (int i = 0; i < details.Length; i++)
136  {
137  buffer.Append(details[i].ToHtml());
138  }
139  }
140 
141  buffer.Append("</item>\n");
142  buffer.Append("</list>\n");
143 
144  return buffer.ToString();
145  }
146 
147  /// <summary> Small Util class used to pass both an idf factor as well as an
148  /// explanation for that factor.
149  ///
150  /// This class will likely be held on a <see cref="Weight" />, so be aware
151  /// before storing any large or un-serializable fields.
152  ///
153  /// </summary>
154  [Serializable]
155  public abstract class IDFExplanation
156  {
157  /// <value> the idf factor </value>
158  public abstract float Idf { get; }
159 
160  /// <summary> This should be calculated lazily if possible.
161  ///
162  /// </summary>
163  /// <returns> the explanation for the idf factor.
164  /// </returns>
165  public abstract System.String Explain();
166  }
167  }
168 }