Search in sources :

Example 1 with EvalError

use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.

the class SmartSplit method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length >= 1 && args.length <= 2) {
        CSVParser parser = null;
        Object v = args[0];
        String s = v.toString();
        if (args.length > 1) {
            String sep = args[1].toString();
            parser = new CSVParser(sep.charAt(0), CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, CSVParser.DEFAULT_STRICT_QUOTES, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE, false);
        }
        if (parser == null) {
            int tab = s.indexOf('\t');
            if (tab >= 0) {
                parser = s_tabParser;
            } else {
                parser = s_commaParser;
            }
        }
        try {
            return parser.parseLine(s);
        } catch (IOException e) {
            return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " error: " + e.getMessage());
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 or 2 strings");
}
Also used : CSVParser(au.com.bytecode.opencsv.CSVParser) EvalError(com.google.refine.expr.EvalError) IOException(java.io.IOException)

Example 2 with EvalError

use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.

the class ToTitlecase method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 1 && args[0] != null) {
        Object o = args[0];
        String s = o instanceof String ? (String) o : o.toString();
        return WordUtils.capitalizeFully(s, delimiters);
    } else if (args.length == 2 && args[0] != null && args[1] != null) {
        Object o = args[0];
        String s = o instanceof String ? (String) o : o.toString();
        o = args[1];
        String delims = o instanceof String ? (String) o : o.toString();
        return WordUtils.capitalizeFully(s, delims.toCharArray());
    } else {
        return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a one or two strings");
    }
}
Also used : EvalError(com.google.refine.expr.EvalError)

Example 3 with EvalError

use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.

the class Cross method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 3) {
        // from project is implied
        // from cell
        Object wrappedCell = args[0];
        Object toProjectName = args[1];
        Object toColumnName = args[2];
        if (wrappedCell != null && wrappedCell instanceof WrappedCell && toProjectName != null && toProjectName instanceof String && toColumnName != null && toColumnName instanceof String) {
            ProjectJoin join = ProjectManager.singleton.getInterProjectModel().getJoin(ProjectManager.singleton.getProjectMetadata(((Project) bindings.get("project")).id).getName(), ((WrappedCell) wrappedCell).columnName, (String) toProjectName, (String) toColumnName);
            return join.getRows(((WrappedCell) wrappedCell).cell.value);
        }
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a cell, a project name to join with, and a column name in that project");
}
Also used : ProjectJoin(com.google.refine.InterProjectModel.ProjectJoin) WrappedCell(com.google.refine.expr.WrappedCell) EvalError(com.google.refine.expr.EvalError)

Example 4 with EvalError

use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.

the class FacetCount method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length == 3 && args[1] instanceof String && args[2] instanceof String) {
        // choice value to look up
        Object choiceValue = args[0];
        String facetExpression = (String) args[1];
        String columnName = (String) args[2];
        Project project = (Project) bindings.get("project");
        Column column = project.columnModel.getColumnByName(columnName);
        if (column == null) {
            return new EvalError("No such column named " + columnName);
        }
        String key = "nominal-bin:" + facetExpression;
        ExpressionNominalValueGrouper grouper = (ExpressionNominalValueGrouper) column.getPrecompute(key);
        if (grouper == null) {
            try {
                Evaluable eval = MetaParser.parse(facetExpression);
                Engine engine = new Engine(project);
                grouper = new ExpressionNominalValueGrouper(eval, columnName, column.getCellIndex());
                engine.getAllRows().accept(project, grouper);
                column.setPrecompute(key, grouper);
            } catch (ParsingException e) {
                return new EvalError("Error parsing facet expression " + facetExpression);
            }
        }
        return grouper.getChoiceValueCountMultiple(choiceValue);
    }
    return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a choice value, an expression as a string, and a column name");
}
Also used : Project(com.google.refine.model.Project) Evaluable(com.google.refine.expr.Evaluable) Column(com.google.refine.model.Column) ParsingException(com.google.refine.expr.ParsingException) EvalError(com.google.refine.expr.EvalError) Engine(com.google.refine.browsing.Engine) ExpressionNominalValueGrouper(com.google.refine.browsing.util.ExpressionNominalValueGrouper)

Example 5 with EvalError

use of com.google.refine.expr.EvalError in project OpenRefine by OpenRefine.

the class Filter method call.

@Override
public Object call(Properties bindings, Evaluable[] args) {
    Object o = args[0].evaluate(bindings);
    if (ExpressionUtils.isError(o)) {
        return o;
    } else if (!ExpressionUtils.isArrayOrCollection(o) && !(o instanceof JSONArray)) {
        return new EvalError("First argument is not an array");
    }
    String name = ((VariableExpr) args[1]).getName();
    Object oldValue = bindings.get(name);
    try {
        List<Object> results = null;
        if (o.getClass().isArray()) {
            Object[] values = (Object[]) o;
            results = new ArrayList<Object>(values.length);
            for (Object v : values) {
                if (v != null) {
                    bindings.put(name, v);
                } else {
                    bindings.remove(name);
                }
                Object r = args[2].evaluate(bindings);
                if (r instanceof Boolean && ((Boolean) r).booleanValue()) {
                    results.add(v);
                }
            }
        } else if (o instanceof JSONArray) {
            JSONArray a = (JSONArray) o;
            int l = a.length();
            results = new ArrayList<Object>(l);
            for (int i = 0; i < l; i++) {
                try {
                    Object v = a.get(i);
                    if (v != null) {
                        bindings.put(name, v);
                    } else {
                        bindings.remove(name);
                    }
                    Object r = args[2].evaluate(bindings);
                    if (r instanceof Boolean && ((Boolean) r).booleanValue()) {
                        results.add(v);
                    }
                } catch (JSONException e) {
                    results.add(new EvalError(e.getMessage()));
                }
            }
        } else {
            Collection<Object> collection = ExpressionUtils.toObjectCollection(o);
            results = new ArrayList<Object>(collection.size());
            for (Object v : collection) {
                if (v != null) {
                    bindings.put(name, v);
                } else {
                    bindings.remove(name);
                }
                Object r = args[2].evaluate(bindings);
                if (r instanceof Boolean && ((Boolean) r).booleanValue()) {
                    results.add(v);
                }
            }
        }
        return results.toArray();
    } finally {
        /*
             *  Restore the old value bound to the variable, if any.
             */
        if (oldValue != null) {
            bindings.put(name, oldValue);
        } else {
            bindings.remove(name);
        }
    }
}
Also used : JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Collection(java.util.Collection) EvalError(com.google.refine.expr.EvalError) VariableExpr(com.google.refine.grel.ast.VariableExpr)

Aggregations

EvalError (com.google.refine.expr.EvalError)23 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 VariableExpr (com.google.refine.grel.ast.VariableExpr)4 ArrayList (java.util.ArrayList)4 Calendar (java.util.Calendar)4 Date (java.util.Date)4 Evaluable (com.google.refine.expr.Evaluable)3 ParsingException (com.google.refine.expr.ParsingException)3 Project (com.google.refine.model.Project)3 WrappedCell (com.google.refine.expr.WrappedCell)2 CalendarParserException (com.google.refine.expr.util.CalendarParserException)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 Element (org.jsoup.nodes.Element)2 CSVParser (au.com.bytecode.opencsv.CSVParser)1 ProjectJoin (com.google.refine.InterProjectModel.ProjectJoin)1 ProjectMetadata (com.google.refine.ProjectMetadata)1 Engine (com.google.refine.browsing.Engine)1 ExpressionNominalValueGrouper (com.google.refine.browsing.util.ExpressionNominalValueGrouper)1