source: josm/trunk/test/unit/org/openstreetmap/josm/data/validation/tests/SimilarNamedWaysTest.groovy@ 7848

Last change on this file since 7848 was 7848, checked in by Don-vip, 9 years ago

fix #3733 - SimilarNamedWays naïvely uses Levenshtein distance and marks a lot of false positives (patch by mdk, brianegge, modified). Rules still need to be stored in JOSM preferences instead of current hardcoding.

File size: 4.2 KB
RevLine 
[7848]1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.data.validation.tests
3
4import static org.junit.Assert.assertEquals
5
6import org.openstreetmap.josm.JOSMFixture
7import org.openstreetmap.josm.data.coor.LatLon
8import org.openstreetmap.josm.data.osm.DataSet
9import org.openstreetmap.josm.data.osm.Node
10import org.openstreetmap.josm.data.osm.Way
11import org.openstreetmap.josm.data.validation.TestError
12
13class SimilarNamedWaysTest extends GroovyTestCase {
14
15 SimilarNamedWays test = new SimilarNamedWays()
16
17 @Override
18 void setUp() {
19 JOSMFixture.createUnitTestFixture().init();
20 }
21
22 public static List<TestError> testWays(String namea, String nameb) {
23 def ds = new DataSet()
24
25 def n00 = new Node(new LatLon(0, 0))
26 def n10 = new Node(new LatLon(1, 0))
27 def n20 = new Node(new LatLon(2, 0))
28 def n30 = new Node(new LatLon(3, 0))
29 def n40 = new Node(new LatLon(4, 0))
30
31 ds.addPrimitive(n00)
32 ds.addPrimitive(n10)
33 ds.addPrimitive(n20)
34 ds.addPrimitive(n30)
35 ds.addPrimitive(n40)
36
37 def waya = new Way()
38 waya.addNode(n00)
39 waya.addNode(n10)
40 waya.addNode(n20)
41 waya.put("name", namea)
42 def wayb = new Way()
43 wayb.addNode(n20)
44 wayb.addNode(n30)
45 wayb.addNode(n40)
46 wayb.put("name", nameb)
47
48 ds.addPrimitive(waya)
49 ds.addPrimitive(wayb)
50
51 assert waya.isUsable()
52 assert wayb.isUsable()
53
54 def t = new SimilarNamedWays()
55 t.startTest(null)
56 t.visit(waya)
57 t.visit(wayb)
58 return t.errors
59 }
60
61 void testCombinations() {
62 assert testWays("Church Street", "Water Street").isEmpty()
63 assert !testWays("Main Street", "Maim Street").isEmpty()
64 assert !testWays("First Street", "Frist Street").isEmpty()
65
66 assert testWays("1st Street", "2nd Street").isEmpty()
67 assert testWays("First Avenue", "Second Avenue").isEmpty()
68 assert testWays("West Main Street", "East Main Street").isEmpty()
69 assert testWays("A Street", "B Street").isEmpty()
70 }
71
72 void checkSimilarity(String message, String name1, String name2, boolean expected) {
73 boolean actual = test.similaryName(name1, name2);
74 assertEquals(message, expected, actual);
75 }
76
77 void testSimilarNames() {
78 checkSimilarity("same string", "Testname", "Testname", false);
79 checkSimilarity("different case", "Testname", "TestName", true);
80 checkSimilarity("typo", "Testname", "Testxame", true);
81 checkSimilarity("missing char", "Testname", "Testame", true);
82 checkSimilarity("additional char", "Testname", "Testxname", true);
83 checkSimilarity("2 changes", "Testname", "Tostxname", true);
84 checkSimilarity("3 changes", "Testname", "Tostxnam", false);
85
86 // regular expression rule
87 checkSimilarity("same number", "track 1", "track 1", false);
88 checkSimilarity("different number", "track 1", "track 2", false);
89 checkSimilarity("different number length", "track 9", "track 10", false);
90 checkSimilarity("multiple numbers", "track 8 - 9", "track 10 - 11", false);
91
92 checkSimilarity("1st and 2nd", "1st Street", "2nd Street", false);
93 checkSimilarity("1st case", "1St Street", "1st Street", true);
94 checkSimilarity("1st and 2nd case", "1St Street", "2nd Street", true);
95 checkSimilarity("3rd and 4th", "2rd Street", "4th Street", false);
96
97 // synonyms
98 checkSimilarity("east and west", "East Foothill Drive", "West Foothill Drive", false);
99 checkSimilarity("east and west case", "east Foothill Drive", "West Foothill Drive", true);
100 checkSimilarity("first and second", "First Street", "Second Street", false);
101 checkSimilarity("first and second case", "First Street", "second Street", true);
102 checkSimilarity("first and second typo", "Forst Street", "Second Street", true);
103 checkSimilarity("first and second typo2", "First Street", "Socond Street", true);
104 checkSimilarity("first and second 2 changes", "First Street", "Soconds Street", true);
105 checkSimilarity("first and second 3 changes", "First Street", "Soconds Stret", false);
106 }
107}
Note: See TracBrowser for help on using the repository browser.