KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > highlight > SimpleFragmenter


1 package org.apache.lucene.search.highlight;
2 /**
3  * Copyright 2002-2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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 import org.apache.lucene.analysis.Token;
19
20 /**
21  * {@link Fragmenter} implementation which breaks text up into same-size fragments with no concerns over spotting sentence boundaries.
22  * @author mark@searcharea.co.uk
23  */

24 public class SimpleFragmenter implements Fragmenter
25 {
26     private static final int DEFAULT_FRAGMENT_SIZE =100;
27     private int currentNumFrags;
28     private int fragmentSize;
29
30
31     public SimpleFragmenter()
32     {
33         this(DEFAULT_FRAGMENT_SIZE);
34     }
35
36
37     /**
38      *
39      * @param fragmentSize size in bytes of each fragment
40      */

41     public SimpleFragmenter(int fragmentSize)
42     {
43         this.fragmentSize=fragmentSize;
44     }
45
46     /* (non-Javadoc)
47      * @see org.apache.lucene.search.highlight.TextFragmenter#start(java.lang.String)
48      */

49     public void start(String JavaDoc originalText)
50     {
51         currentNumFrags=1;
52     }
53
54     /* (non-Javadoc)
55      * @see org.apache.lucene.search.highlight.TextFragmenter#isNewFragment(org.apache.lucene.analysis.Token)
56      */

57     public boolean isNewFragment(Token token)
58     {
59         boolean isNewFrag= token.endOffset()>=(fragmentSize*currentNumFrags);
60         if(isNewFrag)
61         {
62             currentNumFrags++;
63         }
64         return isNewFrag;
65     }
66
67     /**
68      * @return size in bytes of each fragment
69      * @uml.property name="fragmentSize"
70      */

71     public int getFragmentSize()
72     {
73         return fragmentSize;
74     }
75
76     /**
77      * @param size size in bytes of each fragment
78      * @uml.property name="fragmentSize"
79      */

80     public void setFragmentSize(int size)
81     {
82         fragmentSize = size;
83     }
84
85 }
86
Popular Tags