From b03dbf2446ef40b071065e6e4c85fdd5c55a1159 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Sun, 30 Dec 2018 20:47:12 +0100 Subject: [PATCH] Reorder reconciliation results by decreasing score. Fixes #1913. --- .../model/recon/StandardReconConfig.java | 9 +++ .../model/recon/StandardReconConfigTests.java | 58 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/main/src/com/google/refine/model/recon/StandardReconConfig.java b/main/src/com/google/refine/model/recon/StandardReconConfig.java index 34981f0a5..f6a387b56 100644 --- a/main/src/com/google/refine/model/recon/StandardReconConfig.java +++ b/main/src/com/google/refine/model/recon/StandardReconConfig.java @@ -41,6 +41,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -525,6 +526,14 @@ public class StandardReconConfig extends ReconConfig { Recon recon = new Recon(historyEntryID, identifierSpace, schemaSpace); List results = ParsingUtilities.mapper.convertValue(resultsList, new TypeReference>() {}); + // Sort results by decreasing score + results.sort(new Comparator() { + @Override + public int compare(ReconResult a, ReconResult b) { + return Double.compare(b.score, a.score); + } + }); + int length = results.size(); int count = 0; for (int i = 0; i < length; i++) { diff --git a/main/tests/server/src/com/google/refine/tests/model/recon/StandardReconConfigTests.java b/main/tests/server/src/com/google/refine/tests/model/recon/StandardReconConfigTests.java index 3e2a802bb..6ce9a4273 100644 --- a/main/tests/server/src/com/google/refine/tests/model/recon/StandardReconConfigTests.java +++ b/main/tests/server/src/com/google/refine/tests/model/recon/StandardReconConfigTests.java @@ -28,6 +28,7 @@ package com.google.refine.tests.model.recon; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNull; +import static org.testng.Assert.assertTrue; import java.io.IOException; import java.util.ArrayList; @@ -40,7 +41,9 @@ import org.testng.annotations.Test; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.google.refine.model.Project; +import com.google.refine.model.Recon; import com.google.refine.model.Row; import com.google.refine.model.recon.ReconConfig; import com.google.refine.model.recon.ReconJob; @@ -76,6 +79,10 @@ public class StandardReconConfigTests extends RefineTest { public double wordDistanceTest(String s1, String s2) { return wordDistance(s1, s2); } + + protected Recon createReconServiceResults(String text, ArrayNode resultsList, long historyEntryID) throws IOException { + return super.createReconServiceResults(text, resultsList, historyEntryID); + } } @Test @@ -212,4 +219,55 @@ public class StandardReconConfigTests extends RefineTest { ReconResult rr = ParsingUtilities.mapper.readValue(json, ReconResult.class); assertEquals(rr.types.get(0).name, "hamlet in Alberta"); } + + // Issue #1913 + @Test + public void reorderReconciliationResults() throws JsonParseException, JsonMappingException, IOException { + String viafJson = " [\n" + + "\n" + + " {\n" + + " \"id\": \"18951129\",\n" + + " \"name\": \"Varano, Camilla Battista da 1458-1524\",\n" + + " \"type\": [\n" + + " {\n" + + " \"id\": \"/people/person\",\n" + + " \"name\": \"Person\"\n" + + " }\n" + + " ],\n" + + " \"score\": 0.1282051282051282,\n" + + " \"match\": false\n" + + " },\n" + + " {\n" + + " \"id\": \"102271932\",\n" + + " \"name\": \"Shamsie, Kamila, 1973-....\",\n" + + " \"type\": [\n" + + " {\n" + + " \"id\": \"/people/person\",\n" + + " \"name\": \"Person\"\n" + + " }\n" + + " ],\n" + + " \"score\": 0.23076923076923078,\n" + + " \"match\": false\n" + + " },\n" + + " {\n" + + " \"id\": \"63233597\",\n" + + " \"name\": \"Camilla, Duchess of Cornwall, 1947-\",\n" + + " \"type\": [\n" + + " {\n" + + " \"id\": \"/people/person\",\n" + + " \"name\": \"Person\"\n" + + " }\n" + + " ],\n" + + " \"score\": 0.14285714285714285,\n" + + " \"match\": false\n" + + " }\n" + + "\n" + + "]"; + + StandardReconConfigStub stub = new StandardReconConfigStub(); + ArrayNode node = ParsingUtilities.mapper.readValue(viafJson, ArrayNode.class); + Recon recon = stub.createReconServiceResults("Kamila", node, 1234L); + assertTrue(recon.candidates.get(0).score > 0.2); + assertEquals(recon.candidates.get(0).id, "102271932"); + } }