Search in sources :

Example 6 with StreamWriter

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

the class UimaTaskBase method persist.

@Override
public void persist(final TaskContext aContext) throws IOException {
    super.persist(aContext);
    aContext.storeBinary(COLLECTION_READER_DESC_KEY, new StreamWriter() {

        @Override
        public void write(OutputStream aStream) throws Exception {
            getCollectionReaderDescription(aContext).toXML(aStream);
        }
    });
    aContext.storeBinary(ANALYSIS_ENGINE_DESC_KEY, new StreamWriter() {

        @Override
        public void write(OutputStream aStream) throws Exception {
            AnalysisEngineDescription analysisDesc = getAnalysisEngineDescription(aContext);
            // FIXME should use the same resource manager here
            // as the engine uses!
            analysisDesc.resolveImports(UIMAFramework.newDefaultResourceManager());
            analysisDesc.toXML(aStream);
        }
    });
}
Also used : StreamWriter(org.dkpro.lab.storage.StreamWriter) OutputStream(java.io.OutputStream) AnalysisEngineDescription(org.apache.uima.analysis_engine.AnalysisEngineDescription) IOException(java.io.IOException) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException)

Example 7 with StreamWriter

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

the class FlexTable 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 8 with StreamWriter

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

the class FlexTable method getTextWriter.

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

        @Override
        public void write(OutputStream aStream) throws Exception {
            String[] colIds = FlexTable.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 {
                // TODO: A bit of a hack; use Regex instead?
                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 9 with StreamWriter

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

the class FlexTable 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[FlexTable.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 10 with StreamWriter

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

the class FlexTable 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.
 */
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++) {
                // TODO: Pass column alignment as parameter
                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);
                    // TODO: Move to util class? Which one?
                    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 && isPercentage(val)) {
                val = doRoundDouble(val, decimalPlacesForPercentages);
            } else if (decimalPlacesForDouble != -1 && isDouble(val)) {
                val = doRoundDouble(val, decimalPlacesForDouble);
            }
            return val;
        }

        private String doStringReplace(String val) {
            // TODO MW: Make all this configurable
            val = val.replace("de.tudarmstadt.ukp.dkpro.tc.core.task.", "");
            val = val.replace("de.tudarmstadt.ukp.dkpro.tc.weka.task.", "");
            return val;
        }

        private String doRoundDouble(String val, int decimalPlaces) {
            double d = Double.parseDouble(val);
            // TODO: Could this cause
            int temp = (int) (d * Math.pow(10, decimalPlaces));
            // rounding problems?
            Double newD = (temp) / Math.pow(10, decimalPlaces);
            return newD.toString();
        }

        private boolean isDouble(String val) {
            try {
                // TODO: A bit of a hack; use Regex instead?
                Double.parseDouble(val);
            } catch (NumberFormatException ex) {
                return false;
            }
            return true;
        }

        private boolean isPercentage(String val) {
            // TODO: Implement
            return false;
        }

        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