1 package org.apache.lucene.search.highlight;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import org.apache.lucene.analysis.TokenStream;
21 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
24 * {@link Fragmenter} implementation which breaks text up into same-size
25 * fragments with no concerns over spotting sentence boundaries.
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;
33 public SimpleFragmenter() {
34 this(DEFAULT_FRAGMENT_SIZE);
39 * @param fragmentSize size in number of characters of each fragment
41 public SimpleFragmenter(int fragmentSize) {
42 this.fragmentSize = fragmentSize;
47 * @see org.apache.lucene.search.highlight.Fragmenter#start(java.lang.String, org.apache.lucene.analysis.TokenStream)
49 public void start(String originalText, TokenStream stream) {
50 offsetAtt = stream.addAttribute(OffsetAttribute.class);
56 * @see org.apache.lucene.search.highlight.Fragmenter#isNewFragment()
58 public boolean isNewFragment() {
59 boolean isNewFrag = offsetAtt.endOffset() >= (fragmentSize * currentNumFrags);
67 * @return size in number of characters of each fragment
69 public int getFragmentSize() {
74 * @param size size in characters of each fragment
76 public void setFragmentSize(int size) {