@Test void redistributedNGramScoreSingleWord() { String ontologyIri = "http: Entity ontologyEntity = ontologyFactory.create(); ontologyEntity.set(OntologyMetadata.ONTOLOGY_IRI, ontologyIri); when(dataService.findOne( ONTOLOGY, new QueryImpl<>().eq(OntologyMetadata.ONTOLOGY_IRI, ontologyIri))) .thenReturn(ontologyEntity); when(dataService.count( ONTOLOGY_TERM, new QueryImpl<>().eq(OntologyTermMetadata.ONTOLOGY, ontologyEntity))) .thenReturn((long) 100); QueryRule queryRule = new QueryRule( singletonList( new QueryRule( OntologyTermMetadata.ONTOLOGY_TERM_SYNONYM, Operator.FUZZY_MATCH, "hear"))); queryRule.setOperator(Operator.DIS_MAX); QueryRule finalQuery = new QueryRule( asList( new QueryRule(OntologyTermMetadata.ONTOLOGY, Operator.EQUALS, ontologyEntity), new QueryRule(Operator.AND), queryRule)); when(dataService.count(ONTOLOGY_TERM, new QueryImpl<>(finalQuery))).thenReturn((long) 30); Map<String, Double> redistributedNGramScore = informationContentService.redistributedNGramScore("hearing", ontologyIri); assertTrue(redistributedNGramScore.isEmpty()); }
public Map<String, Double> redistributedNGramScore(String queryString, String ontologyIri) { Map<String, Double> wordIDFMap = createWordIDF(queryString, ontologyIri); Map<String, Double> wordWeightedSimilarity = new HashMap<>(); if (wordIDFMap.size() > 0) { double averageIDFValue = wordIDFMap.values().stream().mapToDouble(Double::doubleValue).average().getAsDouble(); double queryStringLength = StringUtils.join(createStemmedWordSet(queryString), SINGLE_WHITESPACE).trim().length(); double totalContribution = 0; double totalDenominator = 0; for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff < 0) { Double contributedSimilarity = (entry.getKey().length() / queryStringLength * 100) * (diff / averageIDFValue); totalContribution += Math.abs(contributedSimilarity); wordWeightedSimilarity.put(entry.getKey(), contributedSimilarity); } else { totalDenominator += diff; } } for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff > 0) { if (totalDenominator == 0) { throw new IllegalStateException("totalDenominator is 0"); } wordWeightedSimilarity.put( entry.getKey(), ((diff / totalDenominator) * totalContribution)); } } } return wordWeightedSimilarity; }
InformationContentService { public Map<String, Double> redistributedNGramScore(String queryString, String ontologyIri) { Map<String, Double> wordIDFMap = createWordIDF(queryString, ontologyIri); Map<String, Double> wordWeightedSimilarity = new HashMap<>(); if (wordIDFMap.size() > 0) { double averageIDFValue = wordIDFMap.values().stream().mapToDouble(Double::doubleValue).average().getAsDouble(); double queryStringLength = StringUtils.join(createStemmedWordSet(queryString), SINGLE_WHITESPACE).trim().length(); double totalContribution = 0; double totalDenominator = 0; for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff < 0) { Double contributedSimilarity = (entry.getKey().length() / queryStringLength * 100) * (diff / averageIDFValue); totalContribution += Math.abs(contributedSimilarity); wordWeightedSimilarity.put(entry.getKey(), contributedSimilarity); } else { totalDenominator += diff; } } for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff > 0) { if (totalDenominator == 0) { throw new IllegalStateException("totalDenominator is 0"); } wordWeightedSimilarity.put( entry.getKey(), ((diff / totalDenominator) * totalContribution)); } } } return wordWeightedSimilarity; } }
InformationContentService { public Map<String, Double> redistributedNGramScore(String queryString, String ontologyIri) { Map<String, Double> wordIDFMap = createWordIDF(queryString, ontologyIri); Map<String, Double> wordWeightedSimilarity = new HashMap<>(); if (wordIDFMap.size() > 0) { double averageIDFValue = wordIDFMap.values().stream().mapToDouble(Double::doubleValue).average().getAsDouble(); double queryStringLength = StringUtils.join(createStemmedWordSet(queryString), SINGLE_WHITESPACE).trim().length(); double totalContribution = 0; double totalDenominator = 0; for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff < 0) { Double contributedSimilarity = (entry.getKey().length() / queryStringLength * 100) * (diff / averageIDFValue); totalContribution += Math.abs(contributedSimilarity); wordWeightedSimilarity.put(entry.getKey(), contributedSimilarity); } else { totalDenominator += diff; } } for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff > 0) { if (totalDenominator == 0) { throw new IllegalStateException("totalDenominator is 0"); } wordWeightedSimilarity.put( entry.getKey(), ((diff / totalDenominator) * totalContribution)); } } } return wordWeightedSimilarity; } InformationContentService(DataService dataService); }
InformationContentService { public Map<String, Double> redistributedNGramScore(String queryString, String ontologyIri) { Map<String, Double> wordIDFMap = createWordIDF(queryString, ontologyIri); Map<String, Double> wordWeightedSimilarity = new HashMap<>(); if (wordIDFMap.size() > 0) { double averageIDFValue = wordIDFMap.values().stream().mapToDouble(Double::doubleValue).average().getAsDouble(); double queryStringLength = StringUtils.join(createStemmedWordSet(queryString), SINGLE_WHITESPACE).trim().length(); double totalContribution = 0; double totalDenominator = 0; for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff < 0) { Double contributedSimilarity = (entry.getKey().length() / queryStringLength * 100) * (diff / averageIDFValue); totalContribution += Math.abs(contributedSimilarity); wordWeightedSimilarity.put(entry.getKey(), contributedSimilarity); } else { totalDenominator += diff; } } for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff > 0) { if (totalDenominator == 0) { throw new IllegalStateException("totalDenominator is 0"); } wordWeightedSimilarity.put( entry.getKey(), ((diff / totalDenominator) * totalContribution)); } } } return wordWeightedSimilarity; } InformationContentService(DataService dataService); Map<String, Double> redistributedNGramScore(String queryString, String ontologyIri); Set<String> createStemmedWordSet(String queryString); }
InformationContentService { public Map<String, Double> redistributedNGramScore(String queryString, String ontologyIri) { Map<String, Double> wordIDFMap = createWordIDF(queryString, ontologyIri); Map<String, Double> wordWeightedSimilarity = new HashMap<>(); if (wordIDFMap.size() > 0) { double averageIDFValue = wordIDFMap.values().stream().mapToDouble(Double::doubleValue).average().getAsDouble(); double queryStringLength = StringUtils.join(createStemmedWordSet(queryString), SINGLE_WHITESPACE).trim().length(); double totalContribution = 0; double totalDenominator = 0; for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff < 0) { Double contributedSimilarity = (entry.getKey().length() / queryStringLength * 100) * (diff / averageIDFValue); totalContribution += Math.abs(contributedSimilarity); wordWeightedSimilarity.put(entry.getKey(), contributedSimilarity); } else { totalDenominator += diff; } } for (Entry<String, Double> entry : wordIDFMap.entrySet()) { double diff = entry.getValue() - averageIDFValue; if (diff > 0) { if (totalDenominator == 0) { throw new IllegalStateException("totalDenominator is 0"); } wordWeightedSimilarity.put( entry.getKey(), ((diff / totalDenominator) * totalContribution)); } } } return wordWeightedSimilarity; } InformationContentService(DataService dataService); Map<String, Double> redistributedNGramScore(String queryString, String ontologyIri); Set<String> createStemmedWordSet(String queryString);  }