From de25ddfe41e1f723be18315877ff8157508b865a Mon Sep 17 00:00:00 2001 From: Tom Morris Date: Fri, 4 Feb 2011 02:08:38 +0000 Subject: [PATCH] Issue 328 - extend solution for key-based recon to guid & id recon git-svn-id: http://google-refine.googlecode.com/svn/trunk@1998 7d457c2a-affb-35e4-300a-418c747d4874 --- .../model/recon/GuidBasedReconConfig.java | 105 +++++++++--------- .../model/recon/IdBasedReconConfig.java | 73 ++++++------ .../model/recon/KeyBasedReconConfig.java | 8 +- .../model/recon/StrictReconConfig.java | 9 ++ 4 files changed, 105 insertions(+), 90 deletions(-) diff --git a/extensions/freebase/src/com/google/refine/freebase/model/recon/GuidBasedReconConfig.java b/extensions/freebase/src/com/google/refine/freebase/model/recon/GuidBasedReconConfig.java index 0d8c940c6..b707b474b 100644 --- a/extensions/freebase/src/com/google/refine/freebase/model/recon/GuidBasedReconConfig.java +++ b/extensions/freebase/src/com/google/refine/freebase/model/recon/GuidBasedReconConfig.java @@ -124,25 +124,25 @@ public class GuidBasedReconConfig extends StrictReconConfig { jsonWriter.object(); jsonWriter.key("query"); - jsonWriter.array(); - jsonWriter.object(); - - jsonWriter.key("id"); jsonWriter.value(null); - jsonWriter.key("name"); jsonWriter.value(null); - jsonWriter.key("guid"); jsonWriter.value(null); - jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray(); - - jsonWriter.key("guid|="); - jsonWriter.array(); - for (ReconJob job : jobs) { - jsonWriter.value(((GuidBasedReconJob) job).guid); - } - jsonWriter.endArray(); - - jsonWriter.endObject(); - jsonWriter.endArray(); + jsonWriter.array(); + jsonWriter.object(); + + jsonWriter.key("id"); jsonWriter.value(null); + jsonWriter.key("name"); jsonWriter.value(null); + jsonWriter.key("guid"); jsonWriter.value(null); + jsonWriter.key("type"); jsonWriter.array(); jsonWriter.endArray(); + + jsonWriter.key("guid|="); + jsonWriter.array(); + for (ReconJob job : jobs) { + jsonWriter.value(((GuidBasedReconJob) job).guid); + } + jsonWriter.endArray(); + jsonWriter.endObject(); - + jsonWriter.endArray(); + jsonWriter.endObject(); + query = stringWriter.toString(); } @@ -160,36 +160,38 @@ public class GuidBasedReconConfig extends StrictReconConfig { try { String s = ParsingUtilities.inputStreamToString(is); JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); - JSONArray results = o.getJSONArray("result"); - int count = results.length(); - - for (int i = 0; i < count; i++) { - JSONObject result = results.getJSONObject(i); - - String guid = result.getString("guid"); - - JSONArray types = result.getJSONArray("type"); - String[] typeIDs = new String[types.length()]; - for (int j = 0; j < typeIDs.length; j++) { - typeIDs[j] = types.getString(j); - } - - ReconCandidate candidate = new ReconCandidate( - result.getString("id"), - result.getString("name"), - typeIDs, - 100 - ); - - Recon recon = Recon.makeFreebaseRecon(historyEntryID); - recon.addCandidate(candidate); - recon.service = "mql"; - recon.judgment = Judgment.Matched; - recon.judgmentAction = "auto"; - recon.match = candidate; - recon.matchRank = 0; - - guidToRecon.put(guid, recon); + if (o.has("result")) { + JSONArray results = o.getJSONArray("result"); + int count = results.length(); + + for (int i = 0; i < count; i++) { + JSONObject result = results.getJSONObject(i); + + String guid = result.getString("guid"); + + JSONArray types = result.getJSONArray("type"); + String[] typeIDs = new String[types.length()]; + for (int j = 0; j < typeIDs.length; j++) { + typeIDs[j] = types.getString(j); + } + + ReconCandidate candidate = new ReconCandidate( + result.getString("id"), + result.getString("name"), + typeIDs, + 100 + ); + + Recon recon = Recon.makeFreebaseRecon(historyEntryID); + recon.addCandidate(candidate); + recon.service = "mql"; + recon.judgment = Judgment.Matched; + recon.judgmentAction = "auto"; + recon.match = candidate; + recon.matchRank = 0; + + guidToRecon.put(guid, recon); + } } } finally { is.close(); @@ -198,9 +200,12 @@ public class GuidBasedReconConfig extends StrictReconConfig { e.printStackTrace(); } - for (int i = 0; i < jobs.size(); i++) { - String guid = ((GuidBasedReconJob) jobs.get(i)).guid; + for (ReconJob job : jobs) { + String guid = ((GuidBasedReconJob) job).guid; Recon recon = guidToRecon.get(guid); + if (recon == null) { + recon = createNoMatchRecon(historyEntryID); + } recons.add(recon); } diff --git a/extensions/freebase/src/com/google/refine/freebase/model/recon/IdBasedReconConfig.java b/extensions/freebase/src/com/google/refine/freebase/model/recon/IdBasedReconConfig.java index e28edf716..75fea3e6a 100644 --- a/extensions/freebase/src/com/google/refine/freebase/model/recon/IdBasedReconConfig.java +++ b/extensions/freebase/src/com/google/refine/freebase/model/recon/IdBasedReconConfig.java @@ -164,47 +164,52 @@ public class IdBasedReconConfig extends StrictReconConfig { try { String s = ParsingUtilities.inputStreamToString(is); JSONObject o = ParsingUtilities.evaluateJsonStringToObject(s); - JSONArray results = o.getJSONArray("result"); - int count = results.length(); - - for (int i = 0; i < count; i++) { - JSONObject result = results.getJSONObject(i); - - String id = result.getString("id"); - - JSONArray types = result.getJSONArray("type"); - String[] typeIDs = new String[types.length()]; - for (int j = 0; j < typeIDs.length; j++) { - typeIDs[j] = types.getString(j); - } - - ReconCandidate candidate = new ReconCandidate( - id, - result.getString("name"), - typeIDs, - 100 - ); - - Recon recon = Recon.makeFreebaseRecon(historyEntryID); - recon.addCandidate(candidate); - recon.service = "mql"; - recon.judgment = Judgment.Matched; - recon.judgmentAction = "auto"; - recon.match = candidate; - recon.matchRank = 0; - - idToRecon.put(id, recon); + if (o.has("result")) { + JSONArray results = o.getJSONArray("result"); + int count = results.length(); + + for (int i = 0; i < count; i++) { + JSONObject result = results.getJSONObject(i); + + String id = result.getString("id"); + + JSONArray types = result.getJSONArray("type"); + String[] typeIDs = new String[types.length()]; + for (int j = 0; j < typeIDs.length; j++) { + typeIDs[j] = types.getString(j); + } + + ReconCandidate candidate = new ReconCandidate( + id, + result.getString("name"), + typeIDs, + 100 + ); + + Recon recon = Recon.makeFreebaseRecon(historyEntryID); + recon.addCandidate(candidate); + recon.service = "mql"; + recon.judgment = Judgment.Matched; + recon.judgmentAction = "auto"; + recon.match = candidate; + recon.matchRank = 0; + + idToRecon.put(id, recon); + } } } finally { - is.close(); + is.close(); } } catch (Exception e) { - e.printStackTrace(); + e.printStackTrace(); } - for (int i = 0; i < jobs.size(); i++) { - String id = ((IdBasedReconJob) jobs.get(i)).id; + for (ReconJob job : jobs) { + String id = ((IdBasedReconJob) job).id; Recon recon = idToRecon.get(id); + if (recon == null) { + recon = createNoMatchRecon(historyEntryID); + } recons.add(recon); } diff --git a/extensions/freebase/src/com/google/refine/freebase/model/recon/KeyBasedReconConfig.java b/extensions/freebase/src/com/google/refine/freebase/model/recon/KeyBasedReconConfig.java index 0a4847776..3daccad9f 100644 --- a/extensions/freebase/src/com/google/refine/freebase/model/recon/KeyBasedReconConfig.java +++ b/extensions/freebase/src/com/google/refine/freebase/model/recon/KeyBasedReconConfig.java @@ -221,12 +221,8 @@ public class KeyBasedReconConfig extends StrictReconConfig { for (ReconJob job : jobs) { String key = ((KeyBasedReconJob) job).key; Recon recon = keyToRecon.get(key); - if (recon == null) { // add a no-match recon if none - recon = Recon.makeFreebaseRecon(historyEntryID); - recon.service = "mql"; - recon.judgment = Judgment.None; - recon.matchRank = -1; - keyToRecon.put(key, recon); + if (recon == null) { + recon = createNoMatchRecon(historyEntryID); } recons.add(recon); } diff --git a/extensions/freebase/src/com/google/refine/freebase/model/recon/StrictReconConfig.java b/extensions/freebase/src/com/google/refine/freebase/model/recon/StrictReconConfig.java index 34f193e05..33370583e 100644 --- a/extensions/freebase/src/com/google/refine/freebase/model/recon/StrictReconConfig.java +++ b/extensions/freebase/src/com/google/refine/freebase/model/recon/StrictReconConfig.java @@ -36,6 +36,7 @@ package com.google.refine.freebase.model.recon; import org.json.JSONObject; import com.google.refine.model.Recon; +import com.google.refine.model.Recon.Judgment; import com.google.refine.model.recon.ReconConfig; abstract public class StrictReconConfig extends ReconConfig { @@ -57,4 +58,12 @@ abstract public class StrictReconConfig extends ReconConfig { public Recon createNewRecon(long historyEntryID) { return Recon.makeFreebaseRecon(historyEntryID); } + + protected Recon createNoMatchRecon(long historyEntryID) { + Recon recon = createNewRecon(historyEntryID); + recon.service = "mql"; + recon.judgment = Judgment.None; + recon.matchRank = -1; + return recon; + } }