Search in sources :

Example 1 with CellChange

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

use of com.google.refine.model.changes.CellChange 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 3 with CellChange

use of com.google.refine.model.changes.CellChange in project OpenRefine by OpenRefine.

the class FillDownOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(Project project, List<CellChange> cellChanges, long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    return new RowVisitor() {

        int cellIndex;

        List<CellChange> cellChanges;

        Cell previousCell;

        public RowVisitor init(int cellIndex, List<CellChange> cellChanges) {
            this.cellIndex = cellIndex;
            this.cellChanges = cellChanges;
            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) {
            Object value = row.getCellValue(cellIndex);
            if (ExpressionUtils.isNonBlankData(value)) {
                previousCell = row.getCell(cellIndex);
            } else if (previousCell != null) {
                CellChange cellChange = new CellChange(rowIndex, cellIndex, row.getCell(cellIndex), previousCell);
                cellChanges.add(cellChange);
            }
            return false;
        }
    }.init(column.getCellIndex(), cellChanges);
}
Also used : Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) CellChange(com.google.refine.model.changes.CellChange) 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 CellChange

use of com.google.refine.model.changes.CellChange in project OpenRefine by OpenRefine.

the class ReconClearSimilarCellsOperation method createRowVisitor.

@Override
protected RowVisitor createRowVisitor(final Project project, final List<CellChange> cellChanges, final long historyEntryID) throws Exception {
    Column column = project.columnModel.getColumnByName(_columnName);
    final int cellIndex = column != null ? column.getCellIndex() : -1;
    return new RowVisitor() {

        @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 = cellIndex < 0 ? null : row.getCell(cellIndex);
            if (cell != null && cell.recon != null) {
                String value = cell.value instanceof String ? ((String) cell.value) : cell.value.toString();
                if (_similarValue.equals(value)) {
                    Cell newCell = new Cell(cell.value, null);
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                    cellChanges.add(cellChange);
                }
            }
            return false;
        }
    };
}
Also used : Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) CellChange(com.google.refine.model.changes.CellChange) Row(com.google.refine.model.Row) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell)

Example 5 with CellChange

use of com.google.refine.model.changes.CellChange in project OpenRefine by OpenRefine.

the class ReconCopyAcrossColumnsOperation method createHistoryEntry.

@Override
protected HistoryEntry createHistoryEntry(final Project project, final long historyEntryID) throws Exception {
    Engine engine = createEngine(project);
    final Column fromColumn = project.columnModel.getColumnByName(_fromColumnName);
    final List<Column> toColumns = new ArrayList<Column>(_toColumnNames.length);
    for (String c : _toColumnNames) {
        Column toColumn = project.columnModel.getColumnByName(c);
        if (toColumn != null) {
            toColumns.add(toColumn);
        }
    }
    final Set<Recon.Judgment> judgments = new HashSet<Recon.Judgment>(_judgments.length);
    for (String j : _judgments) {
        judgments.add(Recon.stringToJudgment(j));
    }
    final List<CellChange> cellChanges = new ArrayList<CellChange>(project.rows.size());
    if (fromColumn != null && toColumns.size() > 0) {
        final Map<Object, Recon> cellValueToRecon = new HashMap<Object, Recon>();
        FilteredRows filteredRows = engine.getAllFilteredRows();
        try {
            filteredRows.accept(project, new RowVisitor() {

                @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(fromColumn.getCellIndex());
                    if (cell != null && cell.value != null && cell.recon != null) {
                        if (judgments.contains(cell.recon.judgment)) {
                            cellValueToRecon.put(cell.value, cell.recon);
                        }
                    }
                    return false;
                }
            });
            filteredRows.accept(project, new RowVisitor() {

                @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) {
                    for (Column column : toColumns) {
                        int cellIndex = column.getCellIndex();
                        Cell cell = row.getCell(cellIndex);
                        if (cell != null && cell.value != null) {
                            Recon reconToCopy = cellValueToRecon.get(cell.value);
                            boolean judged = cell.recon != null && cell.recon.judgment != Judgment.None;
                            if (reconToCopy != null && (!judged || _applyToJudgedCells)) {
                                Cell newCell = new Cell(cell.value, reconToCopy);
                                CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                                cellChanges.add(cellChange);
                            }
                        }
                    }
                    return false;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String description = "Copy " + cellChanges.size() + " recon judgments from column " + _fromColumnName + " to " + StringUtils.join(_toColumnNames);
    return new HistoryEntry(historyEntryID, project, description, this, new MassChange(cellChanges, false));
}
Also used : HashMap(java.util.HashMap) CellChange(com.google.refine.model.changes.CellChange) ArrayList(java.util.ArrayList) FilteredRows(com.google.refine.browsing.FilteredRows) JSONException(org.json.JSONException) MassChange(com.google.refine.model.changes.MassChange) Project(com.google.refine.model.Project) Column(com.google.refine.model.Column) HistoryEntry(com.google.refine.history.HistoryEntry) JSONObject(org.json.JSONObject) Row(com.google.refine.model.Row) Recon(com.google.refine.model.Recon) RowVisitor(com.google.refine.browsing.RowVisitor) Cell(com.google.refine.model.Cell) Engine(com.google.refine.browsing.Engine) HashSet(java.util.HashSet) Judgment(com.google.refine.model.Recon.Judgment)

Aggregations

Column (com.google.refine.model.Column)7 CellChange (com.google.refine.model.changes.CellChange)7 RowVisitor (com.google.refine.browsing.RowVisitor)6 Cell (com.google.refine.model.Cell)6 Project (com.google.refine.model.Project)6 Row (com.google.refine.model.Row)6 JSONObject (org.json.JSONObject)4 HashMap (java.util.HashMap)3 Engine (com.google.refine.browsing.Engine)2 FilteredRows (com.google.refine.browsing.FilteredRows)2 Evaluable (com.google.refine.expr.Evaluable)2 HistoryEntry (com.google.refine.history.HistoryEntry)2 Recon (com.google.refine.model.Recon)2 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 WrappedCell (com.google.refine.expr.WrappedCell)1 Judgment (com.google.refine.model.Recon.Judgment)1 MassCellChange (com.google.refine.model.changes.MassCellChange)1 MassChange (com.google.refine.model.changes.MassChange)1