Example usage for org.apache.commons.csv CSVFormat DEFAULT

List of usage examples for org.apache.commons.csv CSVFormat DEFAULT

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVFormat DEFAULT.

Prototype

CSVFormat DEFAULT

To view the source code for org.apache.commons.csv CSVFormat DEFAULT.

Click Source Link

Document

Standard comma separated format, as for #RFC4180 but allowing empty lines.

Usage

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step8GoldDataAggregator.java

public static File printHashMap(Map<String, Annotations> annotations, int numberOfAnnotators) {

    File dir = new File(TEMP_DIR);
    CSVPrinter csvFilePrinter;/*from   w  ww  . ja  va  2s . c  om*/
    FileWriter fileWriter;
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator('\n').withDelimiter(',').withQuote(null);

    File filename = null;
    try {
        filename = File.createTempFile(TEMP_CSV, EXT, dir);
        fileWriter = new FileWriter(filename);
        csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
        int count = 0;
        for (Map.Entry entry : annotations.entrySet()) {
            Annotations votes = (Annotations) entry.getValue();
            //Create the CSVFormat object with "\n" as a record delimiter
            if (votes == null) {
                throw new IllegalStateException("There are no votes for " + entry.getKey());
            }
            ArrayList<Integer> trueAnnotators = (ArrayList<Integer>) votes.trueAnnotations;
            ArrayList<Integer> falseAnnotators = (ArrayList<Integer>) votes.falseAnnotations;
            if (trueAnnotators.size() + falseAnnotators.size() < 5) {
                try {
                    throw new IllegalStateException(
                            "There are " + trueAnnotators.size() + " true and " + falseAnnotators.size()
                                    + " false and annotations for " + entry.getKey() + " element");
                } catch (IllegalStateException ex) {
                    ex.printStackTrace();
                }
            }
            List<String> votesString = Arrays.asList(new String[numberOfAnnotators]);
            for (int i = 0; i < numberOfAnnotators; i++) {
                if (trueAnnotators.contains(i)) {
                    votesString.set(i, "true");
                } else if (falseAnnotators.contains(i)) {
                    votesString.set(i, "false");
                } else
                    votesString.set(i, "");
            }

            if (votesString.size() != numberOfAnnotators) {
                throw new IllegalStateException(
                        "Number of annotators is " + votesString.size() + " expected " + numberOfAnnotators);
            } else {
                csvFilePrinter.printRecord(votesString);
            }

            if (count % 1000 == 0) {
                System.out.println("Processed " + count + " instances");
            }
            count++;

        }
        fileWriter.flush();
        fileWriter.close();
        csvFilePrinter.close();

    } catch (Exception e) {
        System.out.println("Error in CsvFileWriter !!!");
        e.printStackTrace();
    }
    System.out.println("Wrote to temporary file " + filename);

    return filename;

}

From source file:io.ecarf.core.cloud.task.processor.reason.phase2.DoReasonTask8.java

/**
 * //from   www . ja v a2s. co m
 * @param term
 * @param select
 * @param schemaTriples
 * @param rows
 * @param table
 * @param writer
 * @return
 * @throws IOException
 */
protected int inferAndSaveTriplesToFile(QueryResult queryResult, Set<Long> productiveTerms, PrintWriter writer)
        throws IOException {

    //Term term, List<String> select, Set<Triple> schemaTriples
    log.info("********************** Starting Inference Round **********************");

    int inferredTriples = 0;
    //int failedTriples = 0;

    boolean compressed = queryResult.getTotalRows() > this.ddLimit;

    List<String> files = queryResult.getStats().getOutputFiles();

    for (String file : files) {

        // loop through the instance triples probably stored in a file and generate all the triples matching the schema triples set
        try (BufferedReader r = this.getQueryResultsReader(file, compressed);) {

            Iterable<CSVRecord> records;

            if (compressed) {
                // ignore first row subject,predicate,object
                records = CSVFormat.DEFAULT.withHeader().withSkipHeaderRecord().parse(r);

            } else {
                records = CSVFormat.DEFAULT.parse(r);
            }

            // records will contain lots of duplicates
            //Set<String> inferredAlready = new HashSet<String>();

            try {

                Long term;

                for (CSVRecord record : records) {

                    //String values = ((select.size() == 1) ? record.get(0): StringUtils.join(record.values(), ','));

                    //if(!inferredAlready.contains(values)) {
                    //inferredAlready.add(values);

                    /*ETriple instanceTriple = new ETriple();
                    instanceTriple.setSubject(record.get(0));
                    instanceTriple.setPredicate(record.get(1));
                    instanceTriple.setObject(record.get(2));*/

                    ETriple instanceTriple = ETriple.fromCSV(record.values());

                    // TODO review for OWL ruleset
                    if (SchemaURIType.RDF_TYPE.id == instanceTriple.getPredicate()) {

                        term = instanceTriple.getObject(); // object

                    } else {

                        term = instanceTriple.getPredicate(); // predicate
                    }

                    Set<Triple> schemaTriples = schemaTerms.get(term);

                    if ((schemaTriples != null) && !schemaTriples.isEmpty()) {
                        productiveTerms.add(term);

                        for (Triple schemaTriple : schemaTriples) {
                            Rule rule = GenericRule.getRule(schemaTriple);
                            Triple inferredTriple = rule.head(schemaTriple, instanceTriple);

                            if (inferredTriple != null) {
                                writer.println(inferredTriple.toCsv());
                                inferredTriples++;
                            }
                        }
                    }

                    // this is just to avoid any memory issues
                    //if(inferredAlready.size() > MAX_CACHE) {
                    //   inferredAlready.clear();
                    //   log.info("Cleared cache of inferred terms");
                    //}
                    //} else {
                    //this.duplicates++;
                    //}

                }
            } catch (Exception e) {
                log.error("Failed to parse selected terms", e);
                throw new IOException(e);
                //failedTriples++;
            }
        }
    }

    log.info("Total Rows: " + queryResult.getStats().getTotalRows() + ", Total Processed Bytes: "
            + queryResult.getStats().getTotalProcessedGBytes() + " GB" + ", Inferred: " + inferredTriples
            + ", compressed = " + compressed);

    log.info("********************** Completed Inference Round **********************");

    return inferredTriples;
}

From source file:com.datascience.cascading.scheme.CsvScheme.java

/**
 * Creates a new CSV scheme with the given source and sink {@link cascading.tuple.Fields}.
 *
 * @param fields  The source and sink fields.
 * @param charset The character set with which to read and write CSV files.
 * @param strict  Indicates whether to parse records in strict parsing mode. When strict mode is disabled, single record
 *                parse errors will be caught and logged.
 * @see com.datascience.cascading.scheme.CsvScheme
 *//*w  w w  .j  a va  2s.  c  o m*/
public CsvScheme(Fields fields, Charset charset, boolean strict) {
    this(fields, fields, CSVFormat.DEFAULT, charset, strict);
}

From source file:com.team3637.service.TagServiceMySQLImpl.java

@Override
public void exportCSV(String outputFile) {
    List<Tag> data = getTags();
    FileWriter fileWriter = null;
    CSVPrinter csvFilePrinter = null;//from  w w w  .  j a v  a 2 s .com
    try {
        fileWriter = new FileWriter(outputFile);
        csvFilePrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withRecordSeparator("\n"));
        for (Tag tag : data) {
            List<Object> line = new ArrayList<>();
            for (Field field : Tag.class.getDeclaredFields()) {
                field.setAccessible(true);
                Object value = field.get(tag);
                line.add(value);
            }
            csvFilePrinter.printRecord(line);
        }
    } catch (IOException | IllegalAccessException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fileWriter != null) {
                fileWriter.flush();
                fileWriter.close();
            }
            if (csvFilePrinter != null) {
                csvFilePrinter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:ai.grakn.test.graql.analytics.ScalingTestIT.java

private CSVPrinter createCSVPrinter(String fileName) throws IOException {
    Appendable out = new PrintWriter(fileName, "UTF-8");
    return CSVFormat.DEFAULT.withHeader(headers.toArray(new String[0])).print(out);

}

From source file:com.team3637.service.TagServiceMySQLImpl.java

@Override
public void importCSV(String inputFile) {
    try {//from   w w w  .  j  av  a  2s .c o m
        String csvData = new String(Files.readAllBytes(FileSystems.getDefault().getPath(inputFile)));
        csvData = csvData.replaceAll("\\r", "");
        CSVParser parser = CSVParser.parse(csvData, CSVFormat.DEFAULT.withRecordSeparator("\n"));
        for (CSVRecord record : parser) {
            Tag tag = new Tag();
            tag.setId(Integer.parseInt(record.get(0)));
            tag.setTag(record.get(1));
            tag.setType(record.get(2));
            tag.setCounter(record.get(3));
            tag.setInTable(record.get(4).equals("1") || record.get(4).toLowerCase().equals("true"));
            tag.setRequiesEval(record.get(5).equals("1") || record.get(5).toLowerCase().equals("true"));
            tag.setExpression(record.get(6).replace("\n", ""));
            if (checkForTag(tag))
                update(tag);
            else
                create(tag);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.archimatetool.csv.importer.CSVImporter.java

/**
 * Get all records for a CSV file.//from   w w  w  .  ja v a2s .c o  m
 * This is a brute-force approach to try with a comma delimiter first. If that fails then
 * try a semicolon, and if that fails, a tab.
 * 
 * @param file The file to open
 * @return Records, which may be empty but never null
 * @throws IOException
 */
List<CSVRecord> getRecords(File file) throws IOException {
    List<CSVRecord> records = new ArrayList<CSVRecord>();
    CSVParser parser = null;

    String errorMessage = "invalid char between encapsulated token and delimiter"; //$NON-NLS-1$

    try {
        parser = new CSVParser(new FileReader(file), CSVFormat.DEFAULT);
        records = parser.getRecords();
    } catch (IOException ex) {
        if (parser != null) {
            parser.close();
        }
        if (ex.getMessage() != null && ex.getMessage().contains(errorMessage)) {
            try {
                parser = new CSVParser(new FileReader(file), CSVFormat.DEFAULT.withDelimiter(';'));
                records = parser.getRecords();
            } catch (IOException ex2) {
                if (parser != null) {
                    parser.close();
                }
                if (ex2.getMessage() != null && ex2.getMessage().contains(errorMessage)) {
                    parser = new CSVParser(new FileReader(file), CSVFormat.DEFAULT.withDelimiter('\t'));
                    records = parser.getRecords();
                } else {
                    throw ex2;
                }
            }
        } else {
            throw ex;
        }
    } finally {
        if (parser != null) {
            parser.close();
        }
    }

    return records;
}

From source file:com.datascience.cascading.scheme.CsvScheme.java

/**
 * Creates a new CSV scheme with the given source and sink {@link cascading.tuple.Fields}.
 * <p>//from w w  w  .  j av  a 2s.c  o  m
 * Strict mode is enabled when using this constructor.
 * <p>
 * The CSV input/output encoding set defaults to {@code UTF-8}
 *
 * @param sourceFields The source fields.
 * @param sinkFields   The sink fields.
 * @see com.datascience.cascading.scheme.CsvScheme
 */
public CsvScheme(Fields sourceFields, Fields sinkFields) {
    this(sourceFields, sinkFields, CSVFormat.DEFAULT, StandardCharsets.UTF_8, true);
}

From source file:com.datascience.cascading.scheme.CsvScheme.java

/**
 * Creates a new CSV scheme with the given source and sink {@link cascading.tuple.Fields}.
 * <p>/*from  ww  w  .j a va  2 s . c  om*/
 * Strict mode is enabled when using this constructor.
 *
 * @param sourceFields The source fields.
 * @param sinkFields   The sink fields.
 * @param charset      The character set with which to read and write CSV files.
 * @see com.datascience.cascading.scheme.CsvScheme
 */
public CsvScheme(Fields sourceFields, Fields sinkFields, Charset charset) {
    this(sourceFields, sinkFields, CSVFormat.DEFAULT, charset, true);
}

From source file:mekhq.campaign.finances.Finances.java

public String exportFinances(String path, String format) {
    String report;//from   www .ja  v a 2  s.co m

    try {
        BufferedWriter writer = Files.newBufferedWriter(Paths.get(path));
        CSVPrinter csvPrinter = new CSVPrinter(writer,
                CSVFormat.DEFAULT.withHeader("Date", "Category", "Description", "Amount", "RunningTotal"));
        SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");

        int running_total = 0;
        for (int i = 0; i < transactions.size(); i++) {
            running_total += transactions.get(i).getAmount();
            csvPrinter.printRecord(df.format(transactions.get(i).getDate()),
                    transactions.get(i).getCategoryName(), transactions.get(i).getDescription(),
                    transactions.get(i).getAmount(), running_total);
        }

        csvPrinter.flush();
        csvPrinter.close();

        report = transactions.size() + " " + resourceMap.getString("FinanceExport.text");
    } catch (IOException ioe) {
        MekHQ.getLogger().log(getClass(), "exportFinances", LogLevel.INFO,
                "Error exporting finances to " + format);
        report = "Error exporting finances. See log for details.";
    }

    return report;
}