Skip to content

Commit 3bdae54

Browse files
committed
Added Levenshtein tests
1 parent 00e1752 commit 3bdae54

File tree

3 files changed

+118
-10
lines changed

3 files changed

+118
-10
lines changed

src/main/java/info/debatty/java/stringsimilarity/Levenshtein.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,12 @@
55
/**
66
* The Levenshtein distance between two words is the minimum number of
77
* single-character edits (insertions, deletions or substitutions) required to
8-
* change one word into the other.
8+
* change one string into the other.
99
*
1010
* @author Thibault Debatty
1111
*/
1212
public class Levenshtein implements MetricStringDistance {
1313

14-
public static void main (String[] args) {
15-
Levenshtein l = new Levenshtein();
16-
17-
System.out.println(l.distance("My string", "My $tring"));
18-
System.out.println(l.distance("My string", "M string2"));
19-
System.out.println(l.distance("My string", "My $tring"));
20-
}
21-
22-
2314
/**
2415
* The Levenshtein distance, or edit distance, between two words is the
2516
* minimum number of single-character edits (insertions, deletions or
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2015 Thibault Debatty.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package info.debatty.java.stringsimilarity.examples;
26+
27+
import info.debatty.java.stringsimilarity.Levenshtein;
28+
29+
/**
30+
*
31+
* @author Thibault Debatty
32+
*/
33+
public class Examples {
34+
35+
/**
36+
* @param args the command line arguments
37+
*/
38+
public static void main(String[] args) {
39+
Levenshtein levenshtein = new Levenshtein();
40+
41+
System.out.println(levenshtein.distance("My string", "My $tring"));
42+
System.out.println(levenshtein.distance("My string", "M string2"));
43+
System.out.println(levenshtein.distance("My string", "My $tring"));
44+
}
45+
46+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright 2015 Thibault Debatty.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package info.debatty.java.stringsimilarity;
26+
27+
import org.junit.After;
28+
import org.junit.AfterClass;
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import static org.junit.Assert.*;
33+
34+
/**
35+
*
36+
* @author Thibault Debatty
37+
*/
38+
public class LevenshteinTest {
39+
40+
public LevenshteinTest() {
41+
}
42+
43+
@BeforeClass
44+
public static void setUpClass() {
45+
}
46+
47+
@AfterClass
48+
public static void tearDownClass() {
49+
}
50+
51+
@Before
52+
public void setUp() {
53+
}
54+
55+
@After
56+
public void tearDown() {
57+
}
58+
59+
/**
60+
* Test of distance method, of class Levenshtein.
61+
*/
62+
@Test
63+
public void testDistance() {
64+
System.out.println("distance");
65+
Levenshtein instance = new Levenshtein();
66+
assertEquals(1.0, instance.distance("My string", "My tring"), 0.0);
67+
assertEquals(2.0, instance.distance("My string", "M string2"), 0.0);
68+
assertEquals(1.0, instance.distance("My string", "My $tring"), 0.0);
69+
}
70+
71+
}

0 commit comments

Comments
 (0)