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:com.ibm.watson.app.qaclassifier.tools.GenerateTrainingAndPopulationData.java

/**
 * Reads in the question input file and creates a POJO for each question it finds.  If the label associated with 
 * the question does not exist in the previously read in answer store then it is skipped
 * //from   www.j  a  v  a  2s.  co m
 * @return TrainingData - full POJO of the training data
 */
private static NLClassifierTrainingData readQuestionInput(List<ManagedAnswer> store) {
    NLClassifierTrainingData data = null;

    try (FileReader reader = new FileReader(questionInput);
            CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL)) {
        // read in the csv file and get the records            
        List<CSVRecord> records = parser.getRecords();

        // now we can create the training data because we have read the records
        data = new NLClassifierTrainingData();
        data.setLanguage("en");
        for (CSVRecord r : records) {
            // order is: QuestionText, LabelId

            // check for existence of label first, if not there, skip                
            // we only add the training instance if there is an associated answer
            String text = r.get(0);
            String label = r.get(1);
            if (labelHasAnswer(label, store)) {
                data.addTrainingData(text, label);
            } else {
                System.out.println(MessageKey.AQWQAC24009E_label_not_found_in_answer_store_including_2
                        .getMessage(text, label).getFormattedMessage());
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return data;
}

From source file:com.siemens.sw360.commonIO.ConvertRecord.java

public static List<Risk> convertRisks(List<CSVRecord> records, Map<Integer, RiskCategory> categories) {
    List<Risk> list = new ArrayList<>(records.size());

    for (CSVRecord record : records) {
        int id = Integer.parseInt(record.get(0));
        int catId = Integer.parseInt(record.get(1));
        String text = record.get(2);

        Risk risk = new Risk().setRiskId(id).setText(text);
        risk.setCategory(categories.get(catId));

        list.add(risk);//from w  w w. j a  v a2  s  .  c om
    }

    return list;
}

From source file:com.siemens.sw360.commonIO.ConvertRecord.java

public static List<License> fillLicenses(List<CSVRecord> records, Map<Integer, LicenseType> licenseTypeMap,
        Map<Integer, Todo> todoMap, Map<Integer, Risk> riskMap, Map<String, Set<Integer>> licenseTodo,
        Map<String, Set<Integer>> licenseRisk) {
    List<License> licenses = new ArrayList<>(records.size());

    for (CSVRecord record : records) {
        String identifier = record.get(0);
        String fullname = record.get(1);

        License license = new License().setShortname(identifier).setFullname(fullname);

        String typeString = record.get(2);
        if (!Strings.isNullOrEmpty(typeString) && !"NULL".equals(typeString)) {
            Integer typeId = Integer.parseInt(typeString);
            LicenseType licenseType = licenseTypeMap.get(typeId);
            license.setLicenseType(licenseType);
        }/*from   w  w  w  . jav  a  2s.  com*/

        String gplv2CompatString = record.get(3);
        if (!Strings.isNullOrEmpty(typeString) && !"NULL".equals(gplv2CompatString)) {
            boolean gplv2Compat = parseBoolean(gplv2CompatString);
            license.setGPLv2Compat(gplv2Compat);
        }

        String gplv3CompatString = record.get(4);
        if (!Strings.isNullOrEmpty(typeString) && !"NULL".equals(gplv3CompatString)) {
            boolean gplv3Compat = parseBoolean(gplv3CompatString);
            license.setGPLv3Compat(gplv3Compat);
        }

        String reviewdate = record.get(5);
        license.setReviewdate(ConvertUtil.parseDate(reviewdate));

        String text = record.get(6);
        license.setText(text);

        // Add all risks
        Set<Integer> riskIds = licenseRisk.get(identifier);
        if (riskIds != null) {
            for (int riskId : riskIds) {
                Risk risk = riskMap.get(riskId);
                if (risk != null) {
                    license.addToRiskDatabaseIds(risk.getId());
                }
            }
        }

        // Add all todos
        Set<Integer> todoIds = licenseTodo.get(identifier);
        if (todoIds != null) {
            for (int todoId : todoIds) {
                Todo todo = todoMap.get(todoId);
                if (todo != null) {
                    license.addToTodoDatabaseIds(todo.getId());
                }
            }
        }

        licenses.add(license);
    }

    return licenses;
}

From source file:net.sourceforge.ganttproject.io.GanttCSVOpen.java

private static RecordGroup createTaskRecordGroup(final TaskManager taskManager,
        final HumanResourceManager resourceManager) {
    return new RecordGroup(Sets.newHashSet(getFieldNames(TaskFields.values()))) {
        private Map<Task, String> myAssignmentMap = Maps.newHashMap();
        private Map<Task, String> myPredecessorMap = Maps.newHashMap();
        private Map<String, Task> myTaskIdMap = Maps.newHashMap();

        @Override// www  .  j a  v  a2 s.  com
        public void setHeader(List<String> header) {
            super.setHeader(header);
            createCustomProperties(getCustomFields(), taskManager.getCustomPropertyManager());
        }

        @Override
        protected void process(CSVRecord record) {
            assert record.size() > 0;
            // Create the task
            TaskManager.TaskBuilder builder = taskManager.newTaskBuilder()
                    .withName(record.get(TaskFields.NAME.toString()))
                    .withStartDate(language.parseDate(record.get(TaskFields.BEGIN_DATE.toString())))
                    .withWebLink(record.get(TaskFields.WEB_LINK.toString()))
                    .withNotes(record.get(TaskFields.NOTES.toString()));
            if (record.get(TaskDefaultColumn.DURATION.getName()) != null) {
                builder = builder.withDuration(
                        taskManager.createLength(record.get(TaskDefaultColumn.DURATION.getName())));
            }
            if (Objects.equal(record.get(TaskFields.BEGIN_DATE.toString()),
                    record.get(TaskFields.END_DATE.toString()))
                    && "0".equals(record.get(TaskDefaultColumn.DURATION.getName()))) {
                builder = builder.withLegacyMilestone();
            }
            if (!Strings.isNullOrEmpty(record.get(TaskFields.COMPLETION.toString()))) {
                builder = builder
                        .withCompletion(Integer.parseInt(record.get(TaskFields.COMPLETION.toString())));
            }
            Task task = builder.build();

            if (record.get(TaskDefaultColumn.ID.getName()) != null) {
                myTaskIdMap.put(record.get(TaskDefaultColumn.ID.getName()), task);
            }
            myAssignmentMap.put(task, record.get(TaskFields.RESOURCES.toString()));
            myPredecessorMap.put(task, record.get(TaskDefaultColumn.PREDECESSORS.getName()));
            for (String customField : getCustomFields()) {
                String value = record.get(customField);
                CustomPropertyDefinition def = taskManager.getCustomPropertyManager()
                        .getCustomPropertyDefinition(customField);
                if (def == null) {
                    GPLogger.logToLogger(
                            "Can't find custom field with name=" + customField + " value=" + value);
                    continue;
                }
                if (value != null) {
                    task.getCustomValues().addCustomProperty(def, value);
                }
            }
        }

        @Override
        protected void postProcess() {
            if (resourceManager != null) {
                Map<String, HumanResource> resourceMap = Maps.uniqueIndex(resourceManager.getResources(),
                        new Function<HumanResource, String>() {
                            @Override
                            public String apply(HumanResource input) {
                                return input.getName();
                            }
                        });
                for (Entry<Task, String> assignment : myAssignmentMap.entrySet()) {
                    String[] names = assignment.getValue().split(";");
                    for (String name : names) {
                        HumanResource resource = resourceMap.get(name);
                        if (resource != null) {
                            assignment.getKey().getAssignmentCollection().addAssignment(resource);
                        }
                    }
                }
            }
            for (Entry<Task, String> predecessor : myPredecessorMap.entrySet()) {
                if (predecessor.getValue() == null) {
                    continue;
                }
                String[] ids = predecessor.getValue().split(";");
                for (String id : ids) {
                    Task dependee = myTaskIdMap.get(id);
                    if (dependee != null) {
                        try {
                            taskManager.getDependencyCollection().createDependency(predecessor.getKey(),
                                    dependee);
                        } catch (TaskDependencyException e) {
                            GPLogger.logToLogger(e);
                        }
                    }
                }
            }
        }
    };
}

From source file:com.hack23.cia.service.external.vdem.impl.VdemServiceImpl.java

/**
 * Adds the question data to list.//  w  w w.  j  av a 2 s .  com
 *
 * @param list
 *            the list
 * @param record
 *            the record
 * @param countryName
 *            the country name
 * @param countryId
 *            the country id
 * @param countryTextId
 *            the country text id
 * @param year
 *            the year
 * @param gapStart
 *            the gap start
 * @param gapEnd
 *            the gap end
 * @param codingEnd
 *            the coding end
 * @param cowCode
 *            the cow code
 * @param question
 *            the question
 */
private static void addQuestionDataToList(final List<CountryQuestionData> list, final CSVRecord record,
        final String countryName, final String countryId, final String countryTextId, final String year,
        final String gapStart, final String gapEnd, final String codingEnd, final String cowCode,
        final Question question) {
    if (question.getQuestionId() != null) {

        if (record.isMapped(question.getTag())) {

            try {
                final String questionValue = record.get(question.getTag());

                if (questionValue != null && StringUtils.hasText(questionValue)) {
                    final CountryQuestionData countryQuestionData = new CountryQuestionData();
                    final CountryQuestionDataEmbeddedId embeddedId = new CountryQuestionDataEmbeddedId();

                    embeddedId.setCountryName(countryName);
                    embeddedId.setCountryId(countryId);
                    embeddedId.setIndicatorId(question.getTag());
                    embeddedId.setYear(Integer.parseInt(year));
                    embeddedId.setCountryId(countryId);

                    countryQuestionData.setEmbeddedId(embeddedId);

                    countryQuestionData.setCountryTextId(countryTextId);
                    countryQuestionData.setGapstart(gapStart);
                    countryQuestionData.setGapend(gapEnd);
                    countryQuestionData.setCodingend(codingEnd);
                    countryQuestionData.setCowCode(cowCode);
                    countryQuestionData.setQuestionValue(questionValue.trim());

                    list.add(countryQuestionData);
                }
            } catch (final RuntimeException e) {
                LOGGER.warn("Missing value for:" + question.getTag(), e);
            }
        }
    }
}

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

/**
 * ?CSV.//from   ww  w  .j a va 2 s.  c  om
 *
 * @param channelsFile
 * @param headerRow
 * @param isPrint      ??..
 * @return ?Csv?
 * @throws FileNotFoundException
 */
public static ArrayList<LinkedHashMap<String, String>> readCsvChannels(File channelsFile, int headerRow,
        boolean isPrint) throws Exception {
    try {
        ArrayList<LinkedHashMap<String, String>> csvContent = new ArrayList<>();
        LinkedHashMap<String, String> rowContent;
        ArrayList<String> headerNames = null;
        if (headerRow > 0) {
            String[] csvHeader = getCsvHeader(channelsFile, headerRow);
            List<String> list = Arrays.asList(csvHeader);
            headerNames = new ArrayList<>(list);
        }
        AutoDetectReader reader = null;
        try {
            reader = new AutoDetectReader(new FileInputStream(channelsFile));
            Iterable<CSVRecord> csvRecords = CSVFormat.EXCEL.parse(reader);
            int i = 0;
            for (CSVRecord record : csvRecords) {
                i++;
                if (i <= headerRow) {
                    continue;
                }
                if (headerNames == null) {
                    headerNames = new ArrayList<>();
                    for (i = 0; i < record.size(); i++) {
                        headerNames.add(i + "");
                    }
                }
                rowContent = new LinkedHashMap<>();
                for (i = 0; i < record.size(); i++) {
                    rowContent.put(headerNames.get(i), record.get(i));
                }
                csvContent.add(rowContent);
            }
        } finally {
            try {
                if (reader != null)
                    reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (isPrint) {
            printlnCsv(csvContent);
        }
        return csvContent;
    } catch (IOException e) {
        throw e;
    } catch (TikaException e) {
        throw e;
    }
}

From source file:com.google.cloud.genomics.dockerflow.args.ArgsTableBuilder.java

/**
 * Load the workflow arguments from a CSV file. The header of the CSV contains the input or output
 * parameter names. Each row contains the workflow args for a single run. To run 100 instances of
 * a workflow concurrently, create a CSV with a header row plus 100 rows for each set of
 * parameters.//from w  ww .  ja v  a2s  .  com
 *
 * <p>Columns by default are input parameters, passed as environment variables to the Docker
 * script. For file parameters, you can prefix the column header with "<" for input or ">" for
 * output. For clarity, you can also prefix the regular input parameters as "<", if you like.
 *
 * <p>The column header can also be "logging", which is a reserved name for the logging path.
 *
 * @param csvFile CSV file (RFC4180) that's local or in GCS
 * @return a map with the key being the clientId
 * @throws IOException
 */
static Map<String, WorkflowArgs> loadCsv(String csvFile) throws IOException {
    Map<String, WorkflowArgs> retval = new HashMap<String, WorkflowArgs>();

    String csv = FileUtils.readAll(csvFile);
    CSVParser parser = CSVParser.parse(csv, CSVFormat.RFC4180);

    // Parse header
    List<String> header = null;

    int row = 0;

    // Parse by row
    for (CSVRecord csvRecord : parser) {
        ArgsBuilder args = ArgsBuilder.of(String.valueOf(row));

        LOG.debug(StringUtils.toJson(csvRecord));

        // Parse header the first time
        if (row == 0) {
            header = new ArrayList<String>();
            for (String col : csvRecord) {
                header.add(col);
            }
        } else {
            // Set parameter defined in each column
            for (int col = 0; col < header.size(); ++col) {
                String name = header.get(col);
                String val = csvRecord.get(col);

                if (name.startsWith(PREFIX_INPUT)) {
                    name = name.replace(PREFIX_INPUT, "");
                    args.input(name, val);
                } else if (name.startsWith(PREFIX_OUTPUT)) {
                    name = name.replace(PREFIX_OUTPUT, "");
                    args.output(name, val);
                } else if (LOGGING.equals(name)) {
                    args.logging(val);
                } else {
                    args.input(name, val);
                }
            }
            WorkflowArgs a = args.build();
            a.setRunIndex(row);
            retval.put(a.getClientId(), a);
        }
        ++row;
    }
    return retval;
}

From source file:it.incipict.tool.profiles.util.ProfilesToolUtils.java

public static List<Survey> parseSurvey(File file) throws ProfilesToolException {
    List<Survey> surveys = new ArrayList<Survey>();

    Iterable<CSVRecord> records;
    try {/*from  w  w  w.j  ava 2s. c  om*/
        Reader in = new FileReader(file.getCanonicalPath());
        records = CSVFormat.RFC4180.parse(in);
    } catch (IOException e) {
        throw new ProfilesToolException("error while reading files.", e);
    }

    // loop all surveys in CSV line by line
    // note: a single CSV can contain more surveys
    for (CSVRecord record : records) {
        int line = (int) record.getRecordNumber();
        // skip the first line because it simply contains the headers
        if (line > 1) {
            Survey survey = new Survey();
            List<String> answers = new ArrayList<String>();
            List<String> textualAnswers = new ArrayList<String>();
            // in the Survey model we can define a "RECORD OFFSET" by which
            // the parser must start
            for (int i = survey.RECORD_OFFSET; i < record.size(); i++) {
                String r = record.get(i);

                // if the "r" is empty the user has not responded
                // skip to the next answers
                if (r.isEmpty()) {
                    continue;
                }
                // if "r" isn't a numerical value and it is different than
                // ZERO_STRING (Ref. Survey model)
                // Note: the ZERO_STRING is defined in Survey Class. Its
                // value is Zero.
                if ((!r.matches("\\d*") && r.compareTo(survey.ZERO_STRING) != 0)) {
                    // Otherwise the answer is added to a list of Strings.
                    // this list will appended in queue. (*)
                    String[] str = r.split(";");
                    for (String s : str) {
                        textualAnswers.add("\"" + s + "\"");
                    }
                }
                // if "r" is a numeric value, simply add it in the answers
                // list.
                if (r.compareTo(survey.ZERO_STRING) == 0) {
                    answers.add("0");
                }
                if (r.matches("\\d*")) {
                    answers.add(r);
                }
            }
            // add the textual answers in queue (*)
            answers.addAll(textualAnswers);

            String fname = file.getName();
            survey.setProfile(fname.substring(0, fname.lastIndexOf('.')));
            survey.setAnswers(answers);
            surveys.add(survey);
        }
    }
    return (surveys != null) ? surveys : null;
}

From source file:com.mycompany.match.gender.app.Person.java

public Person(CSVRecord record) {

    this.firstName = record.get(0).toLowerCase();
    this.lastName = record.get(1).toLowerCase();
    this.companyName = record.get(2);
    this.address = record.get(3);
    this.city = record.get(4);
    this.state = record.get(5);
    this.postCode = record.get(6);
    this.phone1 = record.get(7);
    this.phone2 = record.get(8);
    this.email = record.get(9).toLowerCase();
    this.web = record.get(10);
}

From source file:common.data.DocumentMetadata.java

public DocumentMetadata(CSVRecord record) {
    filename = record.get("file");
    title = record.get("title");
    author = record.get("author");
    conference = record.get("conference");
}