Search in sources :

Example 1 with Category

use of org.molgenis.semanticmapper.algorithmgenerator.bean.Category in project molgenis by molgenis.

the class InternalAbstractCategoryRuleTest method testCreateNumericQualityIndicatorDoesNotThrowNPE.

@Test
public void testCreateNumericQualityIndicatorDoesNotThrowNPE() {
    InternalAbstractCategoryRule rule = new NegativeCategoryRule();
    Category target = mock(Category.class);
    when(target.getLabel()).thenReturn("label1");
    Category source = mock(Category.class);
    when(source.getLabel()).thenReturn("label2");
    rule.createCategoryMatchQuality(target, source);
}
Also used : Category(org.molgenis.semanticmapper.algorithmgenerator.bean.Category) Test(org.testng.annotations.Test)

Example 2 with Category

use of org.molgenis.semanticmapper.algorithmgenerator.bean.Category in project molgenis by molgenis.

the class FrequencyCategoryMapper method findBestCategoryMatch.

public Category findBestCategoryMatch(Category sourceCategory, List<Category> targetCategories) {
    Category bestTargetCategory = null;
    double smallestFactor = -1;
    for (Category targetCategory : targetCategories) {
        Double convertFactor = convert(sourceCategory.getAmountWrapper(), targetCategory.getAmountWrapper());
        if (convertFactor == null)
            continue;
        if (smallestFactor == -1) {
            smallestFactor = convertFactor;
            bestTargetCategory = targetCategory;
        } else if (smallestFactor > convertFactor) {
            smallestFactor = convertFactor;
            bestTargetCategory = targetCategory;
        }
    }
    return bestTargetCategory;
}
Also used : Category(org.molgenis.semanticmapper.algorithmgenerator.bean.Category)

Example 3 with Category

use of org.molgenis.semanticmapper.algorithmgenerator.bean.Category 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)

Example 4 with Category

use of org.molgenis.semanticmapper.algorithmgenerator.bean.Category in project molgenis by molgenis.

the class LexicalCategoryMapperTest method findBestCategoryMatch.

@Test
public void findBestCategoryMatch() {
    Category maleCategory = Category.create("0", "MALE");
    Category femaleCategory = Category.create("1", "FEMALE");
    List<Category> targetCategories1 = Lists.newArrayList(maleCategory, femaleCategory);
    Assert.assertEquals(lexicalCategoryMapper.findBestCategoryMatch(Category.create("1", "male"), targetCategories1), maleCategory);
    Assert.assertEquals(lexicalCategoryMapper.findBestCategoryMatch(Category.create("2", "female"), targetCategories1), femaleCategory);
    Category neverStrokeCategory = Category.create("0", "Never had stroke");
    Category hasStrokeCategory = Category.create("1", "Has has stroke");
    List<Category> targetCategories2 = Lists.newArrayList(neverStrokeCategory, hasStrokeCategory);
    Assert.assertEquals(lexicalCategoryMapper.findBestCategoryMatch(Category.create("1", "no"), targetCategories2), neverStrokeCategory);
    Assert.assertEquals(lexicalCategoryMapper.findBestCategoryMatch(Category.create("1", "yes"), targetCategories2), hasStrokeCategory);
}
Also used : Category(org.molgenis.semanticmapper.algorithmgenerator.bean.Category) Test(org.testng.annotations.Test)

Example 5 with Category

use of org.molgenis.semanticmapper.algorithmgenerator.bean.Category in project molgenis by molgenis.

the class OneToManyCategoryAlgorithmGenerator method generateWeightedMap.

public String generateWeightedMap(Attribute attribute) {
    StringBuilder stringBuilder = new StringBuilder();
    for (Category sourceCategory : convertToCategory(attribute)) {
        AmountWrapper amountWrapper = sourceCategory.getAmountWrapper();
        if (amountWrapper != null) {
            if (stringBuilder.length() == 0) {
                stringBuilder.append("$('").append(attribute.getName()).append("').map({");
            }
            double estimatedValue = amountWrapper.getAmount().getEstimatedValue();
            stringBuilder.append("\"").append(sourceCategory.getCode()).append("\":").append(DECIMAL_FORMAT.format(estimatedValue)).append(",");
        }
    }
    if (stringBuilder.length() > 0) {
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        stringBuilder.append("}, null, null).value()");
    }
    return stringBuilder.toString();
}
Also used : Category(org.molgenis.semanticmapper.algorithmgenerator.bean.Category) AmountWrapper(org.molgenis.semanticmapper.algorithmgenerator.bean.AmountWrapper)

Aggregations

Category (org.molgenis.semanticmapper.algorithmgenerator.bean.Category)10 Test (org.testng.annotations.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Attribute (org.molgenis.data.meta.model.Attribute)1 EntityType (org.molgenis.data.meta.model.EntityType)1 AmountWrapper (org.molgenis.semanticmapper.algorithmgenerator.bean.AmountWrapper)1 CategoryMatchQuality (org.molgenis.semanticmapper.algorithmgenerator.rules.CategoryMatchQuality)1 CategoryRule (org.molgenis.semanticmapper.algorithmgenerator.rules.CategoryRule)1 NGramDistanceAlgorithm (org.molgenis.semanticsearch.string.NGramDistanceAlgorithm)1