~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to beagled/Lucene.Net/Search/PhrasePrefixQuery.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2004 The Apache Software Foundation
3
 
 * 
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 * 
8
 
 * http://www.apache.org/licenses/LICENSE-2.0
9
 
 * 
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
using System;
18
 
using IndexReader = Lucene.Net.Index.IndexReader;
19
 
using MultipleTermPositions = Lucene.Net.Index.MultipleTermPositions;
20
 
using Term = Lucene.Net.Index.Term;
21
 
using TermPositions = Lucene.Net.Index.TermPositions;
22
 
using ToStringUtils = Lucene.Net.Util.ToStringUtils;
23
 
 
24
 
namespace Lucene.Net.Search
25
 
{
26
 
        
27
 
        /// <summary> PhrasePrefixQuery is a generalized version of PhraseQuery, with an added
28
 
        /// method {@link #Add(Term[])}.
29
 
        /// To use this class, to search for the phrase "Microsoft app*" first use
30
 
        /// add(Term) on the term "Microsoft", then find all terms that has "app" as
31
 
        /// prefix using IndexReader.terms(Term), and use PhrasePrefixQuery.add(Term[]
32
 
        /// terms) to add them to the query.
33
 
        /// 
34
 
        /// </summary>
35
 
        /// <deprecated> use {@link Lucene.Net.search.MultiPhraseQuery} instead
36
 
        /// </deprecated>
37
 
        /// <author>  Anders Nielsen
38
 
        /// </author>
39
 
        /// <version>  1.0
40
 
        /// </version>
41
 
        [Serializable]
42
 
        public class PhrasePrefixQuery : Query
43
 
        {
44
 
                private System.String field;
45
 
                private System.Collections.ArrayList termArrays = new System.Collections.ArrayList();
46
 
                private System.Collections.ArrayList positions = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10));
47
 
                
48
 
                private int slop = 0;
49
 
                
50
 
                /// <summary>Sets the phrase slop for this query.</summary>
51
 
                /// <seealso cref="PhraseQuery.SetSlop(int)">
52
 
                /// </seealso>
53
 
                public virtual void  SetSlop(int s)
54
 
                {
55
 
                        slop = s;
56
 
                }
57
 
                
58
 
                /// <summary>Sets the phrase slop for this query.</summary>
59
 
                /// <seealso cref="PhraseQuery.GetSlop()">
60
 
                /// </seealso>
61
 
                public virtual int GetSlop()
62
 
                {
63
 
                        return slop;
64
 
                }
65
 
                
66
 
                /// <summary>Add a single term at the next position in the phrase.</summary>
67
 
                /// <seealso cref="PhraseQuery.Add(Term)">
68
 
                /// </seealso>
69
 
                public virtual void  Add(Term term)
70
 
                {
71
 
                        Add(new Term[]{term});
72
 
                }
73
 
                
74
 
                /// <summary>Add multiple terms at the next position in the phrase.  Any of the terms
75
 
                /// may match.
76
 
                /// 
77
 
                /// </summary>
78
 
                /// <seealso cref="PhraseQuery.Add(Term)">
79
 
                /// </seealso>
80
 
                public virtual void  Add(Term[] terms)
81
 
                {
82
 
                        int position = 0;
83
 
                        if (positions.Count > 0)
84
 
                                position = ((System.Int32) positions[positions.Count - 1]) + 1;
85
 
                        
86
 
                        Add(terms, position);
87
 
                }
88
 
                
89
 
                /// <summary> Allows to specify the relative position of terms within the phrase.
90
 
                /// 
91
 
                /// </summary>
92
 
                /// <seealso cref="PhraseQuery.Add(Term, int)">
93
 
                /// </seealso>
94
 
                /// <param name="terms">
95
 
                /// </param>
96
 
                /// <param name="position">
97
 
                /// </param>
98
 
                public virtual void  Add(Term[] terms, int position)
99
 
                {
100
 
                        if (termArrays.Count == 0)
101
 
                                field = terms[0].Field();
102
 
                        
103
 
                        for (int i = 0; i < terms.Length; i++)
104
 
                        {
105
 
                                if (terms[i].Field() != field)
106
 
                                {
107
 
                                        throw new System.ArgumentException("All phrase terms must be in the same field (" + field + "): " + terms[i]);
108
 
                                }
109
 
                        }
110
 
                        
111
 
                        termArrays.Add(terms);
112
 
                        positions.Add((System.Int32) position);
113
 
                }
114
 
                
115
 
                /// <summary> Returns the relative positions of terms in this phrase.</summary>
116
 
                public virtual int[] GetPositions()
117
 
                {
118
 
                        int[] result = new int[positions.Count];
119
 
                        for (int i = 0; i < positions.Count; i++)
120
 
                                result[i] = ((System.Int32) positions[i]);
121
 
                        return result;
122
 
                }
123
 
                
124
 
                [Serializable]
125
 
                private class PhrasePrefixWeight : Weight
126
 
                {
127
 
                        private void  InitBlock(PhrasePrefixQuery enclosingInstance)
128
 
                        {
129
 
                                this.enclosingInstance = enclosingInstance;
130
 
                        }
131
 
                        private PhrasePrefixQuery enclosingInstance;
132
 
                        public PhrasePrefixQuery Enclosing_Instance
133
 
                        {
134
 
                                get
135
 
                                {
136
 
                                        return enclosingInstance;
137
 
                                }
138
 
                                
139
 
                        }
140
 
                        private Similarity similarity;
141
 
                        private float value_Renamed;
142
 
                        private float idf;
143
 
                        private float queryNorm;
144
 
                        private float queryWeight;
145
 
                        
146
 
                        public PhrasePrefixWeight(PhrasePrefixQuery enclosingInstance, Searcher searcher)
147
 
                        {
148
 
                                InitBlock(enclosingInstance);
149
 
                                this.similarity = Enclosing_Instance.GetSimilarity(searcher);
150
 
                                
151
 
                                // compute idf
152
 
                                System.Collections.IEnumerator i = Enclosing_Instance.termArrays.GetEnumerator();
153
 
                                while (i.MoveNext())
154
 
                                {
155
 
                                        Term[] terms = (Term[]) i.Current;
156
 
                                        for (int j = 0; j < terms.Length; j++)
157
 
                                        {
158
 
                                                idf += Enclosing_Instance.GetSimilarity(searcher).Idf(terms[j], searcher);
159
 
                                        }
160
 
                                }
161
 
                        }
162
 
                        
163
 
                        public virtual Query GetQuery()
164
 
                        {
165
 
                                return Enclosing_Instance;
166
 
                        }
167
 
                        public virtual float GetValue()
168
 
                        {
169
 
                                return value_Renamed;
170
 
                        }
171
 
                        
172
 
                        public virtual float SumOfSquaredWeights()
173
 
                        {
174
 
                                queryWeight = idf * Enclosing_Instance.GetBoost(); // compute query weight
175
 
                                return queryWeight * queryWeight; // square it
176
 
                        }
177
 
                        
178
 
                        public virtual void  Normalize(float queryNorm)
179
 
                        {
180
 
                                this.queryNorm = queryNorm;
181
 
                                queryWeight *= queryNorm; // normalize query weight
182
 
                                value_Renamed = queryWeight * idf; // idf for document 
183
 
                        }
184
 
                        
185
 
                        public virtual Scorer Scorer(IndexReader reader)
186
 
                        {
187
 
                                if (Enclosing_Instance.termArrays.Count == 0)
188
 
                                // optimize zero-term case
189
 
                                        return null;
190
 
                                
191
 
                                TermPositions[] tps = new TermPositions[Enclosing_Instance.termArrays.Count];
192
 
                                for (int i = 0; i < tps.Length; i++)
193
 
                                {
194
 
                                        Term[] terms = (Term[]) Enclosing_Instance.termArrays[i];
195
 
                                        
196
 
                                        TermPositions p;
197
 
                                        if (terms.Length > 1)
198
 
                                                p = new MultipleTermPositions(reader, terms);
199
 
                                        else
200
 
                                                p = reader.TermPositions(terms[0]);
201
 
                                        
202
 
                                        if (p == null)
203
 
                                                return null;
204
 
                                        
205
 
                                        tps[i] = p;
206
 
                                }
207
 
                                
208
 
                                if (Enclosing_Instance.slop == 0)
209
 
                                        return new ExactPhraseScorer(this, tps, Enclosing_Instance.GetPositions(), similarity, reader.Norms(Enclosing_Instance.field));
210
 
                                else
211
 
                                        return new SloppyPhraseScorer(this, tps, Enclosing_Instance.GetPositions(), similarity, Enclosing_Instance.slop, reader.Norms(Enclosing_Instance.field));
212
 
                        }
213
 
                        
214
 
                        public virtual Explanation Explain(IndexReader reader, int doc)
215
 
                        {
216
 
                                Explanation result = new Explanation();
217
 
                                result.SetDescription("weight(" + GetQuery() + " in " + doc + "), product of:");
218
 
                                
219
 
                                Explanation idfExpl = new Explanation(idf, "idf(" + GetQuery() + ")");
220
 
                                
221
 
                                // explain query weight
222
 
                                Explanation queryExpl = new Explanation();
223
 
                                queryExpl.SetDescription("queryWeight(" + GetQuery() + "), product of:");
224
 
                                
225
 
                                Explanation boostExpl = new Explanation(Enclosing_Instance.GetBoost(), "boost");
226
 
                                if (Enclosing_Instance.GetBoost() != 1.0f)
227
 
                                        queryExpl.AddDetail(boostExpl);
228
 
                                
229
 
                                queryExpl.AddDetail(idfExpl);
230
 
                                
231
 
                                Explanation queryNormExpl = new Explanation(queryNorm, "queryNorm");
232
 
                                queryExpl.AddDetail(queryNormExpl);
233
 
                                
234
 
                                queryExpl.SetValue(boostExpl.GetValue() * idfExpl.GetValue() * queryNormExpl.GetValue());
235
 
                                
236
 
                                result.AddDetail(queryExpl);
237
 
                                
238
 
                                // explain field weight
239
 
                                Explanation fieldExpl = new Explanation();
240
 
                                fieldExpl.SetDescription("fieldWeight(" + GetQuery() + " in " + doc + "), product of:");
241
 
                                
242
 
                                Explanation tfExpl = Scorer(reader).Explain(doc);
243
 
                                fieldExpl.AddDetail(tfExpl);
244
 
                                fieldExpl.AddDetail(idfExpl);
245
 
                                
246
 
                                Explanation fieldNormExpl = new Explanation();
247
 
                                byte[] fieldNorms = reader.Norms(Enclosing_Instance.field);
248
 
                                float fieldNorm = fieldNorms != null?Similarity.DecodeNorm(fieldNorms[doc]):0.0f;
249
 
                                fieldNormExpl.SetValue(fieldNorm);
250
 
                                fieldNormExpl.SetDescription("fieldNorm(field=" + Enclosing_Instance.field + ", doc=" + doc + ")");
251
 
                                fieldExpl.AddDetail(fieldNormExpl);
252
 
                                
253
 
                                fieldExpl.SetValue(tfExpl.GetValue() * idfExpl.GetValue() * fieldNormExpl.GetValue());
254
 
                                
255
 
                                result.AddDetail(fieldExpl);
256
 
                                
257
 
                                // combine them
258
 
                                result.SetValue(queryExpl.GetValue() * fieldExpl.GetValue());
259
 
                                
260
 
                                if (queryExpl.GetValue() == 1.0f)
261
 
                                        return fieldExpl;
262
 
                                
263
 
                                return result;
264
 
                        }
265
 
                }
266
 
                
267
 
                protected internal override Weight CreateWeight(Searcher searcher)
268
 
                {
269
 
                        if (termArrays.Count == 1)
270
 
                        {
271
 
                                // optimize one-term case
272
 
                                Term[] terms = (Term[]) termArrays[0];
273
 
                                BooleanQuery boq = new BooleanQuery(true);
274
 
                                for (int i = 0; i < terms.Length; i++)
275
 
                                {
276
 
                                        boq.Add(new TermQuery(terms[i]), BooleanClause.Occur.SHOULD);
277
 
                                }
278
 
                                boq.SetBoost(GetBoost());
279
 
                                return boq.CreateWeight(searcher);
280
 
                        }
281
 
                        return new PhrasePrefixWeight(this, searcher);
282
 
                }
283
 
                
284
 
                /// <summary>Prints a user-readable version of this query. </summary>
285
 
                public override System.String ToString(System.String f)
286
 
                {
287
 
                        System.Text.StringBuilder buffer = new System.Text.StringBuilder();
288
 
                        if (!field.Equals(f))
289
 
                        {
290
 
                                buffer.Append(field);
291
 
                                buffer.Append(":");
292
 
                        }
293
 
                        
294
 
                        buffer.Append("\"");
295
 
                        System.Collections.IEnumerator i = termArrays.GetEnumerator();
296
 
                        while (i.MoveNext())
297
 
                        {
298
 
                                Term[] terms = (Term[]) i.Current;
299
 
                                buffer.Append(terms[0].Text() + (terms.Length > 1?"*":""));
300
 
                                if (i.MoveNext())
301
 
                                        buffer.Append(" ");
302
 
                        }
303
 
                        buffer.Append("\"");
304
 
                        
305
 
                        if (slop != 0)
306
 
                        {
307
 
                                buffer.Append("~");
308
 
                                buffer.Append(slop);
309
 
                        }
310
 
                        
311
 
                        buffer.Append(ToStringUtils.Boost(GetBoost()));
312
 
                        
313
 
                        return buffer.ToString();
314
 
                }
315
 
        }
316
 
}
 
 
b'\\ No newline at end of file'