Changeset 14508 in josm for trunk/src/org
- Timestamp:
- 2018-12-04T08:08:46+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java
r14490 r14508 14 14 import java.util.Collections; 15 15 import java.util.HashMap; 16 import java.util.HashSet; 16 17 import java.util.List; 17 18 import java.util.Locale; … … 77 78 private static final List<CheckerData> checkerData = new ArrayList<>(); 78 79 private static final List<String> ignoreDataStartsWith = new ArrayList<>(); 79 private static final List<String> ignoreDataEquals = new ArrayList<>();80 private static final Set<String> ignoreDataEquals = new HashSet<>(); 80 81 private static final List<String> ignoreDataEndsWith = new ArrayList<>(); 81 82 private static final List<Tag> ignoreDataTag = new ArrayList<>(); … … 123 124 */ 124 125 public static final String PREF_CHECK_FIXMES_BEFORE_UPLOAD = PREF_CHECK_FIXMES + "BeforeUpload"; 126 127 private static final int MAX_LEVENSHTEIN_DISTANCE = 2; 125 128 126 129 protected boolean checkKeys; … … 142 145 143 146 // CHECKSTYLE.OFF: SingleSpaceSeparator 144 protected static final int EMPTY_VALUES = 1200; 145 protected static final int INVALID_KEY = 1201; 146 protected static final int INVALID_VALUE = 1202; 147 protected static final int FIXME = 1203; 148 protected static final int INVALID_SPACE = 1204; 149 protected static final int INVALID_KEY_SPACE = 1205; 150 protected static final int INVALID_HTML = 1206; /* 1207 was PAINT */ 151 protected static final int LONG_VALUE = 1208; 152 protected static final int LONG_KEY = 1209; 153 protected static final int LOW_CHAR_VALUE = 1210; 154 protected static final int LOW_CHAR_KEY = 1211; 155 protected static final int MISSPELLED_VALUE = 1212; 156 protected static final int MISSPELLED_KEY = 1213; 157 protected static final int MULTIPLE_SPACES = 1214; 147 protected static final int EMPTY_VALUES = 1200; 148 protected static final int INVALID_KEY = 1201; 149 protected static final int INVALID_VALUE = 1202; 150 protected static final int FIXME = 1203; 151 protected static final int INVALID_SPACE = 1204; 152 protected static final int INVALID_KEY_SPACE = 1205; 153 protected static final int INVALID_HTML = 1206; /* 1207 was PAINT */ 154 protected static final int LONG_VALUE = 1208; 155 protected static final int LONG_KEY = 1209; 156 protected static final int LOW_CHAR_VALUE = 1210; 157 protected static final int LOW_CHAR_KEY = 1211; 158 protected static final int MISSPELLED_VALUE = 1212; 159 protected static final int MISSPELLED_KEY = 1213; 160 protected static final int MULTIPLE_SPACES = 1214; 161 protected static final int MISSPELLED_VALUE_NO_FIX = 1215; 158 162 // CHECKSTYLE.ON: SingleSpaceSeparator 159 163 // 1250 and up is used by tagcheck … … 388 392 */ 389 393 public static boolean isTagIgnored(String key, String value) { 390 boolean tagInPresets = isTagInPresets(key, value);391 boolean ignore = false;392 394 if (ignoreDataEquals.contains(key)) { 395 return true; 396 } 393 397 for (String a : ignoreDataStartsWith) { 394 398 if (key.startsWith(a)) { 395 ignore = true; 396 } 397 } 398 for (String a : ignoreDataEquals) { 399 if (key.equals(a)) { 400 ignore = true; 399 return true; 401 400 } 402 401 } 403 402 for (String a : ignoreDataEndsWith) { 404 403 if (key.endsWith(a)) { 405 ignore =true;406 } 407 } 408 409 if (! tagInPresets) {404 return true; 405 } 406 } 407 408 if (!isTagInPresets(key, value)) { 410 409 for (Tag a : ignoreDataTag) { 411 410 if (key.equals(a.getKey()) && value.equals(a.getValue())) { 412 ignore =true;411 return true; 413 412 } 414 413 } 415 414 } 416 return ignore;415 return false; 417 416 } 418 417 … … 535 534 // try to fix common typos and check again if value is still unknown 536 535 String fixedValue = harmonizeValue(prop.getValue()); 537 Map<String, String> possibleValues = getPossibleValues(getPresetValues(key));536 Set<String> possibleValues = getPresetValues(key); 538 537 List<String> fixVals = new ArrayList<>(); 539 if (!possibleValues.containsKey(fixedValue)) { 540 int minDist = 2; 538 int maxPresetValueLen = 0; 539 if (!possibleValues.contains(fixedValue)) { 540 // use Levenshtein distance to find typical typos 541 int minDist = MAX_LEVENSHTEIN_DISTANCE + 1; 541 542 String closest = null; 542 for (String possibleVal : possibleValues.keySet()) { 543 for (String possibleVal : possibleValues) { 544 if (possibleVal.isEmpty()) 545 continue; 546 maxPresetValueLen = Math.max(maxPresetValueLen, possibleVal.length()); 543 547 int dist = Utils.getLevenshteinDistance(possibleVal, fixedValue); 544 548 if (dist < minDist) { … … 551 555 } 552 556 } 553 if (minDist <= 1) { 557 fixedValue = null; 558 if (minDist <= MAX_LEVENSHTEIN_DISTANCE && maxPresetValueLen > MAX_LEVENSHTEIN_DISTANCE) { 554 559 if (fixVals.size() < 2) { 555 560 fixedValue = closest; … … 557 562 Collections.sort(fixVals); 558 563 // misspelled preset value with multiple good alternatives 559 errors.add(TestError.builder(this, Severity.WARNING, MISSPELLED_VALUE )564 errors.add(TestError.builder(this, Severity.WARNING, MISSPELLED_VALUE_NO_FIX) 560 565 .message(tr("Misspelled property value"), 561 566 marktr("Value ''{0}'' for key ''{1}'' looks like one of {2}."), prop.getValue(), key, fixVals) … … 567 572 } 568 573 } 569 if ( possibleValues.containsKey(fixedValue)) {570 final String newValue = possibleValues.get(fixedValue);574 if (fixedValue != null && possibleValues.contains(fixedValue)) { 575 final String newValue = fixedValue; 571 576 // misspelled preset value 572 577 errors.add(TestError.builder(this, Severity.WARNING, MISSPELLED_VALUE) … … 603 608 } 604 609 605 private static Map<String, String> getPossibleValues(Set<String> values) {606 // generate a map with common typos607 Map<String, String> map = new HashMap<>();608 if (values != null) {609 for (String value : values) {610 map.put(value, value);611 if (value.contains("_")) {612 map.put(value.replace("_", ""), value);613 }614 }615 }616 return map;617 }618 619 610 private static String harmonizeKey(String key) { 620 611 return Utils.strip(key.toLowerCase(Locale.ENGLISH).replace('-', '_').replace(':', '_').replace(' ', '_'), "-_;:,");
Note:
See TracChangeset
for help on using the changeset viewer.