@Override public void setup() { d = new org.apache.commons.text.similarity.FuzzyScore(java.util.Locale.ENGLISH); }
@Signature public Integer fuzzyScore(Environment env, Memory query, @Nullable Locale locale) { FuzzyScore cosineDistance = new FuzzyScore(locale != null ? locale : env.getLocale()); return cosineDistance.fuzzyScore(text, query.toString()); } }
@Override public Object apply(List<Object> list) { if (list.size() < 3) { throw new IllegalStateException("FUZZY_SCORE expects three args: [string, string, string]"); } Object oterm = list.get(0); Object oquery = list.get(1); Object olang = list.get(2); // return 0 here, validate will pass 3 nulls // if we change validate to pass default of expected type, we can differentiate if (!(oterm instanceof String) || !(oquery instanceof String) || !(olang instanceof String)) { return 0; } String term = (String) oterm; String query = (String) oquery; String lang = (String) olang; if (!tagsList.contains(lang)) { throw new ParseException( "FUZZY_SCORE requires a valid IETF BCP47 language code see FUZZY_LANGS and https://tools.ietf.org/html/bcp47"); } if (StringUtils.isEmpty(term) || StringUtils.isEmpty(query)) { return 0; } Locale locale = Locale.forLanguageTag(lang); FuzzyScore score = new FuzzyScore(locale); return score.fuzzyScore(term, query); } }
@Test public void testGetLocale() { final Locale locale = Locale.CANADA_FRENCH; final FuzzyScore fuzzyScore = new FuzzyScore(locale); final Locale localeTwo = fuzzyScore.getLocale(); assertThat(localeTwo).isSameAs(locale); }
@Test public void testMissingLocale() { assertThatIllegalArgumentException().isThrownBy(() -> { new FuzzyScore((Locale) null); }); }
@Override public double calculate(CharSequence s, CharSequence t) { FuzzyScore distance = new FuzzyScore(locale); return distance.fuzzyScore(s, t); }