RootTermDistanceCalculator { public int calculateMinimalDistance(String stringToMatch) { var lowerCaseStringToMatch = stringToMatch.toLowerCase(); var mergedSet = mergeRootTermAndDerivedTerms(); boolean matchesRootTermOrDerivedTerm = mergedSet.contains(lowerCaseStringToMatch); if(matchesRootTermOrDerivedTerm) { return 0; } return mergedSet.parallelStream() .mapToInt(term -> calculateLevenshteinDistance(term, lowerCaseStringToMatch)) .min() .orElse(0); } RootTermDistanceCalculator(String rootTerm, Set<String> derivedTerms); int calculateMinimalDistance(String stringToMatch); static String nearestRootTerm(Set<RootTermDistanceCalculator> distanceCalculators, String term, int threshold);  }
@Test public void calculateMinimalDistanceReturnsZeroWhenTermEqualsRootTerm() { var rootTerm = "banana"; var term = rootTerm; var calculator = new RootTermDistanceCalculator(rootTerm, Set.of()); var actualMinDistance = calculator.calculateMinimalDistance(term); assertThat(actualMinDistance).isEqualTo(0); }
@Test public void calculateMinimalDistanceReturnsZeroWhenTermEqualsDerivedTerm() { var rootTerm = "banana"; var derivedTerm = "yellow-thing"; var term = derivedTerm; var calculator = new RootTermDistanceCalculator(rootTerm, Set.of(derivedTerm)); var actualMinDistance = calculator.calculateMinimalDistance(term); assertThat(actualMinDistance).isEqualTo(0); }
@Test public void calculateMinimalDistanceReturnsMinimalDistanceToRootTermAndDerivedTerms() { var rootTerm = "banana"; var derivedTerms = Set.of("yellow-thing", "monkey-snack"); var term = "yllow-thing"; var expectedMinDistance = 1; var calculator = new RootTermDistanceCalculator(rootTerm, derivedTerms); var actualMinDistance = calculator.calculateMinimalDistance(term); assertThat(actualMinDistance).isEqualTo(expectedMinDistance); }