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
Analysis.Ext.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.Text;
22 using System.IO;
23 
24 using Lucene.Net.Analysis;
25 using Lucene.Net.Analysis.Tokenattributes;
26 using Lucene.Net.Util;
27 
28 
29 namespace Lucene.Net.Analysis.Ext
30 {
46  {
49  public override TokenStream TokenStream(string fieldName, TextReader reader)
50  {
51  TokenStream t = null;
52  t = new LetterOrDigitTokenizer(reader);
53  t = new LowerCaseFilter(t);
54  t = new ASCIIFoldingFilter(t);
55  t = new SingleCharTokenizer(t);
56 
57  return t;
58  }
59 
60  class SingleCharTokenizer : Tokenizer
61  {
62  TokenStream _input = null;
63 
64  ITermAttribute _termAttribute = null;
65  IOffsetAttribute _offsetAttribute = null;
66  IPositionIncrementAttribute _positionIncrementAttribute = null;
67 
68  char[] _buffer = null;
69  int _offset = -1;
70  int _length = -1;
71  int _offsetInStream = -1;
72 
73  public SingleCharTokenizer(TokenStream input): base(input)
74  {
75  _input = input;
76  _termAttribute = AddAttribute<ITermAttribute>();
77  _offsetAttribute = AddAttribute<IOffsetAttribute>();
78  _positionIncrementAttribute = AddAttribute<IPositionIncrementAttribute>();
79  }
80 
81  public override bool IncrementToken()
82  {
83  int positionIncrement = 0;
84  if (_buffer == null || _offset >= _length)
85  {
86  if (!_input.IncrementToken()) return false;
87 
88  _offset = 0;
89  _buffer = _termAttribute.TermBuffer();
90  _length = _termAttribute.TermLength();
91  positionIncrement++;
92  _offsetInStream++;
93  }
94 
95  _offsetAttribute.SetOffset(_offsetInStream, _offsetInStream + 1);
96  _offsetInStream++;
97 
98  positionIncrement++;
99  _positionIncrementAttribute.PositionIncrement = positionIncrement;
100 
101  _termAttribute.SetTermLength(1);
102  _termAttribute.SetTermBuffer(_buffer[_offset++].ToString());
103 
104  return true;
105  }
106 
107  public override void Reset()
108  {
109  _buffer = null;
110  _offset = -1;
111  _length = -1;
112  _offsetInStream = -1;
113 
114  base.Reset();
115  }
116 
117  protected override void Dispose(bool disposing)
118  {
119  _input.Close();
120  base.Dispose(disposing);
121  }
122  }
123  }
124 
134  {
137  public override TokenStream TokenStream(string fieldName, TextReader reader)
138  {
139  TokenStream t = null;
140  t = new LetterOrDigitTokenizer(reader);
141  t = new LowerCaseFilter(t);
142  t = new ASCIIFoldingFilter(t);
143 
144  return t;
145  }
146  }
147 
152  {
155  public LetterOrDigitTokenizer(TextReader reader): base(reader)
156  {
157  }
158 
161  protected override bool IsTokenChar(char c)
162  {
163  return char.IsLetterOrDigit(c);
164  }
165  }
166 }