use of org.apache.lucene.search.spell.LevensteinDistance in project elasticsearch by elastic.
the class AbstractScopedSettings method validate.
/**
* Validates that the setting is valid
*/
public final void validate(String key, Settings settings) {
Setting setting = get(key);
if (setting == null) {
LevensteinDistance ld = new LevensteinDistance();
List<Tuple<Float, String>> scoredKeys = new ArrayList<>();
for (String k : this.keySettings.keySet()) {
float distance = ld.getDistance(key, k);
if (distance > 0.7f) {
scoredKeys.add(new Tuple<>(distance, k));
}
}
CollectionUtil.timSort(scoredKeys, (a, b) -> b.v1().compareTo(a.v1()));
String msgPrefix = "unknown setting";
SecureSettings secureSettings = settings.getSecureSettings();
if (secureSettings != null && settings.getSecureSettings().getSettingNames().contains(key)) {
msgPrefix = "unknown secure setting";
}
String msg = msgPrefix + " [" + key + "]";
List<String> keys = scoredKeys.stream().map((a) -> a.v2()).collect(Collectors.toList());
if (keys.isEmpty() == false) {
msg += " did you mean " + (keys.size() == 1 ? "[" + keys.get(0) + "]" : "any of " + keys.toString()) + "?";
} else {
msg += " please check that any required plugins are installed, or check the breaking changes documentation for removed " + "settings";
}
throw new IllegalArgumentException(msg);
}
setting.get(settings);
}
use of org.apache.lucene.search.spell.LevensteinDistance in project elasticsearch by elastic.
the class BaseRestHandler method unrecognized.
protected final String unrecognized(final RestRequest request, final Set<String> invalids, final Set<String> candidates, final String detail) {
String message = String.format(Locale.ROOT, "request [%s] contains unrecognized %s%s: ", request.path(), detail, invalids.size() > 1 ? "s" : "");
boolean first = true;
for (final String invalid : invalids) {
final LevensteinDistance ld = new LevensteinDistance();
final List<Tuple<Float, String>> scoredParams = new ArrayList<>();
for (final String candidate : candidates) {
final float distance = ld.getDistance(invalid, candidate);
if (distance > 0.5f) {
scoredParams.add(new Tuple<>(distance, candidate));
}
}
CollectionUtil.timSort(scoredParams, (a, b) -> {
// sort by distance in reverse order, then parameter name for equal distances
int compare = a.v1().compareTo(b.v1());
if (compare != 0)
return -compare;
else
return a.v2().compareTo(b.v2());
});
if (first == false) {
message += ", ";
}
message += "[" + invalid + "]";
final List<String> keys = scoredParams.stream().map(Tuple::v2).collect(Collectors.toList());
if (keys.isEmpty() == false) {
message += " -> did you mean " + (keys.size() == 1 ? "[" + keys.get(0) + "]" : "any of " + keys.toString()) + "?";
}
first = false;
}
return message;
}
use of org.apache.lucene.search.spell.LevensteinDistance in project lucene-solr by apache.
the class ConjunctionSolrSpellCheckerTest method test.
@Test
public void test() throws Exception {
ConjunctionSolrSpellChecker cssc = new ConjunctionSolrSpellChecker();
MockSolrSpellChecker levenstein1 = new MockSolrSpellChecker(new LevensteinDistance());
MockSolrSpellChecker levenstein2 = new MockSolrSpellChecker(new LevensteinDistance());
MockSolrSpellChecker ngram = new MockSolrSpellChecker(new NGramDistance());
cssc.addChecker(levenstein1);
cssc.addChecker(levenstein2);
try {
cssc.addChecker(ngram);
fail("ConjunctionSolrSpellChecker should have thrown an exception about non-identical StringDistances.");
} catch (IllegalArgumentException iae) {
// correct behavior
}
}
use of org.apache.lucene.search.spell.LevensteinDistance in project lucene-solr by apache.
the class AbstractLuceneSpellChecker method init.
@Override
public String init(NamedList config, SolrCore core) {
super.init(config, core);
indexDir = (String) config.get(INDEX_DIR);
String accuracy = (String) config.get(ACCURACY);
//If indexDir is relative then create index inside core.getDataDir()
if (indexDir != null) {
if (!new File(indexDir).isAbsolute()) {
indexDir = core.getDataDir() + File.separator + indexDir;
}
}
sourceLocation = (String) config.get(LOCATION);
String compClass = (String) config.get(COMPARATOR_CLASS);
Comparator<SuggestWord> comp = null;
if (compClass != null) {
if (compClass.equalsIgnoreCase(SCORE_COMP)) {
comp = SuggestWordQueue.DEFAULT_COMPARATOR;
} else if (compClass.equalsIgnoreCase(FREQ_COMP)) {
comp = new SuggestWordFrequencyComparator();
} else {
//must be a FQCN
comp = (Comparator<SuggestWord>) core.getResourceLoader().newInstance(compClass, Comparator.class);
}
} else {
comp = SuggestWordQueue.DEFAULT_COMPARATOR;
}
String strDistanceName = (String) config.get(STRING_DISTANCE);
if (strDistanceName != null) {
sd = core.getResourceLoader().newInstance(strDistanceName, StringDistance.class);
//TODO: Figure out how to configure options. Where's Spring when you need it? Or at least BeanUtils...
} else {
sd = new LevensteinDistance();
}
try {
initIndex();
spellChecker = new SpellChecker(index, sd, comp);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (accuracy != null) {
try {
this.accuracy = Float.parseFloat(accuracy);
spellChecker.setAccuracy(this.accuracy);
} catch (NumberFormatException e) {
throw new RuntimeException("Unparseable accuracy given for dictionary: " + name, e);
}
}
return name;
}
use of org.apache.lucene.search.spell.LevensteinDistance in project elasticsearch by elastic.
the class InstallPluginCommand method checkMisspelledPlugin.
/** Returns all the official plugin names that look similar to pluginId. **/
private List<String> checkMisspelledPlugin(String pluginId) {
LevensteinDistance ld = new LevensteinDistance();
List<Tuple<Float, String>> scoredKeys = new ArrayList<>();
for (String officialPlugin : OFFICIAL_PLUGINS) {
float distance = ld.getDistance(pluginId, officialPlugin);
if (distance > 0.7f) {
scoredKeys.add(new Tuple<>(distance, officialPlugin));
}
}
CollectionUtil.timSort(scoredKeys, (a, b) -> b.v1().compareTo(a.v1()));
return scoredKeys.stream().map((a) -> a.v2()).collect(Collectors.toList());
}
Aggregations