LUCENE-3513: Add SimpleFragListBuilder constructor with margin parameter

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1184753 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Koji Sekiguchi 2011-10-16 03:45:34 +00:00
parent 332da4dff8
commit 1631ebfe5c
3 changed files with 27 additions and 9 deletions

View File

@ -130,6 +130,9 @@ API Changes
* LUCENE-3436: Add SuggestMode to the spellchecker, so you can specify the strategy
for suggesting related terms. (James Dyer via Robert Muir)
* LUCENE-3513: Add SimpleFragListBuilder constructor with margin parameter.
(Kelsey Francis via Koji Sekiguchi)
======================= Lucene 3.4.0 ================
New Features

View File

@ -28,13 +28,28 @@ import org.apache.lucene.search.vectorhighlight.FieldPhraseList.WeightedPhraseIn
*/
public class SimpleFragListBuilder implements FragListBuilder {
public static final int MARGIN = 6;
public static final int MIN_FRAG_CHAR_SIZE = MARGIN * 3;
public static final int MARGIN_DEFAULT = 6;
public static final int MIN_FRAG_CHAR_SIZE_FACTOR = 3;
final int margin;
final int minFragCharSize;
public SimpleFragListBuilder( int margin ){
if( margin < 0 )
throw new IllegalArgumentException( "margin(" + margin + ") is too small. It must be 0 or higher." );
this.margin = margin;
this.minFragCharSize = Math.max( 1, margin * MIN_FRAG_CHAR_SIZE_FACTOR );
}
public SimpleFragListBuilder(){
this( MARGIN_DEFAULT );
}
public FieldFragList createFieldFragList(FieldPhraseList fieldPhraseList, int fragCharSize) {
if( fragCharSize < MIN_FRAG_CHAR_SIZE )
if( fragCharSize < minFragCharSize )
throw new IllegalArgumentException( "fragCharSize(" + fragCharSize + ") is too small. It must be " +
MIN_FRAG_CHAR_SIZE + " or higher." );
minFragCharSize + " or higher." );
FieldFragList ffl = new FieldFragList( fragCharSize );
@ -56,8 +71,8 @@ public class SimpleFragListBuilder implements FragListBuilder {
wpil.clear();
wpil.add( phraseInfo );
int st = phraseInfo.getStartOffset() - MARGIN < startOffset ?
startOffset : phraseInfo.getStartOffset() - MARGIN;
int st = phraseInfo.getStartOffset() - margin < startOffset ?
startOffset : phraseInfo.getStartOffset() - margin;
int en = st + fragCharSize;
if( phraseInfo.getEndOffset() > en )
en = phraseInfo.getEndOffset();

View File

@ -31,7 +31,7 @@ public class SimpleFragListBuilderTest extends AbstractTestCase {
public void testTooSmallFragSize() throws Exception {
try{
SimpleFragListBuilder sflb = new SimpleFragListBuilder();
sflb.createFieldFragList( fpl(new TermQuery(new Term(F, "a")), "b c d" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE - 1 );
sflb.createFieldFragList( fpl(new TermQuery(new Term(F, "a")), "b c d" ), sflb.minFragCharSize - 1 );
fail( "IllegalArgumentException must be thrown" );
}
catch ( IllegalArgumentException expected ) {
@ -40,7 +40,7 @@ public class SimpleFragListBuilderTest extends AbstractTestCase {
public void testSmallerFragSizeThanTermQuery() throws Exception {
SimpleFragListBuilder sflb = new SimpleFragListBuilder();
FieldFragList ffl = sflb.createFieldFragList( fpl(new TermQuery(new Term(F, "abcdefghijklmnopqrs")), "abcdefghijklmnopqrs" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE );
FieldFragList ffl = sflb.createFieldFragList( fpl(new TermQuery(new Term(F, "abcdefghijklmnopqrs")), "abcdefghijklmnopqrs" ), sflb.minFragCharSize );
assertEquals( 1, ffl.getFragInfos().size() );
assertEquals( "subInfos=(abcdefghijklmnopqrs((0,19)))/1.0(0,19)", ffl.getFragInfos().get( 0 ).toString() );
}
@ -52,7 +52,7 @@ public class SimpleFragListBuilderTest extends AbstractTestCase {
phraseQuery.add(new Term(F, "abcdefgh"));
phraseQuery.add(new Term(F, "jklmnopqrs"));
FieldFragList ffl = sflb.createFieldFragList( fpl(phraseQuery, "abcdefgh jklmnopqrs" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE );
FieldFragList ffl = sflb.createFieldFragList( fpl(phraseQuery, "abcdefgh jklmnopqrs" ), sflb.minFragCharSize );
assertEquals( 1, ffl.getFragInfos().size() );
if (VERBOSE) System.out.println( ffl.getFragInfos().get( 0 ).toString() );
assertEquals( "subInfos=(abcdefghjklmnopqrs((0,21)))/1.0(0,21)", ffl.getFragInfos().get( 0 ).toString() );