2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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()
40 /// <summary> </summary>
41 /// <param name="queryTerms">The original list of terms from the query, can contain duplicates
43 public QueryTermVector(System
.String
[] queryTerms
)
46 ProcessTerms(queryTerms
);
49 public QueryTermVector(System
.String queryString
, Analyzer analyzer
)
53 TokenStream stream
= analyzer
.TokenStream("", new System
.IO
.StringReader(queryString
));
57 System
.Collections
.ArrayList terms
= new System
.Collections
.ArrayList();
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
);
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
++;
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
];
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();
116 for (int i
= 0; i
< terms
.Length
; i
++)
120 sb
.Append(terms
[i
]).Append('/').Append(termFreqs
[i
]);
123 return sb
.ToString();
127 public virtual int Size()
132 public virtual System
.String
[] GetTerms()
137 public virtual int[] GetTermFrequencies()
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
]);