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.
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
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()
38 /// <summary> </summary>
39 /// <param name="queryTerms">The original list of terms from the query, can contain duplicates
41 public QueryTermVector(System
.String
[] queryTerms
)
44 ProcessTerms(queryTerms
);
47 public QueryTermVector(System
.String queryString
, Analyzer analyzer
)
51 TokenStream stream
= analyzer
.TokenStream("", new System
.IO
.StringReader(queryString
));
55 System
.Collections
.ArrayList terms
= new System
.Collections
.ArrayList();
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
);
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
++;
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
];
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();
114 for (int i
= 0; i
< terms
.Length
; i
++)
118 sb
.Append(terms
[i
]).Append('/').Append(termFreqs
[i
]);
121 return sb
.ToString();
125 public virtual int Size()
130 public virtual System
.String
[] GetTerms()
135 public virtual int[] GetTermFrequencies()
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
]);