Search in sources :

Example 1 with ReconCandidate

use of com.google.refine.model.ReconCandidate in project OpenRefine by OpenRefine.

the class ReconJudgeOneCellCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        Project project = getProject(request);
        int rowIndex = Integer.parseInt(request.getParameter("row"));
        int cellIndex = Integer.parseInt(request.getParameter("cell"));
        Judgment judgment = Recon.stringToJudgment(request.getParameter("judgment"));
        ReconCandidate match = null;
        String id = request.getParameter("id");
        if (id != null) {
            String scoreString = request.getParameter("score");
            match = new ReconCandidate(id, request.getParameter("name"), request.getParameter("types").split(","), scoreString != null ? Double.parseDouble(scoreString) : 100);
        }
        JudgeOneCellProcess process = new JudgeOneCellProcess(project, "Judge one cell's recon result", judgment, rowIndex, cellIndex, match, request.getParameter("identifierSpace"), request.getParameter("schemaSpace"));
        HistoryEntry historyEntry = project.processManager.queueProcess(process);
        if (historyEntry != null) {
            /*
                 * If the process is done, write back the cell's data so that the
                 * client side can update its UI right away.
                 */
            JSONWriter writer = new JSONWriter(response.getWriter());
            Pool pool = new Pool();
            Properties options = new Properties();
            options.put("pool", pool);
            writer.object();
            writer.key("code");
            writer.value("ok");
            writer.key("historyEntry");
            historyEntry.write(writer, options);
            writer.key("cell");
            process.newCell.write(writer, options);
            writer.key("pool");
            pool.write(writer, options);
            writer.endObject();
        } else {
            respond(response, "{ \"code\" : \"pending\" }");
        }
    } catch (Exception e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) Project(com.google.refine.model.Project) HistoryEntry(com.google.refine.history.HistoryEntry) Pool(com.google.refine.util.Pool) Properties(java.util.Properties) ReconCandidate(com.google.refine.model.ReconCandidate) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Judgment(com.google.refine.model.Recon.Judgment)

Example 2 with ReconCandidate

use of com.google.refine.model.ReconCandidate in project OpenRefine by OpenRefine.

the class ReconMatchSpecificTopicCommand method createOperation.

@Override
protected AbstractOperation createOperation(Project project, HttpServletRequest request, JSONObject engineConfig) throws Exception {
    String columnName = request.getParameter("columnName");
    ReconCandidate match = new ReconCandidate(request.getParameter("topicID"), request.getParameter("topicName"), request.getParameter("types").split(","), 100);
    return new ReconMatchSpecificTopicOperation(engineConfig, columnName, match, request.getParameter("identifierSpace"), request.getParameter("schemaSpace"));
}
Also used : ReconMatchSpecificTopicOperation(com.google.refine.operations.recon.ReconMatchSpecificTopicOperation) ReconCandidate(com.google.refine.model.ReconCandidate)

Example 3 with ReconCandidate

use of com.google.refine.model.ReconCandidate in project OpenRefine by OpenRefine.

the class OdsImporter method extractCell.

protected static Cell extractCell(OdfTableCell cell, Map<String, Recon> reconMap) {
    Serializable value = extractCell(cell);
    if (value != null) {
        Recon recon = null;
        // TODO: cell.getHyperlink();
        String hyperlink = "";
        if (hyperlink != null) {
            // TODO: hyperlink.getAddress();
            String url = hyperlink;
            if (url.startsWith("http://") || url.startsWith("https://")) {
                final String sig = "freebase.com/view";
                int i = url.indexOf(sig);
                if (i > 0) {
                    String id = url.substring(i + sig.length());
                    int q = id.indexOf('?');
                    if (q > 0) {
                        id = id.substring(0, q);
                    }
                    int h = id.indexOf('#');
                    if (h > 0) {
                        id = id.substring(0, h);
                    }
                    if (reconMap.containsKey(id)) {
                        recon = reconMap.get(id);
                        recon.judgmentBatchSize++;
                    } else {
                        recon = new Recon(0, null, null);
                        recon.service = "import";
                        recon.match = new ReconCandidate(id, value.toString(), new String[0], 100);
                        recon.matchRank = 0;
                        recon.judgment = Judgment.Matched;
                        recon.judgmentAction = "auto";
                        recon.judgmentBatchSize = 1;
                        recon.addCandidate(recon.match);
                        reconMap.put(id, recon);
                    }
                }
            }
        }
        return new Cell(value, recon);
    } else {
        return null;
    }
}
Also used : Serializable(java.io.Serializable) Recon(com.google.refine.model.Recon) OdfTableCell(org.odftoolkit.odfdom.doc.table.OdfTableCell) Cell(com.google.refine.model.Cell) ReconCandidate(com.google.refine.model.ReconCandidate)

Example 4 with ReconCandidate

use of com.google.refine.model.ReconCandidate in project OpenRefine by OpenRefine.

the class ReconJudgeSimilarCellsOperation method reconstruct.

public static AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
    JSONObject engineConfig = obj.getJSONObject("engineConfig");
    ReconCandidate match = null;
    if (obj.has("match")) {
        JSONObject matchObj = obj.getJSONObject("match");
        JSONArray types = matchObj.getJSONArray("types");
        String[] typeIDs = new String[types.length()];
        for (int i = 0; i < typeIDs.length; i++) {
            typeIDs[i] = types.getString(i);
        }
        match = new ReconCandidate(matchObj.getString("id"), matchObj.getString("name"), typeIDs, matchObj.getDouble("score"));
    }
    Judgment judgment = Judgment.None;
    if (obj.has("judgment")) {
        judgment = Recon.stringToJudgment(obj.getString("judgment"));
    }
    return new ReconJudgeSimilarCellsOperation(engineConfig, obj.getString("columnName"), obj.getString("similarValue"), judgment, match, obj.has("shareNewTopics") ? obj.getBoolean("shareNewTopics") : false);
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ReconCandidate(com.google.refine.model.ReconCandidate) Judgment(com.google.refine.model.Recon.Judgment)

Example 5 with ReconCandidate

use of com.google.refine.model.ReconCandidate in project OpenRefine by OpenRefine.

the class StandardReconConfig method createReconServiceResults.

protected Recon createReconServiceResults(String text, JSONArray results, long historyEntryID) {
    Recon recon = new Recon(historyEntryID, identifierSpace, schemaSpace);
    try {
        int length = results.length();
        int count = 0;
        for (int i = 0; i < length; i++) {
            JSONObject result = results.getJSONObject(i);
            if (!result.has("name")) {
                continue;
            }
            JSONArray types = result.getJSONArray("type");
            String[] typeIDs = new String[types.length()];
            for (int j = 0; j < typeIDs.length; j++) {
                Object type = types.get(j);
                typeIDs[j] = type instanceof String ? (String) type : ((JSONObject) type).getString("id");
            }
            double score = result.getDouble("score");
            ReconCandidate candidate = new ReconCandidate(result.getString("id"), result.getString("name"), typeIDs, score);
            if (autoMatch && i == 0 && result.has("match") && result.getBoolean("match")) {
                recon.match = candidate;
                recon.matchRank = 0;
                recon.judgment = Judgment.Matched;
                recon.judgmentAction = "auto";
            }
            recon.addCandidate(candidate);
            count++;
        }
        if (count > 0) {
            ReconCandidate candidate = recon.candidates.get(0);
            recon.setFeature(Recon.Feature_nameMatch, text.equalsIgnoreCase(candidate.name));
            recon.setFeature(Recon.Feature_nameLevenshtein, StringUtils.getLevenshteinDistance(StringUtils.lowerCase(text), StringUtils.lowerCase(candidate.name)));
            recon.setFeature(Recon.Feature_nameWordDistance, wordDistance(text, candidate.name));
            recon.setFeature(Recon.Feature_typeMatch, false);
            if (this.typeID != null) {
                for (String typeID : candidate.types) {
                    if (this.typeID.equals(typeID)) {
                        recon.setFeature(Recon.Feature_typeMatch, true);
                        break;
                    }
                }
            }
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return recon;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) Recon(com.google.refine.model.Recon) ReconCandidate(com.google.refine.model.ReconCandidate)

Aggregations

ReconCandidate (com.google.refine.model.ReconCandidate)9 Recon (com.google.refine.model.Recon)4 Judgment (com.google.refine.model.Recon.Judgment)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 Cell (com.google.refine.model.Cell)2 Serializable (java.io.Serializable)2 HistoryEntry (com.google.refine.history.HistoryEntry)1 Project (com.google.refine.model.Project)1 ReconJudgeSimilarCellsOperation (com.google.refine.operations.recon.ReconJudgeSimilarCellsOperation)1 ReconMatchSpecificTopicOperation (com.google.refine.operations.recon.ReconMatchSpecificTopicOperation)1 Pool (com.google.refine.util.Pool)1 IOException (java.io.IOException)1 LineNumberReader (java.io.LineNumberReader)1 Properties (java.util.Properties)1 ServletException (javax.servlet.ServletException)1 Hyperlink (org.apache.poi.common.usermodel.Hyperlink)1 JSONException (org.json.JSONException)1 JSONWriter (org.json.JSONWriter)1 OdfTableCell (org.odftoolkit.odfdom.doc.table.OdfTableCell)1