diff --git a/lucene/contrib/CHANGES.txt b/lucene/contrib/CHANGES.txt index 5608c1c2ead..12ea75c612e 100644 --- a/lucene/contrib/CHANGES.txt +++ b/lucene/contrib/CHANGES.txt @@ -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 diff --git a/lucene/contrib/highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java b/lucene/contrib/highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java index 2c318d510c2..8a9447bc841 100644 --- a/lucene/contrib/highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java +++ b/lucene/contrib/highlighter/src/java/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilder.java @@ -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(); diff --git a/lucene/contrib/highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java b/lucene/contrib/highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java index 20d0949a0e9..5f30c6186e4 100644 --- a/lucene/contrib/highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java +++ b/lucene/contrib/highlighter/src/test/org/apache/lucene/search/vectorhighlight/SimpleFragListBuilderTest.java @@ -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() );