@Test public void findBestCategoryMatch() { Category maleCategory = Category.create("0", "MALE"); Category femaleCategory = Category.create("1", "FEMALE"); List<Category> targetCategories1 = Lists.newArrayList(maleCategory, femaleCategory); assertEquals( maleCategory, lexicalCategoryMapper.findBestCategoryMatch(create("1", "male"), targetCategories1)); assertEquals( femaleCategory, lexicalCategoryMapper.findBestCategoryMatch(create("2", "female"), targetCategories1)); Category neverStrokeCategory = Category.create("0", "Never had stroke"); Category hasStrokeCategory = Category.create("1", "Has has stroke"); List<Category> targetCategories2 = Lists.newArrayList(neverStrokeCategory, hasStrokeCategory); assertEquals( neverStrokeCategory, lexicalCategoryMapper.findBestCategoryMatch(create("1", "no"), targetCategories2)); assertEquals( hasStrokeCategory, lexicalCategoryMapper.findBestCategoryMatch(create("1", "yes"), targetCategories2)); }
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; }
LexicalCategoryMapper extends CategoryMapper { 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; } }
LexicalCategoryMapper extends CategoryMapper { 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; } LexicalCategoryMapper(List<CategoryRule> rules); }
LexicalCategoryMapper extends CategoryMapper { 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; } LexicalCategoryMapper(List<CategoryRule> rules); Category findBestCategoryMatch(Category sourceCategory, List<Category> targetCategories); CategoryMatchQuality<?> applyCustomRules(
      Category sourceCategory, Category targetCategory); }
LexicalCategoryMapper extends CategoryMapper { 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; } LexicalCategoryMapper(List<CategoryRule> rules); Category findBestCategoryMatch(Category sourceCategory, List<Category> targetCategories); CategoryMatchQuality<?> applyCustomRules(
      Category sourceCategory, Category targetCategory);  }