Merge pull request #1920 from OpenRefine/issue1913

Reorder reconciliation results by decreasing score
This commit is contained in:
Antonin Delpeuch 2019-01-06 15:55:51 +01:00 committed by GitHub
commit e5da39fa4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 117 additions and 0 deletions

View File

@ -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<ReconResult> results = ParsingUtilities.mapper.convertValue(resultsList, new TypeReference<List<ReconResult>>() {});
// Sort results by decreasing score
results.sort(new Comparator<ReconResult>() {
@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++) {

View File

@ -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,105 @@ 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");
}
@Test
public void reorderReconciliationResultsStableSort() 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.3,\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.3,\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);
assertEquals(recon.candidates.get(0).score, 0.3);
assertEquals(recon.candidates.get(0).id, "18951129");
}
}