@Override public void setup() { d = new org.apache.commons.text.similarity.LevenshteinDistance(); }
@Signature public Integer levenshteinDistance(Environment env, Memory other, @Optional("null") Memory threshold) { LevenshteinDistance distance = new LevenshteinDistance( threshold.isNull() ? null : threshold.toInteger() ); return distance.apply(text, other.toString()); }
public static Stream<Arguments> parameters() { return Stream.of( Arguments.of(new LevenshteinDistance(), "elephant", "hippo", 7), Arguments.of(new LevenshteinDistance(), "hippo", "elephant", 7), Arguments.of(new LevenshteinDistance(), "hippo", "zzzzzzzz", 8), Arguments.of( new SimilarityScore<Boolean>() { @Override public Boolean apply(final CharSequence left, final CharSequence right) { return left == right || (left != null && left.equals(right)); } }, "Bob's your uncle.", "Every good boy does fine.", false )); }
@ParameterizedTest @MethodSource("parameters") public void test(final Integer threshold, final CharSequence left, final CharSequence right, final Integer distance) { final LevenshteinDistance metric = new LevenshteinDistance(threshold); assertThat(metric.apply(left, right)).isEqualTo(distance); }
@Test public void testJavadocExample() { final EditDistance<Integer> metric = new LevenshteinDistance(); final String target = "Apache"; final EditDistanceFrom<Integer> metricFrom = new EditDistanceFrom<>(metric, target); String mostSimilar = null; Integer shortestDistance = null; for (final String test : new String[] {"Appaloosa", "a patchy", "apple" }) { final Integer distance = metricFrom.apply(test); if (shortestDistance == null || distance < shortestDistance) { shortestDistance = distance; mostSimilar = test; } } assertThat(mostSimilar).isEqualTo("a patchy"); assertThat(shortestDistance).isEqualTo(4); }
@Test public void testEquivalence() { final EditDistance<Integer> metric = new LevenshteinDistance(); final String left = "Apache"; final String right = "a patchy"; final Integer distance = 4; final EditDistanceFrom<Integer> metricFrom = new EditDistanceFrom<>(metric, left); assertThat(metricFrom.apply(right)).isEqualTo(distance); assertThat(metricFrom.apply(right)).isEqualTo(metric.apply(left, right)); }
assertThat(new LevenshteinDistance(0).apply("", "")).isEqualTo(0); assertThat(new LevenshteinDistance(8).apply("aaapppp", "")).isEqualTo(7); assertThat(new LevenshteinDistance(7).apply("aaapppp", "")).isEqualTo(7); assertThat(new LevenshteinDistance(6).apply("aaapppp", "")).isEqualTo(-1); assertThat(new LevenshteinDistance(0).apply("b", "a")).isEqualTo(-1); assertThat(new LevenshteinDistance(0).apply("a", "b")).isEqualTo(-1); assertThat(new LevenshteinDistance(0).apply("aa", "aa")).isEqualTo(0); assertThat(new LevenshteinDistance(2).apply("aa", "aa")).isEqualTo(0); assertThat(new LevenshteinDistance(2).apply("aaa", "bbb")).isEqualTo(-1); assertThat(new LevenshteinDistance(3).apply("aaa", "bbb")).isEqualTo(3); assertThat(new LevenshteinDistance(10).apply("aaaaaa", "b")).isEqualTo(6); assertThat(new LevenshteinDistance(8).apply("aaapppp", "b")).isEqualTo(7); assertThat(new LevenshteinDistance(4).apply("a", "bbb")).isEqualTo(3); assertThat(new LevenshteinDistance(7).apply("aaapppp", "b")).isEqualTo(7); assertThat(new LevenshteinDistance(3).apply("a", "bbb")).isEqualTo(3); assertThat(new LevenshteinDistance(2).apply("a", "bbb")).isEqualTo(-1); assertThat(new LevenshteinDistance(2).apply("bbb", "a")).isEqualTo(-1); assertThat(new LevenshteinDistance(6).apply("aaapppp", "b")).isEqualTo(-1); assertThat(new LevenshteinDistance(1).apply("a", "bbb")).isEqualTo(-1); assertThat(new LevenshteinDistance(1).apply("bbb", "a")).isEqualTo(-1);
@Test public void testGetThresholdDirectlyAfterObjectInstantiation() { assertThat(new LevenshteinDistance().getThreshold()).isNull(); }
public static Stream<Arguments> parameters() { return Stream.of( Arguments.of(new HammingDistance(), "Sam I am.", "Ham I am.", 1), Arguments.of(new HammingDistance(), "Japtheth, Ham, Shem", "Japtheth, HAM, Shem", 2), Arguments.of(new HammingDistance(), "Hamming", "Hamming", 0), Arguments.of(new LevenshteinDistance(), "Apache", "a patchy", 4), Arguments.of(new LevenshteinDistance(), "go", "no go", 3), Arguments.of(new LevenshteinDistance(), "go", "go", 0), Arguments.of(new LevenshteinDistance(4), "Apache", "a patchy", 4), Arguments.of(new LevenshteinDistance(4), "go", "no go", 3), Arguments.of(new LevenshteinDistance(0), "go", "go", 0), Arguments.of( new EditDistance<Boolean>() { @Override public Boolean apply(final CharSequence left, final CharSequence right) { return left == right || (left != null && left.equals(right)); } }, "Bob's your uncle.", "Every good boy does fine.", false)); }
@Test public void testConstructorWithNegativeThreshold() { assertThatIllegalArgumentException().isThrownBy(() -> new LevenshteinDistance(-1)); }
@Test public void testApplyThrowsIllegalArgumentExceptionAndCreatesLevenshteinDistanceTakingInteger() { assertThatIllegalArgumentException().isThrownBy(() -> new LevenshteinDistance(0).apply(null, null)); }
@Override public double calculate(CharSequence s, CharSequence t) { org.apache.commons.text.similarity.LevenshteinDistance distance = new org.apache.commons.text.similarity.LevenshteinDistance(); return distance.apply(s, t); }
LevenshteinDistance lev = new LevenshteinDistance(); l.setLevMatchLabel(lev.apply(mention, altLabel)); l.setLevContext(lev.apply(tokensToString(finalMentionContext), altLabel));
LevenshteinDistance lev = new LevenshteinDistance(); l.setLevMatchLabel(lev.apply(mention, altLabel)); l.setLevContext(lev.apply(tokensToString(finalMentionContext), altLabel));