Package com.google.refine.model

Examples of com.google.refine.model.Column


        return "Remove column " + _columnName;
    }

    @Override
    protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
        Column column = project.columnModel.getColumnByName(_columnName);
        if (column == null) {
            throw new Exception("No column named " + _columnName);
        }
       
        String description = "Remove column " + column.getName();
       
        Change change = new ColumnRemovalChange(project.columnModel.columns.indexOf(column));
       
        return new HistoryEntry(historyEntryID, project, description, ColumnRemovalOperation.this, change);
    }
View Full Code Here


        throw new InternalError("Can't get here");
    }

    @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;
            Recon               _sharedNewRecon = null;
            Map<Long, Recon>    _dupReconMap = new HashMap<Long, Recon>();
            long                _historyEntryID;
           
            public RowVisitor init(int cellIndex, List<CellChange> cellChanges, long historyEntryID) {
                _cellIndex = cellIndex;
                _cellChanges = cellChanges;
                _historyEntryID = historyEntryID;
                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);
                if (cell != null && ExpressionUtils.isNonBlankData(cell.value)) {
                    String value = cell.value instanceof String ?
                            ((String) cell.value) : cell.value.toString();
                           
                    if (_similarValue.equals(value)) {
                        Recon recon = null;
                        if (_judgment == Judgment.New && _shareNewTopics) {
                            if (_sharedNewRecon == null) {
                                _sharedNewRecon = new Recon(_historyEntryID, null, null);
                                _sharedNewRecon.judgment = Judgment.New;
                                _sharedNewRecon.judgmentBatchSize = 0;
                                _sharedNewRecon.judgmentAction = "similar";
                            }
                            _sharedNewRecon.judgmentBatchSize++;
                           
                            recon = _sharedNewRecon;
                        } else {
                            if (_dupReconMap.containsKey(cell.recon.id)) {
                                recon = _dupReconMap.get(cell.recon.id);
                                recon.judgmentBatchSize++;
                            } else {
                                recon = cell.recon.dup(_historyEntryID);
                                recon.judgmentBatchSize = 1;
                                recon.matchRank = -1;
                                recon.judgmentAction = "similar";
                               
                                if (_judgment == Judgment.Matched) {
                                    recon.judgment = Recon.Judgment.Matched;
                                    recon.match = _match;
                                   
                                    if (recon.candidates != null) {
                                        for (int m = 0; m < recon.candidates.size(); m++) {
                                            if (recon.candidates.get(m).id.equals(_match.id)) {
                                                recon.matchRank = m;
                                                break;
                                            }
                                        }
                                    }
                                } else if (_judgment == Judgment.New) {
                                    recon.judgment = Recon.Judgment.New;
                                    recon.match = null;
                                } else if (_judgment == Judgment.None) {
                                    recon.judgment = Recon.Judgment.None;
                                    recon.match = null;
                                }
                               
                                _dupReconMap.put(cell.recon.id, recon);
                            }
                        }
                       
                        Cell newCell = new Cell(cell.value, recon);
                       
                        CellChange cellChange = new CellChange(rowIndex, _cellIndex, cell, newCell);
                        _cellChanges.add(cellChange);
                    }
                }
                return false;
            }
        }.init(column.getCellIndex(), cellChanges, historyEntryID);
    }
View Full Code Here

            " cells in column " + column.getName() + ": " + _expression;
    }

    @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);
    }
View Full Code Here

       
        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) {
View Full Code Here

        int startColumnIndex = oldColumns.size();
        int columnCount = _columnCount;
        if (_columnCount > 0) {
            int columnsLeftToTranspose = _columnCount;
            for (int c = 0; c < oldColumns.size(); c++) {
                Column column = oldColumns.get(c);
                if (columnsLeftToTranspose == 0) {
                    // This column is beyond the columns to transpose
                   
                    Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
                    newColumn.setName(column.getName());
                   
                    newColumns.add(newColumn);
                } else if (columnsLeftToTranspose < _columnCount) {
                    // This column is a column to transpose, but not the first
                    // nothing to do
                   
                    columnsLeftToTranspose--;
                } else if (_startColumnName.equals(column.getName())) {
                    // This is the first column to transpose
                   
                    startColumnIndex = c;
                   
                    if (_combinedColumnName != null) {
                        newColumns.add(new Column(newColumns.size(), _combinedColumnName));
                    } else {
                        newColumns.add(new Column(newColumns.size(), _keyColumnName));
                        newColumns.add(new Column(newColumns.size(), _valueColumnName));
                    }
                   
                    columnsLeftToTranspose--;
                } else {
                    // This column is before all columns to transpose
                   
                    Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
                    newColumn.setName(column.getName());
                   
                    newColumns.add(newColumn);
                }
            }
        } else {
            for (int c = 0; c < oldColumns.size(); c++) {
                Column column = oldColumns.get(c);
                if (_startColumnName.equals(column.getName())) {
                    // This is the first column to transpose
                   
                    startColumnIndex = c;
                   
                    if (_combinedColumnName != null) {
                        newColumns.add(new Column(newColumns.size(), _combinedColumnName));
                    } else {
                        newColumns.add(new Column(newColumns.size(), _keyColumnName));
                        newColumns.add(new Column(newColumns.size(), _valueColumnName));
                    }
                    break;
                } else {
                    // This column is before all columns to transpose
                   
                    Column newColumn = new Column(newColumns.size(), column.getOriginalHeaderLabel());
                    newColumn.setName(column.getName());
                   
                    newColumns.add(newColumn);
                }
            }
            columnCount = oldColumns.size() - startColumnIndex;
        }
       
        List<Row> oldRows = project.rows;
        List<Row> newRows = new ArrayList<Row>(oldRows.size() * columnCount);
        for (int r = 0; r < oldRows.size(); r++) {
            Row oldRow = project.rows.get(r);
            Row firstNewRow = new Row(newColumns.size());
            int firstNewRowIndex = newRows.size();
           
            newRows.add(firstNewRow);
           
            int transposedCells = 0;
            for (int c = 0; c < oldColumns.size(); c++) {
                Column column = oldColumns.get(c);
                Cell cell = oldRow.getCell(column.getCellIndex());
               
                if (c < startColumnIndex) {
                    firstNewRow.setCell(c, cell);
                } else if (c == startColumnIndex || c < startColumnIndex + columnCount) {
                    if (_combinedColumnName != null) {
                        Cell newCell;
                        if (cell == null || cell.value == null) {
                            if (_prependColumnName && !_ignoreBlankCells) {
                                newCell = new Cell(column.getName() + _separator, null);
                            } else {
                                continue;
                            }
                        } else if (_prependColumnName) {
                            newCell = new Cell(column.getName() + _separator + cell.value, null);
                        } else {
                            newCell = cell;
                        }
                       
                        Row rowToModify;
                        if (transposedCells == 0) {
                            rowToModify = firstNewRow;
                        } else {
                            rowToModify = new Row(newColumns.size());
                            newRows.add(rowToModify);
                        }
                        rowToModify.setCell(startColumnIndex, newCell);
                       
                        transposedCells++;
                    } else {
                        if (_ignoreBlankCells && (cell == null || cell.value == null)) {
                            continue;
                        }
                       
                        Row rowToModify;
                        if (transposedCells == 0) {
                            rowToModify = firstNewRow;
                        } else {
                            rowToModify = new Row(newColumns.size());
                            newRows.add(rowToModify);
                        }
                        rowToModify.setCell(startColumnIndex, new Cell(column.getName(), null));
                        rowToModify.setCell(startColumnIndex + 1, cell);
                       
                        transposedCells++;
                    }
                   
                } else {
                    firstNewRow.setCell(
                        c - columnCount + (_combinedColumnName != null ? 1 : 2),
                        cell);
                }
            }
           
            if (_fillDown) {
                for (int r2 = firstNewRowIndex + 1; r2 < newRows.size(); r2++) {
                    Row newRow = newRows.get(r2);
                    for (int c = 0; c < newColumns.size(); c++) {
                        if (c < startColumnIndex ||
                            (_combinedColumnName != null ?
                                c > startColumnIndex :
                                c > startColumnIndex + 1)) {
                            Column column = newColumns.get(c);
                            int cellIndex = column.getCellIndex();
                           
                            Cell cellToCopy = firstNewRow.getCell(cellIndex);
                            if (cellToCopy != null && newRow.getCell(cellIndex) == null) {
                                newRow.setCell(cellIndex, cellToCopy);
                            }
View Full Code Here

       
        protected void populateEntries() throws Exception {
            Engine engine = new Engine(_project);
            engine.initializeFromJSON(_engineConfig);
           
            Column column = _project.columnModel.getColumnByName(_columnName);
            if (column == null) {
                throw new Exception("No column named " + _columnName);
            }
           
            _entries = new ArrayList<ReconEntry>(_project.rows.size());
            _cellIndex = column.getCellIndex();
           
            FilteredRows filteredRows = engine.getAllFilteredRows();
            filteredRows.accept(_project, new RowVisitor() {
                @Override
                public void start(Project project) {
View Full Code Here

        return "Split multi-valued cells in column " + _columnName;
    }

    @Override
    protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
        Column column = project.columnModel.getColumnByName(_columnName);
        if (column == null) {
            throw new Exception("No column named " + _columnName);
        }
        int cellIndex = column.getCellIndex();
       
        Column keyColumn = project.columnModel.getColumnByName(_keyColumnName);
        if (keyColumn == null) {
            throw new Exception("No key column named " + _keyColumnName);
        }
        int keyCellIndex = keyColumn.getCellIndex();
       
        List<Row> newRows = new ArrayList<Row>();
       
        int oldRowCount = project.rows.size();
        for (int r = 0; r < oldRowCount; r++) {
View Full Code Here

            " for " + cellChanges.size() + " cells in column " + column.getName();
    }

    @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;
            Map<Long, Recon> dupReconMap = new HashMap<Long, Recon>();
            long historyEntryID;
           
            public RowVisitor init(int cellIndex, List<CellChange> cellChanges, long historyEntryID) {
                this.cellIndex = cellIndex;
                this.cellChanges = cellChanges;
                this.historyEntryID = historyEntryID;
                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);
                if (cell != null && cell.recon != null) {
                    Recon newRecon = null;
                    if (!_clearData) {
                        if (dupReconMap.containsKey(cell.recon.id)) {
                            newRecon = dupReconMap.get(cell.recon.id);
                            newRecon.judgmentBatchSize++;
                        } else {
                            newRecon = cell.recon.dup(historyEntryID);
                            newRecon.match = null;
                            newRecon.matchRank = -1;
                            newRecon.judgment = Judgment.None;
                            newRecon.judgmentAction = "mass";
                            newRecon.judgmentBatchSize = 1;
                           
                            dupReconMap.put(cell.recon.id, newRecon);
                        }
                    }
                   
                    Cell newCell = new Cell(cell.value, newRecon);
                   
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                    cellChanges.add(cellChange);
                }
                return false;
            }
        }.init(column.getCellIndex(), cellChanges, historyEntryID);
    }
View Full Code Here

            " cells in column " + column.getName();
    }

    @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);
    }
View Full Code Here

                ", one topic for each cell");
    }

    @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;
            Map<String, Recon>  sharedRecons = new HashMap<String, Recon>();
            long                historyEntryID;
           
            public RowVisitor init(int cellIndex, List<CellChange> cellChanges, long historyEntryID) {
                this.cellIndex = cellIndex;
                this.cellChanges = cellChanges;
                this.historyEntryID = historyEntryID;
                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);
                if (cell != null) {
                    Recon recon = null;
                    if (_shareNewTopics) {
                        String s = cell.value == null ? "" : cell.value.toString();
                        if (sharedRecons.containsKey(s)) {
                            recon = sharedRecons.get(s);
                            recon.judgmentBatchSize++;
                        } else {
                            recon = new Recon(historyEntryID, null, null);
                            recon.judgment = Judgment.New;
                            recon.judgmentBatchSize = 1;
                            recon.judgmentAction = "mass";
                           
                            sharedRecons.put(s, recon);
                        }
                    } else {
                        recon = cell.recon == null ? new Recon(historyEntryID, null, null) : cell.recon.dup(historyEntryID);
                        recon.match = null;
                        recon.matchRank = -1;
                        recon.judgment = Judgment.New;
                        recon.judgmentBatchSize = 1;
                        recon.judgmentAction = "mass";
                    }
                   
                    Cell newCell = new Cell(cell.value, recon);
                   
                    CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
                    cellChanges.add(cellChange);
                }
                return false;
            }
        }.init(column.getCellIndex(), cellChanges, historyEntryID);
    }
View Full Code Here

TOP

Related Classes of com.google.refine.model.Column

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.