Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
LuceneDictionary.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.Collections;
19 using Lucene.Net.Documents;
20 
21 namespace SpellChecker.Net.Search.Spell
22 {
23  using System;
24  using IndexReader = Lucene.Net.Index.IndexReader;
25  using TermEnum = Lucene.Net.Index.TermEnum;
26  using Term = Lucene.Net.Index.Term;
27 
31  public class LuceneDictionary : IDictionary, System.Collections.Generic.IEnumerable<string>
32  {
33  internal IndexReader reader;
34  internal System.String field;
35 
36  public LuceneDictionary(IndexReader reader, System.String field)
37  {
38  this.reader = reader;
39  this.field = field;
40  }
41 
42  virtual public System.Collections.Generic.IEnumerator<string> GetWordsIterator()
43  {
44  return new LuceneIterator(this);
45  }
46 
47  public System.Collections.Generic.IEnumerator<string> GetEnumerator()
48  {
49  return GetWordsIterator();
50  }
51 
52  IEnumerator IEnumerable.GetEnumerator()
53  {
54  return GetEnumerator();
55  }
56 
57  internal sealed class LuceneIterator : System.Collections.Generic.IEnumerator<string>
58  {
59  private readonly TermEnum termEnum;
60  private Term actualTerm;
61  private bool hasNextCalled;
62 
63  private readonly LuceneDictionary enclosingInstance;
64 
65  public LuceneIterator(LuceneDictionary enclosingInstance)
66  {
67  this.enclosingInstance = enclosingInstance;
68  try
69  {
70  termEnum = enclosingInstance.reader.Terms(new Term(enclosingInstance.field, ""));
71  }
72  catch (System.IO.IOException ex)
73  {
74  System.Console.Error.WriteLine(ex.StackTrace);
75  }
76  }
77 
78  public string Current
79  {
80  get
81  {
82  if (!hasNextCalled)
83  {
84  MoveNext();
85  }
86  hasNextCalled = false;
87  return (actualTerm != null) ? actualTerm.Text : null;
88  }
89 
90  }
91 
92  object IEnumerator.Current
93  {
94  get { return Current; }
95  }
96 
97  public bool MoveNext()
98  {
99  hasNextCalled = true;
100 
101  actualTerm = termEnum.Term;
102 
103  // if there are no words return false
104  if (actualTerm == null) return false;
105 
106  System.String fieldt = actualTerm.Field;
107  termEnum.Next();
108 
109  // if the next word doesn't have the same field return false
110  if (fieldt != enclosingInstance.field)
111  {
112  actualTerm = null;
113  return false;
114  }
115  return true;
116  }
117 
118  public void Remove()
119  {
120 
121  }
122 
123  public void Reset()
124  {
125 
126  }
127 
128  public void Dispose()
129  {
130  // Nothing
131  }
132  }
133  }
134 }