Search in sources :

Example 1 with CategoryMatchQuality

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;
}
Also used : NGramDistanceAlgorithm(org.molgenis.semanticsearch.string.NGramDistanceAlgorithm) Objects(java.util.Objects) CategoryMatchQuality(org.molgenis.semanticmapper.algorithmgenerator.rules.CategoryMatchQuality) List(java.util.List) CategoryRule(org.molgenis.semanticmapper.algorithmgenerator.rules.CategoryRule) Category(org.molgenis.semanticmapper.algorithmgenerator.bean.Category) Optional(java.util.Optional) StringUtils(org.apache.commons.lang3.StringUtils) Category(org.molgenis.semanticmapper.algorithmgenerator.bean.Category) Objects(java.util.Objects) CategoryMatchQuality(org.molgenis.semanticmapper.algorithmgenerator.rules.CategoryMatchQuality)

Aggregations

List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Category (org.molgenis.semanticmapper.algorithmgenerator.bean.Category)1 CategoryMatchQuality (org.molgenis.semanticmapper.algorithmgenerator.rules.CategoryMatchQuality)1 CategoryRule (org.molgenis.semanticmapper.algorithmgenerator.rules.CategoryRule)1 NGramDistanceAlgorithm (org.molgenis.semanticsearch.string.NGramDistanceAlgorithm)1