Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
TermVectorEnumerator.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System.Text;
23 
24 namespace Lucene.Net.Index
25 {
26  /// <summary>
27  /// Class to allow for enumerating over the documents in the index to
28  /// retrieve the term vector for each one.
29  /// </summary>
30  public class TermVectorEnumerator : IEnumerator<ITermFreqVector>, IEnumerable<ITermFreqVector>
31  {
32  /// <summary>
33  /// Current document being accessed.
34  /// </summary>
35  private int document = -1;
36 
37  /// <summary>
38  /// The field name the vectors are being accessed from.
39  /// </summary>
40  private string fieldName;
41 
42  /// <summary>
43  /// The index reader that the vetors are retreived from.
44  /// </summary>
45  private IndexReader reader;
46 
47  /// <summary>
48  /// The return value should a document be deleted.
49  /// </summary>
50  private EmptyVector emptyVector;
51 
52  /// <summary>
53  /// Ctor.
54  /// </summary>
55  /// <param name="reader">The index reader used to read the vectors.</param>
56  /// <param name="field">The name of the field the vectors are read from.</param>
57  public TermVectorEnumerator(IndexReader reader, string field)
58  {
59  this.reader = reader;
60  this.fieldName = field;
61  this.emptyVector = new EmptyVector(field);
62  }
63 
64  #region IEnumerator<TermFreqVector> Members
65 
66  public ITermFreqVector Current
67  {
68  get { return this.CurrentVector(); }
69  }
70 
71  #endregion
72 
73  #region IDisposable Members
74 
75  public void Dispose()
76  {
77  // this is a noop as we do not want to close the reader
78  }
79 
80  #endregion
81 
82  #region IEnumerator Members
83 
84  object IEnumerator.Current
85  {
86  get { return this.CurrentVector(); }
87  }
88 
89  public bool MoveNext()
90  {
91  this.document++;
92  return this.document < this.reader.MaxDoc;
93  }
94 
95  public void Reset()
96  {
97  this.document = 0;
98  }
99 
100  #endregion
101 
102  #region IEnumerable<TermFreqVector> Members
103 
104  public IEnumerator<ITermFreqVector> GetEnumerator()
105  {
106  return (IEnumerator<ITermFreqVector>)this;
107  }
108 
109  #endregion
110 
111  #region IEnumerable Members
112 
113  IEnumerator IEnumerable.GetEnumerator()
114  {
115  return (IEnumerator<ITermFreqVector>)this;
116  }
117 
118  #endregion
119 
120  /// <summary>
121  /// Retrieve the current TermFreqVector from the index.
122  /// </summary>
123  /// <returns>The current TermFreqVector.</returns>
124  private ITermFreqVector CurrentVector()
125  {
126  if (this.reader.IsDeleted(this.document))
127  {
128  return this.emptyVector;
129  }
130  else
131  {
132  ITermFreqVector vector = this.reader.GetTermFreqVector(this.document, this.fieldName);
133  if (vector == null)
134  {
135  vector = this.emptyVector;
136  }
137  return vector;
138  }
139  }
140  }
141 
142  /// <summary>
143  /// A simple TermFreqVector implementation for an empty vector for use
144  /// with a deleted document or a document that does not have the field
145  /// that is being enumerated.
146  /// </summary>
148  {
149  private string field;
150 
151  private string[] emptyString = new string[0];
152 
153  private int[] emptyInt = new int[0];
154 
155  public EmptyVector(string field)
156  {
157  this.field = field;
158  }
159 
160  #region TermFreqVector Members
161 
162  public string Field
163  {
164  get { return this.field; }
165  }
166 
167  public int Size
168  {
169  get { return 0; }
170  }
171 
172  public string[] GetTerms()
173  {
174  return this.emptyString;
175  }
176 
177  public int[] GetTermFrequencies()
178  {
179  return this.emptyInt;
180  }
181 
182  public int IndexOf(string term)
183  {
184  return 0;
185  }
186 
187  public int[] IndexesOf(string[] terms, int start, int len)
188  {
189  return this.emptyInt;
190  }
191 
192  #endregion
193  }
194 }