pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / highlighter / src / java / org / apache / lucene / search / vectorhighlight / SimpleBoundaryScanner.java
1 package org.apache.lucene.search.vectorhighlight;
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 java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 public class SimpleBoundaryScanner implements BoundaryScanner {
25   
26   public static final int DEFAULT_MAX_SCAN = 20;
27   public static final Character[] DEFAULT_BOUNDARY_CHARS = {'.', ',', '!', '?', ' ', '\t', '\n'};
28
29   protected int maxScan;
30   protected Set<Character> boundaryChars;
31   
32   public SimpleBoundaryScanner(){
33     this( DEFAULT_MAX_SCAN, DEFAULT_BOUNDARY_CHARS );
34   }
35   
36   public SimpleBoundaryScanner( int maxScan ){
37     this( maxScan, DEFAULT_BOUNDARY_CHARS );
38   }
39   
40   public SimpleBoundaryScanner( Character[] boundaryChars ){
41     this( DEFAULT_MAX_SCAN, boundaryChars );
42   }
43   
44   public SimpleBoundaryScanner( int maxScan, Character[] boundaryChars ){
45     this.maxScan = maxScan;
46     this.boundaryChars = new HashSet<Character>();
47     this.boundaryChars.addAll(Arrays.asList(boundaryChars));
48   }
49   
50   public SimpleBoundaryScanner( int maxScan, Set<Character> boundaryChars ){
51     this.maxScan = maxScan;
52     this.boundaryChars = boundaryChars;
53   }
54
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-- ){
60       // found?
61       if( boundaryChars.contains( buffer.charAt( offset - 1 ) ) ) return offset;
62       offset--;
63     }
64     // not found
65     return start;
66   }
67
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-- ){
74       // found?
75       if( boundaryChars.contains( buffer.charAt( offset ) ) ) return offset;
76       offset++;
77     }
78     // not found
79     return start;
80   }
81 }