Example usage for org.apache.commons.csv CSVRecord get

List of usage examples for org.apache.commons.csv CSVRecord get

Introduction

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

Prototype

public String get(final String name) 

Source Link

Document

Returns a value by name.

Usage

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.annotator.OnlyFilesMatchingPredictionsReader.java

public static List<Sequence> extractSequences(File tokenLevelPredictionsCsvFile1) throws IOException {
    List<Sequence> result = new ArrayList<>();
    // load the CSV
    CSVParser csvParser = new CSVParser(new FileReader(tokenLevelPredictionsCsvFile1),
            CSVFormat.DEFAULT.withCommentMarker('#'));

    String prevSeqId = null;//from   w  w  w  .j av a  2  s . c o  m
    Sequence currentSequence = new Sequence();

    int tokenCounter = 0;

    for (CSVRecord csvRecord : csvParser) {
        // row for particular instance (token)
        String predictedTag = csvRecord.get(1);
        String token = csvRecord.get(2);
        String seqId = csvRecord.get(3);

        TokenEntry tokenEntry = new TokenEntry(token, predictedTag);

        // if the token belongs to the previous seqId, add it to the sequence
        if (prevSeqId == null || seqId.equals(prevSeqId)) {
            currentSequence.getTokens().add(tokenEntry);
        } else {
            // otherwise start a new sequence
            result.add(currentSequence);

            currentSequence = new Sequence();
            currentSequence.getTokens().add(tokenEntry);
        }

        prevSeqId = seqId;
        tokenCounter++;
    }

    // don't forget to add the last sequence
    result.add(currentSequence);

    System.out.println("Loaded " + result.size() + " sequences with total " + tokenCounter + " tokens.");

    return result;
}

From source file:net.iaeste.iws.core.transformers.CSVTransformer.java

private static Date convertDate(final Map<String, String> errors, final OfferFields field,
        final CSVRecord record) {
    final String value = record.get(field.getField());
    Date result = null;/*from   w ww.  j  a  va  2s  . co m*/

    if ((value != null) && !value.isEmpty()) {
        try {
            final DateFormat formatter = new SimpleDateFormat(DATE_FORMAT, DEFAULT_LOCALE);
            result = new Date(formatter.parse(value));
        } catch (ParseException e) {
            LOG.debug(e.getMessage(), e);
            errors.put(field.getField(), e.getMessage());
        }
    }

    return result;
}

From source file:com.bigtester.ate.tcg.controller.TrainingFileDB.java

/**
 * Parses the line./* w w  w.ja  va  2  s .c o  m*/
 *
 * @param line
 *            the line
 * @return the user input training record
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static UserInputTrainingRecord parseLine(String line) throws IOException {
    CSVParser lineParser = CSVParser.parse(line, TrainingFileDB.getCSVFormat());
    List<CSVRecord> csvRecords = lineParser.getRecords();
    UserInputTrainingRecord retVal = null; // NOPMD
    for (CSVRecord record : csvRecords) {
        if (null != record) {
            String temp = record.get(0);
            String temp2 = record.get(1);
            if (null == temp)
                temp = "";
            if (null == temp2)
                temp2 = "";
            retVal = new UserInputTrainingRecord(temp, // NOPMD
                    temp2);
        }
    }
    if (null == retVal)
        throw new IOException();
    return retVal;
}

From source file:net.iaeste.iws.core.transformers.CSVTransformer.java

static void transformString(final Map<String, String> errors, final Verifiable obj, final OfferFields field,
        final CSVRecord record) {
    invokeMethodOnObject(errors, obj, field, record.get(field.getField()));
}

From source file:net.iaeste.iws.core.transformers.CSVTransformer.java

static <T extends Enum<T>> void transformEnum(final Map<String, String> errors, final Verifiable obj,
        final OfferFields field, final CSVRecord record, final Class<T> enumType) {
    final String value = record.get(field.getField());

    if ((value != null) && !value.isEmpty()) {
        try {/*from   ww  w .  j av a  2  s  . c  o  m*/
            final T theEnum = Enum.valueOf(enumType, value.toUpperCase(DEFAULT_LOCALE));
            invokeMethodOnObject(errors, obj, field, theEnum);
        } catch (IllegalArgumentException e) {
            LOG.debug(e.getMessage(), e);
            errors.put(field.getField(), e.getMessage());
        }
    }
}

From source file:net.iaeste.iws.core.transformers.CSVTransformer.java

static <E extends Enum<E> & Descriptable<E>> void transformDescriptableEnumSet(final Map<String, String> errors,
        final Verifiable obj, final OfferFields field, final CSVRecord record, final Class<E> enumType) {
    final String value = record.get(field.getField());

    if ((value != null) && !value.isEmpty()) {
        try {//from w w  w.j a  v  a2  s. c  o  m
            final Set<E> set = CollectionTransformer.explodeEnumSet(enumType, value);
            invokeMethodOnObject(errors, obj, field, set);
        } catch (IllegalArgumentException e) {
            LOG.debug(e.getMessage(), e);
            errors.put(field.getField(), e.getMessage());
        }
    }
}

From source file:net.iaeste.iws.core.transformers.CSVTransformer.java

static <E extends Enum<E> & Descriptable<E>> void transformDescriptableEnum(final Map<String, String> errors,
        final Verifiable obj, final OfferFields field, final CSVRecord record, final Class<E> enumType) {
    final String value = record.get(field.getField());

    if ((value != null) && !value.isEmpty()) {
        try {//from  w w  w .  j a  v a2  s . c om
            final E theEnum = EnumUtil.valueOf(enumType, value);
            invokeMethodOnObject(errors, obj, field, theEnum);
        } catch (IllegalArgumentException e) {
            LOG.debug(e.getMessage(), e);
            errors.put(field.getField(), e.getMessage());
        }
    }
}

From source file:net.iaeste.iws.core.transformers.CSVTransformer.java

static void transformTypeOfWork(final Map<String, String> errors, final Verifiable obj, final OfferFields field,
        final CSVRecord record) {
    final Boolean typeR = convertBoolean(record.get(OfferFields.WORK_TYPE_P.getField()));
    final Boolean typeO = convertBoolean(record.get(OfferFields.WORK_TYPE_R.getField()));
    final Boolean typeF = convertBoolean(record.get(OfferFields.WORK_TYPE_W.getField()));

    if (convertBoolean(record.get(OfferFields.WORK_TYPE_N.getField()))) {
        LOG.info("Ignoring the TypeOfWork 'N'.");
    }/* ww w . java 2 s  . c  om*/

    // Using the Boolean comparison as it reduces the NPath complexity
    final int sum = Boolean.compare(typeR, false) + Boolean.compare(typeO, false)
            + Boolean.compare(typeF, false);

    if (sum > 1) {
        errors.put(field.getField(), "Multiple TypeOfWork is set, only one is allowed.");
    } else if (sum == 0) {
        errors.put(field.getField(), "No TypeOfWork defined.");
    } else {
        final TypeOfWork value;
        if (typeR) {
            value = TypeOfWork.R;
        } else if (typeO) {
            value = TypeOfWork.O;
        } else {
            value = TypeOfWork.F;
        }

        invokeMethodOnObject(errors, obj, field, value);
    }
}

From source file:com.hurence.logisland.processor.TimeSeriesCsvLoader.java

/**
 * CSV reader that waits for a 2 columns csv files with or without a header.
 * If less than 2 columns ==> exception, otherwise, the 3rd and following columns are ignored
 *
 * @param in/* w w  w .j  a  v  a2s.c  o  m*/
 * @param hasHeader
 * @param inputDatetimeFormat input date format
 * @return
 * @throws IOException
 * @throws IllegalArgumentException
 * @throws ArrayIndexOutOfBoundsException
 */
public static List<Record> load(Reader in, boolean hasHeader, DateTimeFormatter inputDatetimeFormat)
        throws IOException {

    List<Record> records = new ArrayList<>();
    for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
        try {
            if (!hasHeader) {
                StandardRecord event = new StandardRecord("sensors");
                event.setField(TIMESTAMP_KEY, FieldType.LONG, inputDatetimeFormat.withZone(DateTimeZone.UTC)
                        .parseDateTime(record.get(0)).getMillis());
                event.setField(VALUE_KEY, FieldType.DOUBLE, Double.parseDouble(record.get(1)));

                records.add(event);
            } else {
                TIMESTAMP_KEY = record.get(0);
                VALUE_KEY = record.get(1);
            }

            hasHeader = false;
        } catch (Exception e) {
            logger.error("Parsing error " + e.getMessage());
            throw new RuntimeException("parsing error", e);
        }
    }

    return records;
}

From source file:com.house365.build.util.CsvUtil.java

/**
 * ?CSV.//from w ww.  ja va 2 s .co m
 *
 * @param csvFile
 * @param headerRow ,1
 * @return Csv
 */
private static String[] getCsvHeader(File csvFile, int headerRow) throws Exception {
    String[] headNameStrings = null;
    AutoDetectReader reader = null;
    try {
        reader = new AutoDetectReader(new FileInputStream(csvFile));
        Iterable<CSVRecord> csvRecords = CSVFormat.EXCEL.parse(reader);
        int i = 1;
        for (CSVRecord record : csvRecords) {
            if (i == headerRow) {
                headNameStrings = new String[record.size()];
                for (int j = 0; j < record.size(); j++) {
                    headNameStrings[j] = record.get(j);
                }
                break;
            }
            i = i + 1;
        }
    } catch (Exception e) {
        throw e;
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (Exception e) {
        }
    }
    return headNameStrings;
}