A bit more careful error handling during recon.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@105 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-19 01:17:35 +00:00
parent 28a86dfe0f
commit 5264c829ae

View File

@ -1,9 +1,7 @@
package com.metaweb.gridworks.model.operations; package com.metaweb.gridworks.model.operations;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.ArrayList; import java.util.ArrayList;
@ -203,11 +201,8 @@ public class ReconOperation extends EngineDependentOperation {
List<CellChange> cellChanges = new ArrayList<CellChange>(_entries.size()); List<CellChange> cellChanges = new ArrayList<CellChange>(_entries.size());
List<String> values = new ArrayList<String>(valueToEntries.keySet()); List<String> values = new ArrayList<String>(valueToEntries.keySet());
for (int i = 0; i < values.size(); i += 10) { for (int i = 0; i < values.size(); i += 10) {
try {
recon(valueToEntries, values, i, Math.min(i + 10, values.size()), cellChanges); recon(valueToEntries, values, i, Math.min(i + 10, values.size()), cellChanges);
} catch (JSONException e1) {
e1.printStackTrace();
}
_progress = i * 100 / values.size(); _progress = i * 100 / values.size();
try { try {
@ -240,8 +235,8 @@ public class ReconOperation extends EngineDependentOperation {
int from, int from,
int to, int to,
List<CellChange> cellChanges List<CellChange> cellChanges
) throws JSONException { ) {
try {
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
JSONWriter jsonWriter = new JSONWriter(stringWriter); JSONWriter jsonWriter = new JSONWriter(stringWriter);
@ -267,7 +262,6 @@ public class ReconOperation extends EngineDependentOperation {
sb.append("http://api.freebase.com/api/service/search?indent=1&queries="); sb.append("http://api.freebase.com/api/service/search?indent=1&queries=");
sb.append(ParsingUtilities.encode(stringWriter.toString())); sb.append(ParsingUtilities.encode(stringWriter.toString()));
try {
URL url = new URL(sb.toString()); URL url = new URL(sb.toString());
URLConnection connection = url.openConnection(); URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000); connection.setConnectTimeout(5000);
@ -285,14 +279,17 @@ public class ReconOperation extends EngineDependentOperation {
continue; continue;
} }
JSONObject o2 = o.getJSONObject(key); Recon recon;
if (!(o2.has("result"))) {
continue;
}
JSONObject o2 = o.getJSONObject(key);
if (o2.has("result")) {
JSONArray results = o2.getJSONArray("result"); JSONArray results = o2.getJSONArray("result");
Recon recon = createRecon(value, results); recon = createRecon(value, results);
} else {
recon = new Recon();
}
for (ReconEntry entry : valueToEntries.get(value)) { for (ReconEntry entry : valueToEntries.get(value)) {
Cell oldCell = entry.cell; Cell oldCell = entry.cell;
@ -306,20 +303,37 @@ public class ReconOperation extends EngineDependentOperation {
); );
cellChanges.add(cellChange); cellChanges.add(cellChange);
} }
valueToEntries.remove(value);
} }
} finally { } finally {
is.close(); is.close();
} }
} catch (UnsupportedEncodingException e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} }
protected Recon createRecon(String text, JSONArray results) throws JSONException { for (List<ReconEntry> entries : valueToEntries.values()) {
Recon recon = new Recon(); Recon recon = new Recon();
for (ReconEntry entry : entries) {
Cell oldCell = entry.cell;
Cell newCell = new Cell(oldCell.value, recon);
CellChange cellChange = new CellChange(
entry.rowIndex,
_cellIndex,
oldCell,
newCell
);
cellChanges.add(cellChange);
}
}
}
protected Recon createRecon(String text, JSONArray results) {
Recon recon = new Recon();
try {
int length = results.length(); int length = results.length();
for (int i = 0; i < length && recon.candidates.size() < 3; i++) { for (int i = 0; i < length && recon.candidates.size() < 3; i++) {
JSONObject result = results.getJSONObject(i); JSONObject result = results.getJSONObject(i);
@ -358,7 +372,9 @@ public class ReconOperation extends EngineDependentOperation {
recon.candidates.add(candidate); recon.candidates.add(candidate);
} }
} catch (JSONException e) {
e.printStackTrace();
}
return recon; return recon;
} }
} }