use of org.molgenis.semanticmapper.algorithmgenerator.rules.CategoryMatchQuality in project molgenis by molgenis.
the class LexicalCategoryMapper method findBestCategoryMatch.
public Category findBestCategoryMatch(Category sourceCategory, List<Category> targetCategories) {
String sourceCategoryLabel = sourceCategory.getLabel().toLowerCase();
Category bestCategory = null;
double bestNGramScore = -1;
for (Category targetCategory : targetCategories) {
String targetCategoryLabel = targetCategory.getLabel();
if (StringUtils.equalsIgnoreCase(sourceCategoryLabel, targetCategoryLabel)) {
return targetCategory;
}
double ngramScore = NGramDistanceAlgorithm.stringMatching(sourceCategoryLabel, targetCategoryLabel);
if (bestNGramScore == -1 || bestNGramScore < ngramScore) {
bestNGramScore = ngramScore;
bestCategory = targetCategory;
}
}
if (bestNGramScore < DEFAULT_THRESHOLD) {
Optional<?> findFirst = targetCategories.stream().map(targetCategory -> applyCustomRules(sourceCategory, targetCategory)).filter(Objects::nonNull).sorted().findFirst();
if (findFirst.isPresent() && findFirst.get() instanceof CategoryMatchQuality) {
bestCategory = ((CategoryMatchQuality<?>) findFirst.get()).getTargetCategory();
}
}
return bestCategory;
}
Aggregations