add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spellchecker / src / test / org / apache / lucene / search / spell / TestNGramDistance.java
1 package org.apache.lucene.search.spell;
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.util.LuceneTestCase;
21
22 public class TestNGramDistance extends LuceneTestCase {
23
24   
25   
26   public void testGetDistance1() {
27     StringDistance nsd = new NGramDistance(1);
28     float d = nsd.getDistance("al", "al");
29     assertEquals(d,1.0f,0.001);
30     d = nsd.getDistance("a", "a");
31     assertEquals(d,1.0f,0.001);
32     d = nsd.getDistance("b", "a");
33     assertEquals(d,0.0f,0.001);
34     d = nsd.getDistance("martha", "marhta");
35     assertEquals(d,0.6666,0.001);
36     d = nsd.getDistance("jones", "johnson");
37     assertEquals(d,0.4285,0.001);
38     d = nsd.getDistance("natural", "contrary");
39     assertEquals(d,0.25,0.001);
40     d = nsd.getDistance("abcvwxyz", "cabvwxyz");
41     assertEquals(d,0.75,0.001);    
42     d = nsd.getDistance("dwayne", "duane");
43     assertEquals(d,0.666,0.001);
44     d = nsd.getDistance("dixon", "dicksonx");
45     assertEquals(d,0.5,0.001);
46     d = nsd.getDistance("six", "ten");
47     assertEquals(d,0,0.001);
48     float d1 = nsd.getDistance("zac ephron", "zac efron");
49     float d2 = nsd.getDistance("zac ephron", "kai ephron");
50     assertEquals(d1,d2,0.001);
51     d1 = nsd.getDistance("brittney spears", "britney spears");
52     d2 = nsd.getDistance("brittney spears", "brittney startzman");
53     assertTrue(d1 > d2);
54     d1 = nsd.getDistance("12345678", "12890678");
55     d2 = nsd.getDistance("12345678", "72385698");
56     assertEquals(d1,d2,001);
57   }
58   
59   public void testGetDistance2() {
60     StringDistance sd = new NGramDistance(2);
61     float d = sd.getDistance("al", "al");
62     assertEquals(d,1.0f,0.001);
63     d = sd.getDistance("a", "a");
64     assertEquals(d,1.0f,0.001);
65     d = sd.getDistance("b", "a");
66     assertEquals(d,0.0f,0.001);
67     d = sd.getDistance("a", "aa");
68     assertEquals(d,0.5f,0.001);
69     d = sd.getDistance("martha", "marhta");
70     assertEquals(d,0.6666,0.001);
71     d = sd.getDistance("jones", "johnson");
72     assertEquals(d,0.4285,0.001);
73     d = sd.getDistance("natural", "contrary");
74     assertEquals(d,0.25,0.001);
75     d = sd.getDistance("abcvwxyz", "cabvwxyz");
76     assertEquals(d,0.625,0.001);    
77     d = sd.getDistance("dwayne", "duane");
78     assertEquals(d,0.5833,0.001);
79     d = sd.getDistance("dixon", "dicksonx");
80     assertEquals(d,0.5,0.001);
81     d = sd.getDistance("six", "ten");
82     assertEquals(d,0,0.001);
83     float d1 = sd.getDistance("zac ephron", "zac efron");
84     float d2 = sd.getDistance("zac ephron", "kai ephron");
85     assertTrue(d1 > d2);
86     d1 = sd.getDistance("brittney spears", "britney spears");
87     d2 = sd.getDistance("brittney spears", "brittney startzman");
88     assertTrue(d1 > d2);
89     d1 = sd.getDistance("0012345678", "0012890678");
90     d2 = sd.getDistance("0012345678", "0072385698");
91     assertEquals(d1,d2,0.001);
92   }
93   
94   public void testGetDistance3() {
95     StringDistance sd = new NGramDistance(3);
96     float d = sd.getDistance("al", "al");
97     assertEquals(d,1.0f,0.001);
98     d = sd.getDistance("a", "a");
99     assertEquals(d,1.0f,0.001);
100     d = sd.getDistance("b", "a");
101     assertEquals(d,0.0f,0.001);
102     d = sd.getDistance("martha", "marhta");
103     assertEquals(d,0.7222,0.001);
104     d = sd.getDistance("jones", "johnson");
105     assertEquals(d,0.4762,0.001);
106     d = sd.getDistance("natural", "contrary");
107     assertEquals(d,0.2083,0.001);
108     d = sd.getDistance("abcvwxyz", "cabvwxyz");
109     assertEquals(d,0.5625,0.001);    
110     d = sd.getDistance("dwayne", "duane");
111     assertEquals(d,0.5277,0.001);
112     d = sd.getDistance("dixon", "dicksonx");
113     assertEquals(d,0.4583,0.001);
114     d = sd.getDistance("six", "ten");
115     assertEquals(d,0,0.001);
116     float d1 = sd.getDistance("zac ephron", "zac efron");
117     float d2 = sd.getDistance("zac ephron", "kai ephron");
118     assertTrue(d1 > d2);
119     d1 = sd.getDistance("brittney spears", "britney spears");
120     d2 = sd.getDistance("brittney spears", "brittney startzman");
121     assertTrue(d1 > d2);
122     d1 = sd.getDistance("0012345678", "0012890678");
123     d2 = sd.getDistance("0012345678", "0072385698");
124     assertTrue(d1 < d2);
125   }
126
127   public void testEmpty() throws Exception {
128     StringDistance nsd = new NGramDistance(1);
129     float d = nsd.getDistance("", "al");
130     assertEquals(d,0.0f,0.001);
131   }
132 }