use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class Relation method structure.
public Table structure() {
Table t = Table.create("Structure of " + name());
IntColumn index = IntColumn.indexColumn("Index", columnCount(), 0);
StringColumn columnName = StringColumn.create("Column Name", columnCount());
StringColumn columnType = StringColumn.create("Column Type", columnCount());
t.addColumns(index);
t.addColumns(columnName);
t.addColumns(columnType);
for (int i = 0; i < columnCount(); i++) {
Column<?> column = this.columns().get(i);
columnType.set(i, column.type().name());
columnName.set(i, columnNames().get(i));
}
return t;
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class HtmlReader method read.
@Override
public Table read(HtmlReadOptions options) throws IOException {
Document doc;
InputStream inputStream = options.source().inputStream();
if (inputStream != null) {
// Reader must support mark, so can't use InputStreamReader
// Parse the InputStream directly
doc = Jsoup.parse(inputStream, null, "");
} else {
doc = Parser.htmlParser().parseInput(options.source().createReader(null), "");
}
Elements tables = doc.select("table");
int tableIndex = 0;
if (tables.size() != 1) {
if (options.tableIndex() != null) {
if (options.tableIndex() >= 0 && options.tableIndex() < tables.size()) {
tableIndex = options.tableIndex();
} else {
throw new IndexOutOfBoundsException("Table index outside bounds. The URL has " + tables.size() + " tables");
}
} else {
throw new IllegalStateException(tables.size() + " tables found. When more than one html table is present on the page you must specify the index of the table to read from.");
}
}
Element htmlTable = tables.get(tableIndex);
List<String[]> rows = new ArrayList<>();
for (Element row : htmlTable.select("tr")) {
Elements headerCells = row.getElementsByTag("th");
Elements cells = row.getElementsByTag("td");
String[] nextLine = Stream.concat(headerCells.stream(), cells.stream()).map(Element::text).toArray(String[]::new);
rows.add(nextLine);
}
Table table = Table.create(options.tableName());
if (rows.isEmpty()) {
return table;
}
List<String> columnNames = new ArrayList<>();
if (options.header()) {
String[] headerRow = rows.remove(0);
for (int i = 0; i < headerRow.length; i++) {
columnNames.add(headerRow[i]);
}
} else {
for (int i = 0; i < rows.get(0).length; i++) {
columnNames.add("C" + i);
}
}
return TableBuildingUtils.build(columnNames, rows, options);
}
use of tech.tablesaw.api.Table 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.Table in project symja_android_library by axkr.
the class TableSlice method first.
@Override
public Table first(int nRows) {
int count = 0;
PrimitiveIterator.OfInt it = sourceRowNumberIterator();
Table copy = table.emptyCopy();
while (it.hasNext() && count < nRows) {
int row = it.nextInt();
copy.addRow(table.row(row));
count++;
}
return copy;
}
use of tech.tablesaw.api.Table in project symja_android_library by axkr.
the class DataFrameReader method db.
public Table db(ResultSet resultSet, String tableName) throws SQLException {
Table table = SqlResultSetReader.read(resultSet);
table.setName(tableName);
return table;
}
Aggregations