pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / highlighter / src / java / org / apache / lucene / search / vectorhighlight / FieldFragList.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.ArrayList;
21 import java.util.List;
22
23 import org.apache.lucene.search.vectorhighlight.FieldPhraseList.WeightedPhraseInfo;
24 import org.apache.lucene.search.vectorhighlight.FieldPhraseList.WeightedPhraseInfo.Toffs;
25
26 /**
27  * FieldFragList has a list of "frag info" that is used by FragmentsBuilder class
28  * to create fragments (snippets).
29  */
30 public class FieldFragList {
31
32   private List<WeightedFragInfo> fragInfos = new ArrayList<WeightedFragInfo>();
33
34   /**
35    * a constructor.
36    * 
37    * @param fragCharSize the length (number of chars) of a fragment
38    */
39   public FieldFragList( int fragCharSize ){
40   }
41
42   /**
43    * convert the list of WeightedPhraseInfo to WeightedFragInfo, then add it to the fragInfos
44    * 
45    * @param startOffset start offset of the fragment
46    * @param endOffset end offset of the fragment
47    * @param phraseInfoList list of WeightedPhraseInfo objects
48    */
49   public void add( int startOffset, int endOffset, List<WeightedPhraseInfo> phraseInfoList ){
50     fragInfos.add( new WeightedFragInfo( startOffset, endOffset, phraseInfoList ) );
51   }
52   
53   /**
54    * return the list of WeightedFragInfos.
55    * 
56    * @return fragInfos.
57    */ 
58   public List<WeightedFragInfo> getFragInfos() {
59     return fragInfos;
60   }
61
62   public static class WeightedFragInfo {
63
64     List<SubInfo> subInfos;
65     float totalBoost;
66     int startOffset;
67     int endOffset;
68
69     public WeightedFragInfo( int startOffset, int endOffset, List<WeightedPhraseInfo> phraseInfoList ){
70       this.startOffset = startOffset;
71       this.endOffset = endOffset;
72       subInfos = new ArrayList<SubInfo>();
73       for( WeightedPhraseInfo phraseInfo : phraseInfoList ){
74         SubInfo subInfo = new SubInfo( phraseInfo.text, phraseInfo.termsOffsets, phraseInfo.seqnum );
75         subInfos.add( subInfo );
76         totalBoost += phraseInfo.boost;
77       }
78     }
79     
80     public List<SubInfo> getSubInfos(){
81       return subInfos;
82     }
83     
84     public float getTotalBoost(){
85       return totalBoost;
86     }
87     
88     public int getStartOffset(){
89       return startOffset;
90     }
91     
92     public int getEndOffset(){
93       return endOffset;
94     }
95     
96     @Override
97     public String toString(){
98       StringBuilder sb = new StringBuilder();
99       sb.append( "subInfos=(" );
100       for( SubInfo si : subInfos )
101         sb.append( si.toString() );
102       sb.append( ")/" ).append( totalBoost ).append( '(' ).append( startOffset ).append( ',' ).append( endOffset ).append( ')' );
103       return sb.toString();
104     }
105     
106     public static class SubInfo {
107       final String text;  // unnecessary member, just exists for debugging purpose
108       final List<Toffs> termsOffsets;   // usually termsOffsets.size() == 1,
109                               // but if position-gap > 1 and slop > 0 then size() could be greater than 1
110       int seqnum;
111
112       SubInfo( String text, List<Toffs> termsOffsets, int seqnum ){
113         this.text = text;
114         this.termsOffsets = termsOffsets;
115         this.seqnum = seqnum;
116       }
117       
118       public List<Toffs> getTermsOffsets(){
119         return termsOffsets;
120       }
121       
122       public int getSeqnum(){
123         return seqnum;
124       }
125       
126       @Override
127       public String toString(){
128         StringBuilder sb = new StringBuilder();
129         sb.append( text ).append( '(' );
130         for( Toffs to : termsOffsets )
131           sb.append( to.toString() );
132         sb.append( ')' );
133         return sb.toString();
134       }
135     }
136   }
137 }