use of org.apache.commons.text.similarity.LevenshteinDistance in project drools by kiegroup.
the class KiePMMLTextIndexTest method evaluateLevenshteinDistanceBestHits.
@Test
public void evaluateLevenshteinDistanceBestHits() {
// brown-foxy does not match
String wordSeparatorCharacterRE = "\\s+";
Pattern pattern = Pattern.compile(wordSeparatorCharacterRE);
List<String> terms = KiePMMLTextIndex.splitText("The", pattern);
List<String> texts = KiePMMLTextIndex.splitText(TEXT_0, pattern);
LevenshteinDistance levenshteinDistance = new LevenshteinDistance(0);
assertEquals(2, KiePMMLTextIndex.evaluateLevenshteinDistanceBestHits(levenshteinDistance, terms, texts));
levenshteinDistance = new LevenshteinDistance(1);
assertEquals(2, KiePMMLTextIndex.evaluateLevenshteinDistanceBestHits(levenshteinDistance, terms, texts));
levenshteinDistance = new LevenshteinDistance(2);
assertEquals(2, KiePMMLTextIndex.evaluateLevenshteinDistanceBestHits(levenshteinDistance, terms, texts));
// ---
// brown-foxy match
wordSeparatorCharacterRE = "[\\s\\-]";
pattern = Pattern.compile(wordSeparatorCharacterRE);
terms = KiePMMLTextIndex.splitText("The", pattern);
texts = KiePMMLTextIndex.splitText(TEXT_0, pattern);
levenshteinDistance = new LevenshteinDistance(0);
assertEquals(2, KiePMMLTextIndex.evaluateLevenshteinDistanceBestHits(levenshteinDistance, terms, texts));
levenshteinDistance = new LevenshteinDistance(1);
assertEquals(2, KiePMMLTextIndex.evaluateLevenshteinDistanceBestHits(levenshteinDistance, terms, texts));
levenshteinDistance = new LevenshteinDistance(2);
assertEquals(2, KiePMMLTextIndex.evaluateLevenshteinDistanceBestHits(levenshteinDistance, terms, texts));
}
use of org.apache.commons.text.similarity.LevenshteinDistance in project drools by kiegroup.
the class KiePMMLTextIndexTest method evaluateLevenshteinDistanceAllHits.
@Test
public void evaluateLevenshteinDistanceAllHits() {
// brown-foxy does not match
String wordSeparatorCharacterRE = "\\s+";
Pattern pattern = Pattern.compile(wordSeparatorCharacterRE);
List<String> terms = KiePMMLTextIndex.splitText(TERM_0, pattern);
List<String> texts = KiePMMLTextIndex.splitText(TEXT_0, pattern);
LevenshteinDistance levenshteinDistance = new LevenshteinDistance(0);
assertEquals(1, KiePMMLTextIndex.evaluateLevenshteinDistanceAllHits(levenshteinDistance, terms, texts));
levenshteinDistance = new LevenshteinDistance(1);
assertEquals(2, KiePMMLTextIndex.evaluateLevenshteinDistanceAllHits(levenshteinDistance, terms, texts));
levenshteinDistance = new LevenshteinDistance(2);
assertEquals(3, KiePMMLTextIndex.evaluateLevenshteinDistanceAllHits(levenshteinDistance, terms, texts));
// ---
// brown-foxy match
wordSeparatorCharacterRE = "[\\s\\-]";
pattern = Pattern.compile(wordSeparatorCharacterRE);
terms = KiePMMLTextIndex.splitText(TERM_0, pattern);
texts = KiePMMLTextIndex.splitText(TEXT_0, pattern);
levenshteinDistance = new LevenshteinDistance(0);
assertEquals(1, KiePMMLTextIndex.evaluateLevenshteinDistanceAllHits(levenshteinDistance, terms, texts));
levenshteinDistance = new LevenshteinDistance(1);
assertEquals(3, KiePMMLTextIndex.evaluateLevenshteinDistanceAllHits(levenshteinDistance, terms, texts));
levenshteinDistance = new LevenshteinDistance(2);
assertEquals(4, KiePMMLTextIndex.evaluateLevenshteinDistanceAllHits(levenshteinDistance, terms, texts));
}
use of org.apache.commons.text.similarity.LevenshteinDistance in project goci by EBISPOT.
the class PublicationController method matchPublication.
@RequestMapping(value = "/match", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<Map<String, Object>> matchPublication(Model model, @RequestBody String pubmedId) {
Map<String, Object> results = new HashMap<>();
CosineDistance cosScore = new CosineDistance();
LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
JaroWinklerSimilarity jwDistance = new JaroWinklerSimilarity();
EuropePMCData europePMCResult = europepmcPubMedSearchService.createStudyByPubmed(pubmedId);
Map<String, String> searchProps = new HashMap<>();
List<Map<String, String>> data = new ArrayList<>();
if (!europePMCResult.getError()) {
try {
searchProps.put("pubMedID", europePMCResult.getPublication().getPubmedId());
searchProps.put("author", europePMCResult.getFirstAuthor().getFullname());
searchProps.put("title", europePMCResult.getPublication().getTitle());
searchProps.put("doi", europePMCResult.getDoi());
results.put("search", searchProps);
String searchTitle = europePMCResult.getPublication().getTitle();
String searchAuthor = europePMCResult.getFirstAuthor().getFullname();
CharSequence searchString = buildSearch(searchAuthor, searchTitle);
Map<String, Submission> submissionMap = submissionService.getSubmissionsBasic();
for (Map.Entry<String, Submission> e : submissionMap.entrySet()) {
Map<String, String> props = new HashMap<>();
Submission submission = e.getValue();
String matchTitle = submission.getTitle();
String matchAuthor = submission.getAuthor();
CharSequence matchString = buildSearch(matchAuthor, matchTitle);
props.put("submissionID", submission.getId());
props.put("pubMedID", submission.getPubMedID());
props.put("author", submission.getAuthor());
props.put("title", submission.getTitle());
props.put("doi", submission.getDoi());
if (matchString.equals("")) {
props.put("cosScore", new Integer(0).toString());
props.put("levDistance", new Integer(0).toString());
props.put("jwScore", new Integer(0).toString());
} else {
Double score = cosScore.apply(searchString, matchString) * 100;
Integer ldScore = levenshteinDistance.apply(searchString, matchString);
Double jwScore = jwDistance.apply(searchString, matchString) * 100;
props.put("cosScore", normalizeScore(score.intValue()).toString());
props.put("levDistance", normalizeScore(ldScore).toString());
props.put("jwScore", new Integer(jwScore.intValue()).toString());
}
data.add(props);
}
data.sort((o1, o2) -> Integer.decode(o2.get("cosScore")).compareTo(Integer.decode(o1.get("cosScore"))));
} catch (IOException e) {
e.printStackTrace();
}
} else {
results.put("error", "ID " + pubmedId + " not found");
}
results.put("data", data);
model.addAttribute("baseUrl", depositionUiURL);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.add("Content-Type", "application/json; charset=utf-8");
return new ResponseEntity<>(results, responseHeaders, HttpStatus.OK);
}
Aggregations