use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.
the class XlsxReader method createColumn.
private Column<?> createColumn(int colNum, String name, Sheet sheet, int excelColNum, TableRange tableRange, XlsxReadOptions options) {
Column<?> column;
ColumnType columnType = options.columnTypeReadOptions().columnType(colNum, name).orElse(calculateColumnTypeForColumn(sheet, excelColNum, tableRange).orElse(ColumnType.STRING));
column = columnType.create(name);
return column;
}
use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.
the class FileReader method getColumnTypes.
/**
* Returns an array containing the inferred columnTypes for the file being read, as calculated by
* the ColumnType inference logic. These types may not be correct.
*/
public ColumnType[] getColumnTypes(Reader reader, ReadOptions options, int linesToSkip, AbstractParser<?> parser, String[] columnNames) {
if (parser.getContext() == null)
parser.beginParsing(reader);
for (int i = 0; i < linesToSkip; i++) {
parser.parseNext();
}
ColumnTypeDetector detector = new ColumnTypeDetector(options.columnTypesToDetect());
ColumnType[] columnTypes = detector.detectColumnTypes(new Iterator<String[]>() {
String[] nextRow = parser.parseNext();
@Override
public boolean hasNext() {
return nextRow != null;
}
@Override
public String[] next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
String[] tmp = nextRow;
nextRow = parser.parseNext();
return tmp;
}
}, options);
// If there are columnTypes configured by the user use them
for (int i = 0; i < columnTypes.length; i++) {
boolean hasColumnName = columnNames != null && i < columnNames.length;
Optional<ColumnType> configuredColumnType = options.columnTypeReadOptions().columnType(i, hasColumnName ? columnNames[i] : null);
if (configuredColumnType.isPresent()) {
columnTypes[i] = configuredColumnType.get();
}
}
return columnTypes;
}
use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.
the class FileReader method parseRows.
protected Table parseRows(ReadOptions options, boolean headerOnly, Reader reader, ReadOptions.ColumnTypeReadOptions columnTypeReadOptions, AbstractParser<?> parser, int sampleSize) {
parser.beginParsing(reader);
Table table = Table.create(options.tableName());
List<String> headerRow = Lists.newArrayList(getColumnNames(options, columnTypeReadOptions, parser));
@SuppressWarnings({ "UnstableApiUsage", "OptionalGetWithoutIsPresent" }) ColumnType[] types = Streams.mapWithIndex(headerRow.stream(), (columnName, idx) -> columnTypeReadOptions.columnType((int) idx, columnName)).filter(Optional::isPresent).map(Optional::get).toArray(ColumnType[]::new);
for (int x = 0; x < types.length; x++) {
if (types[x] != SKIP) {
String columnName = cleanName(headerRow.get(x));
if (Strings.isNullOrEmpty(columnName)) {
columnName = "Column " + table.columnCount();
}
Column<?> newColumn = types[x].create(columnName);
table.addColumns(newColumn);
}
}
if (!headerOnly) {
String[] columnNames = selectColumnNames(headerRow, types);
int[] columnIndexes = new int[columnNames.length];
for (int i = 0; i < columnIndexes.length; i++) {
// get the index in the original table, which includes skipped fields
columnIndexes[i] = headerRow.indexOf(columnNames[i]);
}
addRows(options, types, parser, table, columnIndexes, sampleSize);
}
return table;
}
use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.
the class Rows method compareRows.
public static boolean compareRows(int rowInOriginal, Table original, Table tempTable) {
int columnCount = original.columnCount();
boolean result;
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
ColumnType columnType = original.column(columnIndex).type();
result = columnType.compare(rowInOriginal, tempTable.column(columnIndex), original.column(columnIndex));
if (!result) {
return false;
}
}
return true;
}
use of tech.tablesaw.api.ColumnType in project symja_android_library by axkr.
the class ASTDataset method normal.
@Override
public IASTAppendable normal(boolean nilIfUnevaluated) {
Cache<IAST, IAST> cache = CacheBuilder.newBuilder().maximumSize(500).build();
final List<String> names = fTable.columnNames();
List<IStringX> namesStr = new ArrayList<IStringX>(names.size());
for (int i = 0; i < names.size(); i++) {
namesStr.add(F.stringx(names.get(i)));
}
if (names.size() == 1) {
Column<?> column = fTable.column(names.get(0));
ColumnType t = column.type();
IASTAppendable resultList = F.ListAlloc(column.size());
for (int j = 0; j < column.size(); j++) {
Object obj = column.get(j);
IExpr expr = F.NIL;
expr = dataToExpr(obj, t);
resultList.append(expr);
}
return resultList;
}
IASTAppendable list = F.ListAlloc(names.size());
int size = fTable.rowCount();
for (int k = 0; k < size; k++) {
Row row = fTable.row(k);
IAssociation assoc = F.assoc();
for (int j = 0; j < row.columnCount(); j++) {
String columnName = names.get(j);
IStringX colName = namesStr.get(j);
ColumnType t = row.getColumnType(columnName);
Object obj = row.getObject(j);
if (t.equals(ColumnType.EXPR)) {
IExpr expr = (IExpr) obj;
ruleCache(cache, assoc, F.Rule(colName, expr));
} else if (t.equals(ColumnType.BOOLEAN)) {
Boolean b = row.getBoolean(j);
if (b) {
ruleCache(cache, assoc, F.Rule(colName, S.True));
} else {
ruleCache(cache, assoc, F.Rule(colName, S.False));
}
} else if (t.equals(ColumnType.SHORT)) {
short sValue = row.getShort(j);
ruleCache(cache, assoc, F.Rule(colName, F.ZZ(sValue)));
} else if (t.equals(ColumnType.INTEGER)) {
int iValue = row.getInt(j);
ruleCache(cache, assoc, F.Rule(colName, F.ZZ(iValue)));
} else if (t.equals(ColumnType.LONG)) {
long lValue = row.getLong(j);
ruleCache(cache, assoc, F.Rule(colName, F.ZZ(lValue)));
} else if (t.equals(ColumnType.FLOAT)) {
float fValue = row.getFloat(j);
ruleCache(cache, assoc, F.Rule(colName, F.num(fValue)));
} else if (t.equals(ColumnType.DOUBLE)) {
double dValue = row.getDouble(j);
ruleCache(cache, assoc, F.Rule(colName, F.num(dValue)));
} else if (t.equals(ColumnType.STRING)) {
ruleCache(cache, assoc, F.Rule(colName, F.stringx(row.getString(j))));
} else if (t.equals(ColumnType.LOCAL_DATE_TIME)) {
LocalDateTime lDate = row.getDateTime(j);
ruleCache(cache, assoc, F.Rule(colName, DateObjectExpr.newInstance(lDate)));
} else if (t.equals(ColumnType.LOCAL_DATE)) {
LocalDate lDate = row.getDate(j);
ruleCache(cache, assoc, F.Rule(colName, DateObjectExpr.newInstance(lDate.atStartOfDay())));
} else if (t.equals(ColumnType.LOCAL_TIME)) {
LocalTime lTime = row.getTime(j);
ruleCache(cache, assoc, F.Rule(colName, TimeObjectExpr.newInstance(lTime)));
} else if (t.equals(ColumnType.SKIP)) {
// ruleCache(cache, assoc, F.Rule(colName, F.Missing));
ruleCache(cache, assoc, F.Rule(colName, F.Missing(S.NotAvailable)));
} else {
IExpr valueStr = F.stringx(obj.toString());
ruleCache(cache, assoc, F.Rule(colName, valueStr));
}
}
if (size == 1) {
return assoc;
}
list.append(assoc);
}
return list;
}
Aggregations