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
SimpleFragmenter.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 Lucene.Net.Analysis;
20 using Lucene.Net.Analysis.Tokenattributes;
21 
22 namespace Lucene.Net.Search.Highlight
23 {
24 
25  /// <summary> <see cref="IFragmenter"/> implementation which breaks text up into same-size
26  /// fragments with no concerns over spotting sentence boundaries.
27  /// </summary>
28  /// <author> mark@searcharea.co.uk
29  /// </author>
31  {
32  private static int DEFAULT_FRAGMENT_SIZE = 100;
33  private int currentNumFrags;
34  private int fragmentSize;
35  private IOffsetAttribute offsetAtt;
36 
38  : this(DEFAULT_FRAGMENT_SIZE)
39  {
40  }
41 
42  /*
43  *
44  * @param fragmentSize size in number of characters of each fragment
45  */
46 
47  public SimpleFragmenter(int fragmentSize)
48  {
49  this.fragmentSize = fragmentSize;
50  }
51 
52 
53  /* (non-Javadoc)
54  * @see org.apache.lucene.search.highlight.Fragmenter#start(java.lang.String, org.apache.lucene.analysis.TokenStream)
55  */
56 
57  public void Start(String originalText, TokenStream stream)
58  {
59  offsetAtt = stream.AddAttribute<IOffsetAttribute>();
60  currentNumFrags = 1;
61  }
62 
63 
64  /* (non-Javadoc)
65  * @see org.apache.lucene.search.highlight.Fragmenter#isNewFragment()
66  */
67 
68  public bool IsNewFragment()
69  {
70  bool isNewFrag = offsetAtt.EndOffset >= (fragmentSize*currentNumFrags);
71  if (isNewFrag)
72  {
73  currentNumFrags++;
74  }
75  return isNewFrag;
76  }
77 
78  /// <summary>
79  /// Gets or sets the size in number of characters of each fragment
80  /// </summary>
81  public int FragmentSize
82  {
83  get { return fragmentSize; }
84  set { fragmentSize = value; }
85  }
86  }
87 }