Search in sources :

Example 1 with Cell

use of com.google.refine.model.Cell 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 2 with Cell

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

the class CellAtRow method load.

public static CellAtRow load(String s, Pool pool) throws Exception {
    int semicolon = s.indexOf(';');
    int row = Integer.parseInt(s.substring(0, semicolon));
    Cell cell = semicolon < s.length() - 1 ? Cell.loadStreaming(s.substring(semicolon + 1), pool) : null;
    return new CellAtRow(row, cell);
}
Also used : Cell(com.google.refine.model.Cell)

Example 3 with Cell

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

the class MassEditOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    Evaluable eval = MetaParser.parse(_expression);
    Properties bindings = ExpressionUtils.createBindings(project);
    Map<String, Serializable> fromTo = new HashMap<String, Serializable>();
    Serializable fromBlankTo = null;
    Serializable fromErrorTo = null;
    for (Edit edit : _edits) {
        for (String s : edit.from) {
            fromTo.put(s, edit.to);
        }
        // the last edit wins
        if (edit.fromBlank) {
            fromBlankTo = edit.to;
        }
        if (edit.fromError) {
            fromErrorTo = edit.to;
        }
    }
    return new RowVisitor() {

        int cellIndex;

        Properties bindings;

        List<CellChange> cellChanges;

        Evaluable eval;

        Map<String, Serializable> fromTo;

        Serializable fromBlankTo;

        Serializable fromErrorTo;

        public RowVisitor init(int cellIndex, Properties bindings, List<CellChange> cellChanges, Evaluable eval, Map<String, Serializable> fromTo, Serializable fromBlankTo, Serializable fromErrorTo) {
            this.cellIndex = cellIndex;
            this.bindings = bindings;
            this.cellChanges = cellChanges;
            this.eval = eval;
            this.fromTo = fromTo;
            this.fromBlankTo = fromBlankTo;
            this.fromErrorTo = fromErrorTo;
            return this;
        }

        @Override
        public void start(Project project) {
        // nothing to do
        }

        @Override
        public void end(Project project) {
        // nothing to do
        }

        @Override
        public boolean visit(Project project, int rowIndex, Row row) {
            Cell cell = row.getCell(cellIndex);
            Cell newCell = null;
            ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
            Object v = eval.evaluate(bindings);
            if (ExpressionUtils.isError(v)) {
                if (fromErrorTo != null) {
                    newCell = new Cell(fromErrorTo, (cell != null) ? cell.recon : null);
                }
            } else if (ExpressionUtils.isNonBlankData(v)) {
                String from = v.toString();
                Serializable to = fromTo.get(from);
                if (to != null) {
                    newCell = new Cell(to, (cell != null) ? cell.recon : null);
                }
            } else {
                if (fromBlankTo != null) {
                    newCell = new Cell(fromBlankTo, (cell != null) ? cell.recon : null);
                }
            }
            if (newCell != null) {
                CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                cellChanges.add(cellChange);
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellChanges, eval, fromTo, fromBlankTo, fromErrorTo);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) CellChange(com.google.refine.model.changes.CellChange) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell)

Example 4 with Cell

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

the class TextTransformOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    Evaluable eval = MetaParser.parse(_expression);
    Properties bindings = ExpressionUtils.createBindings(project);
    return new RowVisitor() {

        int cellIndex;

        Properties bindings;

        List<CellChange> cellChanges;

        Evaluable eval;

        public RowVisitor init(int cellIndex, Properties bindings, List<CellChange> cellChanges, Evaluable eval) {
            this.cellIndex = cellIndex;
            this.bindings = bindings;
            this.cellChanges = cellChanges;
            this.eval = eval;
            return this;
        }

        @Override
        public void start(Project project) {
        // nothing to do
        }

        @Override
        public void end(Project project) {
        // nothing to do
        }

        @Override
        public boolean visit(Project project, int rowIndex, Row row) {
            Cell cell = row.getCell(cellIndex);
            Cell newCell = null;
            Object oldValue = cell != null ? cell.value : null;
            ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
            Object o = eval.evaluate(bindings);
            if (o == null) {
                if (oldValue != null) {
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, null);
                    cellChanges.add(cellChange);
                }
            } else {
                if (o instanceof Cell) {
                    newCell = (Cell) o;
                } else if (o instanceof WrappedCell) {
                    newCell = ((WrappedCell) o).cell;
                } else {
                    Serializable newValue = ExpressionUtils.wrapStorable(o);
                    if (ExpressionUtils.isError(newValue)) {
                        if (_onError == OnError.KeepOriginal) {
                            return false;
                        } else if (_onError == OnError.SetToBlank) {
                            newValue = null;
                        }
                    }
                    if (!ExpressionUtils.sameValue(oldValue, newValue)) {
                        newCell = new Cell(newValue, (cell != null) ? cell.recon : null);
                        if (_repeat) {
                            for (int i = 0; i < _repeatCount; i++) {
                                ExpressionUtils.bind(bindings, row, rowIndex, _columnName, newCell);
                                newValue = ExpressionUtils.wrapStorable(eval.evaluate(bindings));
                                if (ExpressionUtils.isError(newValue)) {
                                    break;
                                } else if (ExpressionUtils.sameValue(newCell.value, newValue)) {
                                    break;
                                }
                                newCell = new Cell(newValue, newCell.recon);
                            }
                        }
                    }
                }
                if (newCell != null) {
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                    cellChanges.add(cellChange);
                }
            }
            return false;
        }
    }.init(column.getCellIndex(), bindings, cellChanges, eval);
}
Also used : Serializable(java.io.Serializable) CellChange(com.google.refine.model.changes.CellChange) Properties(java.util.Properties) Evaluable(com.google.refine.expr.Evaluable) Project(com.google.refine.model.Project) WrappedCell(com.google.refine.expr.WrappedCell) Column(com.google.refine.model.Column) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) WrappedCell(com.google.refine.expr.WrappedCell)

Example 5 with Cell

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

the class TransposeRowsIntoColumnsOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
    List<Column> newColumns = new ArrayList<Column>();
    List<Column> oldColumns = project.columnModel.columns;
    int columnIndex = project.columnModel.getColumnIndexByName(_columnName);
    int columnCount = oldColumns.size();
    for (int i = 0; i < columnCount; i++) {
        Column column = oldColumns.get(i);
        if (i == columnIndex) {
            int newIndex = 1;
            for (int n = 0; n < _rowCount; n++) {
                String columnName = _columnName + " " + newIndex++;
                while (project.columnModel.getColumnByName(columnName) != null) {
                    columnName = _columnName + " " + newIndex++;
                }
                newColumns.add(new Column(i + n, columnName));
            }
        } else if (i < columnIndex) {
            newColumns.add(new Column(i, column.getName()));
        } else {
            newColumns.add(new Column(i + _rowCount - 1, column.getName()));
        }
    }
    List<Row> oldRows = project.rows;
    List<Row> newRows = new ArrayList<Row>(oldRows.size() / _rowCount);
    for (int r = 0; r < oldRows.size(); r += _rowCount) {
        Row firstNewRow = new Row(newColumns.size());
        for (int r2 = 0; r2 < _rowCount && r + r2 < oldRows.size(); r2++) {
            Row oldRow = oldRows.get(r + r2);
            Row newRow = r2 == 0 ? firstNewRow : new Row(newColumns.size());
            boolean hasData = r2 == 0;
            for (int c = 0; c < oldColumns.size(); c++) {
                Column column = oldColumns.get(c);
                Cell cell = oldRow.getCell(column.getCellIndex());
                if (cell != null && cell.value != null) {
                    if (c == columnIndex) {
                        firstNewRow.setCell(columnIndex + r2, cell);
                    } else if (c < columnIndex) {
                        newRow.setCell(c, cell);
                        hasData = true;
                    } else {
                        newRow.setCell(c + _rowCount - 1, cell);
                        hasData = true;
                    }
                }
            }
            if (hasData) {
                newRows.add(newRow);
            }
        }
    }
    return new HistoryEntry(historyEntryID, project, getBriefDescription(null), this, new MassRowColumnChange(newColumns, newRows));
}
Also used : Column(com.google.refine.model.Column) MassRowColumnChange(com.google.refine.model.changes.MassRowColumnChange) ArrayList(java.util.ArrayList) HistoryEntry(com.google.refine.history.HistoryEntry) Row(com.google.refine.model.Row) Cell(com.google.refine.model.Cell)

Aggregations

Cell (com.google.refine.model.Cell)58 Row (com.google.refine.model.Row)36 Column (com.google.refine.model.Column)19 Test (org.testng.annotations.Test)16 RefineTest (com.google.refine.tests.RefineTest)15 BeforeTest (org.testng.annotations.BeforeTest)15 JSONObject (org.json.JSONObject)13 ArrayList (java.util.ArrayList)12 Project (com.google.refine.model.Project)11 IOException (java.io.IOException)11 Properties (java.util.Properties)11 JSONException (org.json.JSONException)9 RowVisitor (com.google.refine.browsing.RowVisitor)7 HistoryEntry (com.google.refine.history.HistoryEntry)7 Serializable (java.io.Serializable)7 Recon (com.google.refine.model.Recon)6 CellChange (com.google.refine.model.changes.CellChange)6 HashMap (java.util.HashMap)6 Evaluable (com.google.refine.expr.Evaluable)5 JSONArray (org.json.JSONArray)4