Example usage for org.apache.commons.csv CSVParser CSVParser

List of usage examples for org.apache.commons.csv CSVParser CSVParser

Introduction

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

Prototype

public CSVParser(final Reader reader, final CSVFormat format) throws IOException 

Source Link

Document

Customized CSV parser using the given CSVFormat

If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .

Usage

From source file:de.upb.wdqa.wdvd.labels.CorpusLabelReader.java

/**
 *   Initializes the label reader./*from ww  w.j av  a2  s .  com*/
 */
public void startReading() {
    try {
        BufferedReader csvReader = new BufferedReader(new InputStreamReader(labelsStream, "UTF-8"),
                BUFFER_SIZE);

        csvParser = new CSVParser(csvReader, CSVFormat.RFC4180.withHeader(FILE_HEADER));
        iterator = csvParser.iterator();

        CSVRecord headerRecord = iterator.next();

        for (int i = 0; i < FILE_HEADER.length; i++) {
            if (!FILE_HEADER[i].equals(headerRecord.get(i))) {
                throw new IOException("The header of the CSV file is wrong.");
            }
        }
    } catch (IOException e) {
        logger.error("", e);
        finishReading();
    }
}

From source file:com.datascience.hadoop.CsvRecordReader.java

public CsvRecordReader(Reader reader, CSVFormat format, long length, boolean strict) throws IOException {
    this.length = length;
    this.strict = strict;
    parser = new CSVParser(reader, format);
    iterator = parser.iterator();/*from   w w w.j a  v a  2 s .c  o  m*/
    if (parser.getHeaderMap() == null) {
        colLength = null;
    } else {
        colLength = parser.getHeaderMap().size();
    }
}

From source file:edu.emory.mathcs.nlp.zzz.CSVRadiology.java

public void tokenize(String inputFile, int outputStart) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    String inputPath = FileUtils.getPath(inputFile) + "/";
    List<CSVRecord> records = parser.getRecords();
    Tokenizer tokenizer = new EnglishTokenizer();

    P_BEFORE = new ArrayList<>();
    P_AFTER = new ArrayList<>();
    for (String s : BEFORE)
        P_BEFORE.add(new Pair<>(Pattern.compile(s), "\n" + s));
    for (String s : AFTER)
        P_AFTER.add(new Pair<>(Pattern.compile(s), s + "\n"));

    for (int i = 0; i < records.size(); i++) {
        PrintStream fout = IOUtils.createBufferedPrintStream(getOuputFilename(inputPath, i + outputStart));

        for (List<Token> tokens : tokenizer.segmentize(records.get(i).get(0)))
            print(fout, tokens);//from   ww w .ja v a 2s .co  m

        fout.close();
    }

    parser.close();
}

From source file:com.twentyn.TargetMolecule.java

public static List<TargetMolecule> loadTargets(File inputFile) throws IOException {
    List<TargetMolecule> results = new ArrayList<>();
    try (CSVParser parser = new CSVParser(new FileReader(inputFile), TSV_FORMAT)) {
        for (CSVRecord record : parser) {
            results.add(TargetMolecule.fromCSVRecord(record));
        }//from ww w . j a  v  a  2  s . co m
    }
    return results;
}

From source file:com.act.utils.TSVParser.java

public void parse(InputStream inStream) throws IOException {
    List<Map<String, String>> results = new ArrayList<>();
    try (CSVParser parser = new CSVParser(new InputStreamReader(inStream), TSV_FORMAT)) {
        headerMap = parser.getHeaderMap();
        Iterator<CSVRecord> iter = parser.iterator();
        while (iter.hasNext()) {
            CSVRecord r = iter.next();/*from  w w w  . j av a  2s.c  o  m*/
            results.add(r.toMap());
        }
    }
    this.results = results;
}

From source file:com.itemanalysis.jmetrik.data.Scorer.java

private void readScoringFile(String fileName) throws IOException {
    File f = new File(fileName);
    CSVParser parser = null;// ww w.  jav  a 2  s  .c o m
    Reader reader = null;
    GenericItemScoring itemScoring = null;
    SpecialDataCodes specialCodes = null;
    String name = "";
    String option = "";
    int score = 0;
    try {
        reader = new InputStreamReader(new BOMInputStream(new FileInputStream(f)), "UTF-8");
        parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());

        for (CSVRecord csvRecord : parser) {
            name = csvRecord.get("name");
            option = csvRecord.get("option");
            score = Integer.parseInt(csvRecord.get("score"));

            itemScoring = new GenericItemScoring(name);
            itemScoring.addCategory(option, score);

            specialCodes = new SpecialDataCodes();

            if (csvRecord.isMapped("missing"))
                specialCodes.setMissingDataCode(csvRecord.get("missing"));
            if (csvRecord.isMapped("missing score"))
                specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("missing score")));

            if (csvRecord.isMapped("notreached"))
                specialCodes.setMissingDataCode(csvRecord.get("notreached"));
            if (csvRecord.isMapped("notreached score"))
                specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("notreached score")));

            if (csvRecord.isMapped("omitted"))
                specialCodes.setMissingDataCode(csvRecord.get("omitted"));
            if (csvRecord.isMapped("omitted score"))
                specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("omitted score")));

            scoring.put(name, itemScoring);
        }
    } catch (IOException ex) {
        throw (ex);
    } finally {
        parser.close();
        reader.close();
    }

}

From source file:com.leadscope.commanda.sources.CSVSource.java

@Override
public Stream<CSVRecord> stream(InputStream inputStream) {
    try {/* w w  w  . j ava  2  s  . c  om*/
        CSVParser parser = new CSVParser(new InputStreamReader(inputStream, charset), format);
        return StreamSupport.stream(parser.spliterator(), false);
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.clemson.lph.civet.addons.VspsCviFile.java

private void saveme(Window parent, File fIn) {
    try {//from   w ww .  jav a 2s.  c  o  m
        File fOut = fixCSV(fIn);
        CSVParser parserIn = new CSVParser(new FileReader(fOut), CSVFormat.EXCEL);
        parser = new LabeledCSVParser(parserIn);
        aCols = parser.getNext();
    } catch (FileNotFoundException e) {
        logger.error(e.getMessage() + "\nCould not read file: " + fIn.getName());
    } catch (IOException e) {
        logger.error(e.getMessage() + "\nCould not read file: " + fIn.getName());
    }
    InsertVspsCviThread thread = new InsertVspsCviThread(parent, this);
    thread.start();
}

From source file:edu.emory.mathcs.nlp.zzz.CSVSentiment.java

public void toTSV(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    PrintStream fout = IOUtils.createBufferedPrintStream(inputFile + ".tsv");
    List<CSVRecord> records = parser.getRecords();
    List<Token> tokens;/*ww w  .  jav  a 2 s.co m*/
    CSVRecord record;
    int label;

    System.out.println(inputFile);

    for (int i = 0; i < records.size(); i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        label = toIntLabel(record.get(0));
        tokens = tokenizer.tokenize(record.get(6));
        fout.println(label + "\t" + Joiner.join(tokens, " ", Token::getWordForm));
    }

    fout.close();
    parser.close();
}

From source file:br.edimarmanica.weir2.check.CheckDistanceExpectedMapping.java

private String getMasterRule(Site site, Attribute attribute) {
    try (Reader in = new FileReader(Paths.PATH_INTRASITE + "/" + site.getPath() + "/result.csv")) {
        try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) {

            for (CSVRecord record : parser) {
                if (record.get("ATTRIBUTE").equals(attribute.getAttributeID())) {
                    if (record.get("RULE").equals("Attribute not found")) {
                        return null;
                    }/*from  ww  w.jav a 2s.co  m*/
                    return record.get("RULE");
                }
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CheckDistanceExpectedMapping.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CheckDistanceExpectedMapping.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}