1 package org.apache.lucene.search.vectorhighlight;
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 java.util.Arrays;
21 import java.util.HashSet;
24 public class SimpleBoundaryScanner implements BoundaryScanner {
26 public static final int DEFAULT_MAX_SCAN = 20;
27 public static final Character[] DEFAULT_BOUNDARY_CHARS = {'.', ',', '!', '?', ' ', '\t', '\n'};
29 protected int maxScan;
30 protected Set<Character> boundaryChars;
32 public SimpleBoundaryScanner(){
33 this( DEFAULT_MAX_SCAN, DEFAULT_BOUNDARY_CHARS );
36 public SimpleBoundaryScanner( int maxScan ){
37 this( maxScan, DEFAULT_BOUNDARY_CHARS );
40 public SimpleBoundaryScanner( Character[] boundaryChars ){
41 this( DEFAULT_MAX_SCAN, boundaryChars );
44 public SimpleBoundaryScanner( int maxScan, Character[] boundaryChars ){
45 this.maxScan = maxScan;
46 this.boundaryChars = new HashSet<Character>();
47 this.boundaryChars.addAll(Arrays.asList(boundaryChars));
50 public SimpleBoundaryScanner( int maxScan, Set<Character> boundaryChars ){
51 this.maxScan = maxScan;
52 this.boundaryChars = boundaryChars;
55 public int findStartOffset(StringBuilder buffer, int start) {
56 // avoid illegal start offset
57 if( start > buffer.length() || start < 1 ) return start;
58 int offset, count = maxScan;
59 for( offset = start; offset > 0 && count > 0; count-- ){
61 if( boundaryChars.contains( buffer.charAt( offset - 1 ) ) ) return offset;
68 public int findEndOffset(StringBuilder buffer, int start) {
69 // avoid illegal start offset
70 if( start > buffer.length() || start < 0 ) return start;
71 int offset, count = maxScan;
72 //for( offset = start; offset <= buffer.length() && count > 0; count-- ){
73 for( offset = start; offset < buffer.length() && count > 0; count-- ){
75 if( boundaryChars.contains( buffer.charAt( offset ) ) ) return offset;