QueryResponses.cs, DumpIndex.cs, IQueryResult.cs, QueryExecutor.cs, QueryResult.cs...
[beagle.git] / beagled / Lucene.Net / Search / QueryTermVector.cs
blob5a005e67a3bba8a35a234705d91d85dfcb3935a4
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.
17 using System;
18 using Analyzer = Lucene.Net.Analysis.Analyzer;
19 using Token = Lucene.Net.Analysis.Token;
20 using TokenStream = Lucene.Net.Analysis.TokenStream;
21 using TermFreqVector = Lucene.Net.Index.TermFreqVector;
23 namespace Lucene.Net.Search
26 /// <summary>
27 ///
28 ///
29 /// </summary>
30 public class QueryTermVector : TermFreqVector
32 private System.String[] terms = new System.String[0];
33 private int[] termFreqs = new int[0];
35 public virtual System.String GetField()
37 return null;
40 /// <summary> </summary>
41 /// <param name="queryTerms">The original list of terms from the query, can contain duplicates
42 /// </param>
43 public QueryTermVector(System.String[] queryTerms)
46 ProcessTerms(queryTerms);
49 public QueryTermVector(System.String queryString, Analyzer analyzer)
51 if (analyzer != null)
53 TokenStream stream = analyzer.TokenStream("", new System.IO.StringReader(queryString));
54 if (stream != null)
56 Token next = null;
57 System.Collections.ArrayList terms = new System.Collections.ArrayList();
58 try
60 while ((next = stream.Next()) != null)
62 terms.Add(next.TermText());
64 ProcessTerms((System.String[]) terms.ToArray(typeof(System.String)));
66 catch (System.IO.IOException)
73 private void ProcessTerms(System.String[] queryTerms)
75 if (queryTerms != null)
77 System.Array.Sort(queryTerms);
78 System.Collections.IDictionary tmpSet = new System.Collections.Hashtable(queryTerms.Length);
79 //filter out duplicates
80 System.Collections.ArrayList tmpList = new System.Collections.ArrayList(queryTerms.Length);
81 System.Collections.ArrayList tmpFreqs = new System.Collections.ArrayList(queryTerms.Length);
82 int j = 0;
83 for (int i = 0; i < queryTerms.Length; i++)
85 System.String term = queryTerms[i];
86 System.Object tmpPosition = tmpSet[term];
87 if (tmpPosition == null)
89 tmpSet[term] = (System.Int32) j++;
90 tmpList.Add(term);
91 tmpFreqs.Add(1);
93 else
95 System.Int32 position = (System.Int32) tmpSet[term];
96 System.Int32 integer = (System.Int32) tmpFreqs[position];
97 tmpFreqs[position] = (System.Int32) (integer + 1);
100 terms = (System.String[]) tmpList.ToArray(typeof(System.String));
101 //termFreqs = (int[])tmpFreqs.toArray(termFreqs);
102 termFreqs = new int[tmpFreqs.Count];
103 int i2 = 0;
104 for (System.Collections.IEnumerator iter = tmpFreqs.GetEnumerator(); iter.MoveNext(); )
106 System.Int32 integer = (System.Int32) iter.Current;
107 termFreqs[i2++] = integer;
112 public override System.String ToString()
114 System.Text.StringBuilder sb = new System.Text.StringBuilder();
115 sb.Append('{');
116 for (int i = 0; i < terms.Length; i++)
118 if (i > 0)
119 sb.Append(", ");
120 sb.Append(terms[i]).Append('/').Append(termFreqs[i]);
122 sb.Append('}');
123 return sb.ToString();
127 public virtual int Size()
129 return terms.Length;
132 public virtual System.String[] GetTerms()
134 return terms;
137 public virtual int[] GetTermFrequencies()
139 return termFreqs;
142 public virtual int IndexOf(System.String term)
144 int res = System.Array.BinarySearch(terms, term);
145 return res >= 0?res:- 1;
148 public virtual int[] IndexesOf(System.String[] terms, int start, int len)
150 int[] res = new int[len];
152 for (int i = 0; i < len; i++)
154 res[i] = IndexOf(terms[i]);
156 return res;