Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » search » highlight » [javadoc | source]
    1   package org.apache.lucene.search.highlight;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import org.apache.lucene.analysis.TokenStream;
   21   import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
   22   
   23   /**
   24    * {@link Fragmenter} implementation which breaks text up into same-size
   25    * fragments with no concerns over spotting sentence boundaries.
   26    */
   27   public class SimpleFragmenter implements Fragmenter {
   28     private static final int DEFAULT_FRAGMENT_SIZE = 100;
   29     private int currentNumFrags;
   30     private int fragmentSize;
   31     private OffsetAttribute offsetAtt;
   32   
   33     public SimpleFragmenter() {
   34       this(DEFAULT_FRAGMENT_SIZE);
   35     }
   36   
   37     /**
   38      * 
   39      * @param fragmentSize size in number of characters of each fragment
   40      */
   41     public SimpleFragmenter(int fragmentSize) {
   42       this.fragmentSize = fragmentSize;
   43     }
   44   
   45   
   46     /* (non-Javadoc)
   47      * @see org.apache.lucene.search.highlight.Fragmenter#start(java.lang.String, org.apache.lucene.analysis.TokenStream)
   48      */
   49     public void start(String originalText, TokenStream stream) {
   50       offsetAtt = stream.addAttribute(OffsetAttribute.class);
   51       currentNumFrags = 1;
   52     }
   53   
   54   
   55     /* (non-Javadoc)
   56      * @see org.apache.lucene.search.highlight.Fragmenter#isNewFragment()
   57      */
   58     public boolean isNewFragment() {
   59       boolean isNewFrag = offsetAtt.endOffset() >= (fragmentSize * currentNumFrags);
   60       if (isNewFrag) {
   61         currentNumFrags++;
   62       }
   63       return isNewFrag;
   64     }
   65   
   66     /**
   67      * @return size in number of characters of each fragment
   68      */
   69     public int getFragmentSize() {
   70       return fragmentSize;
   71     }
   72   
   73     /**
   74      * @param size size in characters of each fragment
   75      */
   76     public void setFragmentSize(int size) {
   77       fragmentSize = size;
   78     }
   79   
   80   }

Save This Page
Home » lucene-3.0.1-src » org.apache » lucene » search » highlight » [javadoc | source]