Search in sources :

Example 1 with StreamWriter

use of org.dkpro.lab.storage.StreamWriter in project dkpro-lab by dkpro.

the class FlexTable method getExcelWriter.

public StreamWriter getExcelWriter() {
    return new StreamWriter() {

        @Override
        public void write(OutputStream aStream) throws Exception {
            String[] colIds = FlexTable.this.compact ? getCompactColumnIds(false) : getColumnIds();
            Workbook wb = new HSSFWorkbook();
            Sheet sheet = wb.createSheet("Summary");
            PrintSetup printSetup = sheet.getPrintSetup();
            printSetup.setLandscape(true);
            sheet.setFitToPage(true);
            sheet.setHorizontallyCenter(true);
            // Header row
            {
                Row row = sheet.createRow(0);
                Cell rowIdCell = row.createCell(0);
                rowIdCell.setCellValue("ID");
                int colNum = 1;
                for (String colId : colIds) {
                    Cell cell = row.createCell(colNum);
                    cell.setCellValue(colId);
                    colNum++;
                }
            }
            // Body rows
            {
                int rowNum = 1;
                for (String rowId : getRowIds()) {
                    Row row = sheet.createRow(rowNum);
                    Cell rowIdCell = row.createCell(0);
                    rowIdCell.setCellValue(rowId);
                    int colNum = 1;
                    for (String colId : colIds) {
                        Cell cell = row.createCell(colNum);
                        String value = getValueAsString(rowId, colId);
                        try {
                            cell.setCellValue(Double.valueOf(value));
                        } catch (NumberFormatException e) {
                            cell.setCellValue(value);
                        }
                        colNum++;
                    }
                    rowNum++;
                }
            }
            wb.write(aStream);
            wb.close();
        }
    };
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) StreamWriter(org.dkpro.lab.storage.StreamWriter) OutputStream(java.io.OutputStream) PrintSetup(org.apache.poi.ss.usermodel.PrintSetup) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 2 with StreamWriter

use of org.dkpro.lab.storage.StreamWriter in project dkpro-tc by dkpro.

the class TcFlexTable method getCsvWriter.

public StreamWriter getCsvWriter() {
    return new StreamWriter() {

        @Override
        public void write(OutputStream aStream) throws Exception {
            String[] colIds = getColumnIds();
            CSVWriter writer = new CSVWriter(new OutputStreamWriter(aStream, "UTF-8"));
            String[] buf = new String[TcFlexTable.this.columns.size() + 1];
            {
                int i = 1;
                buf[0] = "ID";
                for (String col : colIds) {
                    buf[i] = col;
                    i++;
                }
            }
            writer.writeNext(buf);
            for (String rowId : getRowIds()) {
                buf[0] = rowId;
                int i = 1;
                for (String colId : colIds) {
                    buf[i] = getValueAsString(rowId, colId);
                    i++;
                }
                writer.writeNext(buf);
            }
            writer.flush();
            writer.close();
        }
    };
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) StreamWriter(org.dkpro.lab.storage.StreamWriter) OutputStream(java.io.OutputStream) CSVWriter(au.com.bytecode.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Example 3 with StreamWriter

use of org.dkpro.lab.storage.StreamWriter in project dkpro-tc by dkpro.

the class TcFlexTable method getTextWriter.

public StreamWriter getTextWriter() {
    return new StreamWriter() {

        @Override
        public void write(OutputStream aStream) throws Exception {
            String[] colIds = TcFlexTable.this.compact ? getCompactColumnIds(false) : getColumnIds();
            // Obtain the width of the columns based on their content and headers
            // col 0 is reserved here for the rowId width
            int[] colWidths = new int[colIds.length + 1];
            for (String rowId : getRowIds()) {
                colWidths[0] = Math.max(colWidths[0], rowId.length());
            }
            for (int i = 1; i < colWidths.length; i++) {
                colWidths[i] = Math.max(colWidths[i], colIds[i - 1].length());
                for (String rowId : getRowIds()) {
                    colWidths[i] = Math.max(colWidths[i], getValueAsString(rowId, colIds[i - 1]).length());
                }
            }
            StringBuilder separator = new StringBuilder();
            for (int w : colWidths) {
                if (separator.length() > 0) {
                    separator.append("-+-");
                }
                separator.append(StringUtils.repeat("-", w));
            }
            separator.insert(0, "+-");
            separator.append("-+");
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(aStream, "UTF-8"));
            // Render header column
            writer.println(separator);
            writer.print("| ");
            writer.print(StringUtils.repeat(" ", colWidths[0]));
            for (int i = 0; i < colIds.length; i++) {
                writer.print(" | ");
                // Remember: colWidth[0] is the rowId width!
                writer.print(StringUtils.center(colIds[i], colWidths[i + 1]));
            }
            writer.println(" |");
            writer.println(separator);
            // Render body
            for (String rowId : getRowIds()) {
                writer.print("| ");
                writer.print(StringUtils.rightPad(rowId, colWidths[0]));
                for (int i = 0; i < colIds.length; i++) {
                    writer.print(" | ");
                    // Remember: colWidth[0] is the rowId width!
                    String val = getValueAsString(rowId, colIds[i]);
                    if (isDouble(val)) {
                        writer.print(StringUtils.leftPad(val, colWidths[i + 1]));
                    } else {
                        writer.print(StringUtils.rightPad(val, colWidths[i + 1]));
                    }
                }
                writer.println(" |");
            }
            // Closing separator
            writer.println(separator);
            writer.flush();
        }

        private boolean isDouble(String val) {
            try {
                Double.parseDouble(val);
            } catch (NumberFormatException ex) {
                return false;
            }
            return true;
        }
    };
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) StreamWriter(org.dkpro.lab.storage.StreamWriter) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 4 with StreamWriter

use of org.dkpro.lab.storage.StreamWriter in project dkpro-tc by dkpro.

the class TcFlexTable method getTWikiWriter.

public StreamWriter getTWikiWriter() {
    return new StreamWriter() {

        protected PrintWriter writer;

        @Override
        public void write(OutputStream aStream) throws Exception {
            writer = new PrintWriter(new OutputStreamWriter(aStream, "UTF-8"));
            if (compact && rows.size() > 0) {
                String firstRowId = getRowIds()[0];
                String[] colIds = getCompactColumnIds(true);
                for (String colId : colIds) {
                    writer.print("| *");
                    writer.print(colId.replace('|', ' '));
                    writer.print("* | ");
                    writer.print(getValueAsString(firstRowId, colId).replace('|', ' '));
                    writer.println(" |");
                }
                writer.println();
                writer.println();
            }
            String[] colIds = compact ? getCompactColumnIds(false) : getColumnIds();
            String[] buf = new String[colIds.length + 1];
            {
                int i = 1;
                buf[0] = "ID";
                for (String col : colIds) {
                    buf[i] = col.replace('|', ' ');
                    i++;
                }
            }
            printHeaderRow(buf);
            for (String rowId : getRowIds()) {
                buf[0] = rowId;
                int i = 1;
                for (String colId : colIds) {
                    buf[i] = getValueAsString(rowId, colId).replace('|', ' ');
                    i++;
                }
                printRow(buf);
            }
            writer.flush();
        }

        protected void printHeaderRow(String[] aHeaders) {
            writer.print("| *");
            writer.print(StringUtils.join(aHeaders, "* | *"));
            writer.println("* |");
        }

        protected void printRow(String[] aCells) {
            writer.print("| !");
            writer.print(StringUtils.join(aCells, " | "));
            writer.println(" |");
        }
    };
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) StreamWriter(org.dkpro.lab.storage.StreamWriter) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Example 5 with StreamWriter

use of org.dkpro.lab.storage.StreamWriter in project dkpro-tc by dkpro.

the class TcFlexTable method getLatexWriter.

/**
 * Returns a LaTeX writer to write the FlexTable to a Latex file. To get a writer for a
 * transposed version of the table, call table.transpose() prior to retrieving the writer.
 *
 * @param decimalPlacesForDouble
 *            How many decimal places should double values have; if set to -1, the values won't
 *            be rounded.
 * @param decimalPlacesForPercentages
 *            How many decimal places should percentage values have; if set to -1, the values
 *            won't be rounded.
 *
 * @return a stream writer
 */
public StreamWriter getLatexWriter(final int decimalPlacesForDouble, final int decimalPlacesForPercentages) {
    return new StreamWriter() {

        @Override
        public void write(OutputStream aStream) throws Exception {
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(aStream, "UTF-8"));
            writer.print("\\begin{tabular}");
            String[] colIds = getColumnIds();
            writer.print("{");
            for (int i = 0; i <= colIds.length; i++) {
                writer.print(" l");
            }
            writer.println(" }");
            writer.print("\\small");
            String[] buf = new String[colIds.length + 1];
            {
                int i = 1;
                buf[0] = "ID";
                for (String col : colIds) {
                    buf[i] = escapeForLatex(col.replace('|', ' '));
                    i++;
                }
            }
            writer.println("\\hline");
            writer.print(StringUtils.join(buf, " & "));
            writer.println("\\\\");
            for (String rowId : getRowIds()) {
                String rowVal = doStringReplace(rowId);
                buf[0] = escapeForLatex(rowVal);
                int i = 1;
                for (String colId : colIds) {
                    String val = getValueAsString(rowId, colId);
                    val = convertNumbers(decimalPlacesForDouble, decimalPlacesForPercentages, val);
                    buf[i] = escapeForLatex(val);
                    i++;
                }
                writer.print(StringUtils.join(buf, " & "));
                writer.println("\\\\");
            }
            writer.println("\\hline");
            writer.println("\\end{tabular}");
            writer.flush();
        }

        private String convertNumbers(final int decimalPlacesForDouble, final int decimalPlacesForPercentages, String val) {
            if (decimalPlacesForPercentages != -1) {
                val = doRoundDouble(val, decimalPlacesForPercentages);
            } else if (decimalPlacesForDouble != -1 && isDouble(val)) {
                val = doRoundDouble(val, decimalPlacesForDouble);
            }
            return val;
        }

        private String doStringReplace(String val) {
            val = val.replace("org.dkpro.tc.core.task.", "");
            val = val.replace("org.dkpro.tc.ml.", "");
            return val;
        }

        private String doRoundDouble(String val, int decimalPlaces) {
            double d = Double.parseDouble(val);
            int temp = (int) (d * Math.pow(10, decimalPlaces));
            Double newD = (temp) / Math.pow(10, decimalPlaces);
            return newD.toString();
        }

        private boolean isDouble(String val) {
            try {
                Double.parseDouble(val);
            } catch (NumberFormatException ex) {
                return false;
            }
            return true;
        }

        private String escapeForLatex(String val) {
            val = val.replace("_", "\\_");
            return val;
        }
    };
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) StreamWriter(org.dkpro.lab.storage.StreamWriter) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter)

Aggregations

OutputStream (java.io.OutputStream)11 StreamWriter (org.dkpro.lab.storage.StreamWriter)11 OutputStreamWriter (java.io.OutputStreamWriter)10 PrintWriter (java.io.PrintWriter)6 CSVWriter (au.com.bytecode.opencsv.CSVWriter)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2 Cell (org.apache.poi.ss.usermodel.Cell)2 PrintSetup (org.apache.poi.ss.usermodel.PrintSetup)2 Row (org.apache.poi.ss.usermodel.Row)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 Workbook (org.apache.poi.ss.usermodel.Workbook)2 IOException (java.io.IOException)1 AnalysisEngineDescription (org.apache.uima.analysis_engine.AnalysisEngineDescription)1 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)1