pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / highlighter / src / java / org / apache / lucene / search / highlight / WeightedSpanTerm.java
1 package org.apache.lucene.search.highlight;
2
3
4 /**
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements.  See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License.  You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23
24
25 /**
26  * Lightweight class to hold term, weight, and positions used for scoring this
27  * term.
28  */
29 public class WeightedSpanTerm extends WeightedTerm{
30   boolean positionSensitive;
31   private List<PositionSpan> positionSpans = new ArrayList<PositionSpan>();
32
33   /**
34    * @param weight
35    * @param term
36    */
37   public WeightedSpanTerm(float weight, String term) {
38     super(weight, term);
39     this.positionSpans = new ArrayList<PositionSpan>();
40   }
41
42   /**
43    * @param weight
44    * @param term
45    * @param positionSensitive
46    */
47   public WeightedSpanTerm(float weight, String term, boolean positionSensitive) {
48     super(weight, term);
49     this.positionSensitive = positionSensitive;
50   }
51
52   /**
53    * Checks to see if this term is valid at <code>position</code>.
54    *
55    * @param position
56    *            to check against valid term positions
57    * @return true iff this term is a hit at this position
58    */
59   public boolean checkPosition(int position) {
60     // There would probably be a slight speed improvement if PositionSpans
61     // where kept in some sort of priority queue - that way this method
62     // could
63     // bail early without checking each PositionSpan.
64     Iterator<PositionSpan> positionSpanIt = positionSpans.iterator();
65
66     while (positionSpanIt.hasNext()) {
67       PositionSpan posSpan = positionSpanIt.next();
68
69       if (((position >= posSpan.start) && (position <= posSpan.end))) {
70         return true;
71       }
72     }
73
74     return false;
75   }
76
77   public void addPositionSpans(List<PositionSpan> positionSpans) {
78     this.positionSpans.addAll(positionSpans);
79   }
80
81   public boolean isPositionSensitive() {
82     return positionSensitive;
83   }
84
85   public void setPositionSensitive(boolean positionSensitive) {
86     this.positionSensitive = positionSensitive;
87   }
88
89   public List<PositionSpan> getPositionSpans() {
90     return positionSpans;
91   }
92 }
93
94
95 // Utility class to store a Span
96 class PositionSpan {
97   int start;
98   int end;
99
100   public PositionSpan(int start, int end) {
101     this.start = start;
102     this.end = end;
103   }
104 }