add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / highlighter / src / test / org / apache / lucene / search / vectorhighlight / SimpleFragListBuilderTest.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 org.apache.lucene.search.Query;
21
22 public class SimpleFragListBuilderTest extends AbstractTestCase {
23   
24   public void testNullFieldFragList() throws Exception {
25     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
26     FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "b c d" ), 100 );
27     assertEquals( 0, ffl.getFragInfos().size() );
28   }
29   
30   public void testTooSmallFragSize() throws Exception {
31     try{
32       SimpleFragListBuilder sflb = new SimpleFragListBuilder();
33       sflb.createFieldFragList( fpl( "a", "b c d" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE - 1 );
34       fail( "IllegalArgumentException must be thrown" );
35     }
36     catch ( IllegalArgumentException expected ) {
37     }
38   }
39   
40   public void testSmallerFragSizeThanTermQuery() throws Exception {
41     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
42     FieldFragList ffl = sflb.createFieldFragList( fpl( "abcdefghijklmnopqrs", "abcdefghijklmnopqrs" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE );
43     assertEquals( 1, ffl.getFragInfos().size() );
44     assertEquals( "subInfos=(abcdefghijklmnopqrs((0,19)))/1.0(0,19)", ffl.getFragInfos().get( 0 ).toString() );
45   }
46   
47   public void testSmallerFragSizeThanPhraseQuery() throws Exception {
48     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
49     FieldFragList ffl = sflb.createFieldFragList( fpl( "\"abcdefgh jklmnopqrs\"", "abcdefgh   jklmnopqrs" ), SimpleFragListBuilder.MIN_FRAG_CHAR_SIZE );
50     assertEquals( 1, ffl.getFragInfos().size() );
51     if (VERBOSE) System.out.println( ffl.getFragInfos().get( 0 ).toString() );
52     assertEquals( "subInfos=(abcdefghjklmnopqrs((0,21)))/1.0(0,21)", ffl.getFragInfos().get( 0 ).toString() );
53   }
54   
55   public void test1TermIndex() throws Exception {
56     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
57     FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "a" ), 100 );
58     assertEquals( 1, ffl.getFragInfos().size() );
59     assertEquals( "subInfos=(a((0,1)))/1.0(0,100)", ffl.getFragInfos().get( 0 ).toString() );
60   }
61   
62   public void test2TermsIndex1Frag() throws Exception {
63     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
64     FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "a a" ), 100 );
65     assertEquals( 1, ffl.getFragInfos().size() );
66     assertEquals( "subInfos=(a((0,1))a((2,3)))/2.0(0,100)", ffl.getFragInfos().get( 0 ).toString() );
67   
68     ffl = sflb.createFieldFragList( fpl( "a", "a b b b b b b b b a" ), 20 );
69     assertEquals( 1, ffl.getFragInfos().size() );
70     assertEquals( "subInfos=(a((0,1))a((18,19)))/2.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
71
72     ffl = sflb.createFieldFragList( fpl( "a", "b b b b a b b b b a" ), 20 );
73     assertEquals( 1, ffl.getFragInfos().size() );
74     assertEquals( "subInfos=(a((8,9))a((18,19)))/2.0(2,22)", ffl.getFragInfos().get( 0 ).toString() );
75   }
76   
77   public void test2TermsIndex2Frags() throws Exception {
78     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
79     FieldFragList ffl = sflb.createFieldFragList( fpl( "a", "a b b b b b b b b b b b b b a" ), 20 );
80     assertEquals( 2, ffl.getFragInfos().size() );
81     assertEquals( "subInfos=(a((0,1)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
82     assertEquals( "subInfos=(a((28,29)))/1.0(22,42)", ffl.getFragInfos().get( 1 ).toString() );
83
84     ffl = sflb.createFieldFragList( fpl( "a", "a b b b b b b b b b b b b a" ), 20 );
85     assertEquals( 2, ffl.getFragInfos().size() );
86     assertEquals( "subInfos=(a((0,1)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
87     assertEquals( "subInfos=(a((26,27)))/1.0(20,40)", ffl.getFragInfos().get( 1 ).toString() );
88
89     ffl = sflb.createFieldFragList( fpl( "a", "a b b b b b b b b b a" ), 20 );
90     assertEquals( 2, ffl.getFragInfos().size() );
91     assertEquals( "subInfos=(a((0,1)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
92     assertEquals( "subInfos=(a((20,21)))/1.0(20,40)", ffl.getFragInfos().get( 1 ).toString() );
93   }
94   
95   public void test2TermsQuery() throws Exception {
96     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
97     FieldFragList ffl = sflb.createFieldFragList( fpl( "a b", "c d e" ), 20 );
98     assertEquals( 0, ffl.getFragInfos().size() );
99
100     ffl = sflb.createFieldFragList( fpl( "a b", "d b c" ), 20 );
101     assertEquals( 1, ffl.getFragInfos().size() );
102     assertEquals( "subInfos=(b((2,3)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
103
104     ffl = sflb.createFieldFragList( fpl( "a b", "a b c" ), 20 );
105     assertEquals( 1, ffl.getFragInfos().size() );
106     assertEquals( "subInfos=(a((0,1))b((2,3)))/2.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
107   }
108   
109   public void testPhraseQuery() throws Exception {
110     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
111     FieldFragList ffl = sflb.createFieldFragList( fpl( "\"a b\"", "c d e" ), 20 );
112     assertEquals( 0, ffl.getFragInfos().size() );
113
114     ffl = sflb.createFieldFragList( fpl( "\"a b\"", "a c b" ), 20 );
115     assertEquals( 0, ffl.getFragInfos().size() );
116
117     ffl = sflb.createFieldFragList( fpl( "\"a b\"", "a b c" ), 20 );
118     assertEquals( 1, ffl.getFragInfos().size() );
119     assertEquals( "subInfos=(ab((0,3)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
120   }
121   
122   public void testPhraseQuerySlop() throws Exception {
123     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
124     FieldFragList ffl = sflb.createFieldFragList( fpl( "\"a b\"~1", "a c b" ), 20 );
125     assertEquals( 1, ffl.getFragInfos().size() );
126     assertEquals( "subInfos=(ab((0,1)(4,5)))/1.0(0,20)", ffl.getFragInfos().get( 0 ).toString() );
127   }
128
129   private FieldPhraseList fpl( String queryValue, String indexValue ) throws Exception {
130     make1d1fIndex( indexValue );
131     Query query = paW.parse( queryValue );
132     FieldQuery fq = new FieldQuery( query, true, true );
133     FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
134     return new FieldPhraseList( stack, fq );
135   }
136   
137   public void test1PhraseShortMV() throws Exception {
138     makeIndexShortMV();
139
140     FieldQuery fq = new FieldQuery( tq( "d" ), true, true );
141     FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
142     FieldPhraseList fpl = new FieldPhraseList( stack, fq );
143     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
144     FieldFragList ffl = sflb.createFieldFragList( fpl, 100 );
145     assertEquals( 1, ffl.getFragInfos().size() );
146     assertEquals( "subInfos=(d((9,10)))/1.0(3,103)", ffl.getFragInfos().get( 0 ).toString() );
147   }
148   
149   public void test1PhraseLongMV() throws Exception {
150     makeIndexLongMV();
151
152     FieldQuery fq = new FieldQuery( pqF( "search", "engines" ), true, true );
153     FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
154     FieldPhraseList fpl = new FieldPhraseList( stack, fq );
155     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
156     FieldFragList ffl = sflb.createFieldFragList( fpl, 100 );
157     assertEquals( 1, ffl.getFragInfos().size() );
158     assertEquals( "subInfos=(searchengines((102,116))searchengines((157,171)))/2.0(96,196)", ffl.getFragInfos().get( 0 ).toString() );
159   }
160
161   public void test1PhraseLongMVB() throws Exception {
162     makeIndexLongMVB();
163
164     FieldQuery fq = new FieldQuery( pqF( "sp", "pe", "ee", "ed" ), true, true ); // "speed" -(2gram)-> "sp","pe","ee","ed"
165     FieldTermStack stack = new FieldTermStack( reader, 0, F, fq );
166     FieldPhraseList fpl = new FieldPhraseList( stack, fq );
167     SimpleFragListBuilder sflb = new SimpleFragListBuilder();
168     FieldFragList ffl = sflb.createFieldFragList( fpl, 100 );
169     assertEquals( 1, ffl.getFragInfos().size() );
170     assertEquals( "subInfos=(sppeeeed((88,93)))/1.0(82,182)", ffl.getFragInfos().get( 0 ).toString() );
171   }
172 }