pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / highlighter / src / java / org / apache / lucene / search / vectorhighlight / ScoreOrderFragmentsBuilder.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.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23
24 import org.apache.lucene.search.vectorhighlight.FieldFragList.WeightedFragInfo;
25
26 /**
27  * An implementation of FragmentsBuilder that outputs score-order fragments.
28  */
29 public class ScoreOrderFragmentsBuilder extends BaseFragmentsBuilder {
30
31   /**
32    * a constructor.
33    */
34   public ScoreOrderFragmentsBuilder(){
35     super();
36   }
37
38   /**
39    * a constructor.
40    * 
41    * @param preTags array of pre-tags for markup terms.
42    * @param postTags array of post-tags for markup terms.
43    */
44   public ScoreOrderFragmentsBuilder( String[] preTags, String[] postTags ){
45     super( preTags, postTags );
46   }
47
48   public ScoreOrderFragmentsBuilder( BoundaryScanner bs ){
49     super( bs );
50   }
51
52   public ScoreOrderFragmentsBuilder( String[] preTags, String[] postTags, BoundaryScanner bs ){
53     super( preTags, postTags, bs );
54   }
55
56   /**
57    * Sort by score the list of WeightedFragInfo
58    */
59   @Override
60   public List<WeightedFragInfo> getWeightedFragInfoList( List<WeightedFragInfo> src ) {
61     Collections.sort( src, new ScoreComparator() );
62     return src;
63   }
64
65   public static class ScoreComparator implements Comparator<WeightedFragInfo> {
66
67     public int compare( WeightedFragInfo o1, WeightedFragInfo o2 ) {
68       if( o1.totalBoost > o2.totalBoost ) return -1;
69       else if( o1.totalBoost < o2.totalBoost ) return 1;
70       // if same score then check startOffset
71       else{
72         if( o1.startOffset < o2.startOffset ) return -1;
73         else if( o1.startOffset > o2.startOffset ) return 1;
74       }
75       return 0;
76     }
77   }
78 }