Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Search / QueryTermVector.cs
blobd90a752dc9c8455677e442845c0ecac6bf6c63fc
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.
16 using System;
17 using Analyzer = Lucene.Net.Analysis.Analyzer;
18 using Token = Lucene.Net.Analysis.Token;
19 using TokenStream = Lucene.Net.Analysis.TokenStream;
20 using TermFreqVector = Lucene.Net.Index.TermFreqVector;
21 namespace Lucene.Net.Search
24 /// <summary>
25 ///
26 ///
27 /// </summary>
28 public class QueryTermVector : TermFreqVector
30 private System.String[] terms = new System.String[0];
31 private int[] termFreqs = new int[0];
33 public virtual System.String GetField()
35 return null;
38 /// <summary> </summary>
39 /// <param name="queryTerms">The original list of terms from the query, can contain duplicates
40 /// </param>
41 public QueryTermVector(System.String[] queryTerms)
44 ProcessTerms(queryTerms);
47 public QueryTermVector(System.String queryString, Analyzer analyzer)
49 if (analyzer != null)
51 TokenStream stream = analyzer.TokenStream("", new System.IO.StringReader(queryString));
52 if (stream != null)
54 Token next = null;
55 System.Collections.ArrayList terms = new System.Collections.ArrayList();
56 try
58 while ((next = stream.Next()) != null)
60 terms.Add(next.TermText());
62 ProcessTerms((System.String[]) terms.ToArray(typeof(System.String)));
64 catch (System.IO.IOException)
71 private void ProcessTerms(System.String[] queryTerms)
73 if (queryTerms != null)
75 System.Array.Sort(queryTerms);
76 System.Collections.IDictionary tmpSet = new System.Collections.Hashtable(queryTerms.Length);
77 //filter out duplicates
78 System.Collections.ArrayList tmpList = new System.Collections.ArrayList(queryTerms.Length);
79 System.Collections.ArrayList tmpFreqs = new System.Collections.ArrayList(queryTerms.Length);
80 int j = 0;
81 for (int i = 0; i < queryTerms.Length; i++)
83 System.String term = queryTerms[i];
84 System.Object tmpPosition = tmpSet[term];
85 if (tmpPosition == null)
87 tmpSet[term] = (System.Int32) j++;
88 tmpList.Add(term);
89 tmpFreqs.Add(1);
91 else
93 System.Int32 position = (System.Int32) tmpSet[term];
94 System.Int32 integer = (System.Int32) tmpFreqs[position];
95 tmpFreqs[position] = (System.Int32) (integer + 1);
98 terms = (System.String[]) tmpList.ToArray(typeof(System.String));
99 //termFreqs = (int[])tmpFreqs.toArray(termFreqs);
100 termFreqs = new int[tmpFreqs.Count];
101 int i2 = 0;
102 for (System.Collections.IEnumerator iter = tmpFreqs.GetEnumerator(); iter.MoveNext(); )
104 System.Int32 integer = (System.Int32) iter.Current;
105 termFreqs[i2++] = integer;
110 public override System.String ToString()
112 System.Text.StringBuilder sb = new System.Text.StringBuilder();
113 sb.Append('{');
114 for (int i = 0; i < terms.Length; i++)
116 if (i > 0)
117 sb.Append(", ");
118 sb.Append(terms[i]).Append('/').Append(termFreqs[i]);
120 sb.Append('}');
121 return sb.ToString();
125 public virtual int Size()
127 return terms.Length;
130 public virtual System.String[] GetTerms()
132 return terms;
135 public virtual int[] GetTermFrequencies()
137 return termFreqs;
140 public virtual int IndexOf(System.String term)
142 int res = System.Array.BinarySearch(terms, term);
143 return res >= 0?res:- 1;
146 public virtual int[] IndexesOf(System.String[] terms, int start, int len)
148 int[] res = new int[len];
150 for (int i = 0; i < len; i++)
152 res[i] = IndexOf(terms[i]);
154 return res;