List of usage examples for org.apache.commons.csv CSVRecord get
public String get(final String name)
From source file:com.ibm.watson.developer_cloud.natural_language_classifier.v1.util.TrainingDataUtils.java
/** * Transform a {@link Record} into a {@link TrainingData}. * * @param record the record/* w ww. jav a 2 s. c om*/ * @return the trainig data from csv record */ private static TrainingData getTrainigDataFromCSVRecord(CSVRecord record) { TrainingData trainingData = new TrainingData(); trainingData.setText(record.get(0)); for (int i = 1; i < record.size(); i++) { trainingData.addClass(record.get(i)); } return trainingData; }
From source file:com.mycompany.match.gender.app.FirstNamesListModel.java
static HashMap<String, String> getNames() throws IOException { InputStream in = Thread.currentThread().getClass().getResourceAsStream("/data/malenames.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); Iterable<CSVRecord> records = readCsv(reader); for (CSVRecord record : records) { if (record.getRecordNumber() >= 0) { // Here true represents male String key = record.get(0).toLowerCase(); GenderNamesList.put(key, "male"); }//from w ww . j a v a 2 s. c o m } in = Thread.currentThread().getClass().getResourceAsStream("/data/femalenames.txt"); reader = new BufferedReader(new InputStreamReader(in)); records = readCsv(reader); for (CSVRecord record : records) { if (record.getRecordNumber() >= 0) { // Here false represents female String key = record.get(0).toLowerCase(); if (!GenderNamesList.containsKey(key)) { GenderNamesList.put(key, "female"); } else { GenderNamesList.put(key, "either"); } } } return GenderNamesList; }
From source file:br.edimarmanica.weir2.integration.ScoredPairs.java
public static List<ScoredPair> loadAndSort(Domain domain) { List<ScoredPair> pairs = new ArrayList<>(); try (Reader in = new FileReader(new File(Paths.PATH_WEIR_V2 + "/" + domain.getPath() + "/scores.csv"))) { try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) { for (CSVRecord record : parser) { //para cada value Rule rule1 = new Rule(domain.getSiteOf(record.get("SITE1")), record.get("RULE1")); Rule rule2 = new Rule(domain.getSiteOf(record.get("SITE2")), record.get("RULE2")); double score = Double.valueOf(record.get("SCORE")); if (score == 1) { continue; }/* w w w . j a v a2 s .c o m*/ ScoredPair pair = new ScoredPair(rule1, rule2, score); pairs.add(pair); } } } catch (FileNotFoundException ex) { Logger.getLogger(RulesDataTypeController.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(RulesDataTypeController.class.getName()).log(Level.SEVERE, null, ex); } Collections.sort(pairs); return pairs; }
From source file:ca.craigthomas.visualclassifier.dataset.DataSetReader.java
/** * Read from a CSV file, and return the samples as a list of doubles. * //from ww w .ja v a 2s .c o m * @param filename the name of the file to read from * @return the list of samples from the file * @throws IOException */ public static List<List<Double>> readCSVFile(String filename) throws IOException { File file = new File(filename); String fileContents = FileUtils.readFileToString(file); Reader reader = new StringReader(fileContents); CSVFormat format = CSVFormat.EXCEL; CSVParser parser = new CSVParser(reader, format); List<CSVRecord> records = parser.getRecords(); List<List<Double>> inputs = new ArrayList<List<Double>>(); for (CSVRecord record : records) { List<Double> inputLine = new ArrayList<Double>(); for (int index = 0; index < record.size(); index++) { String value = record.get(index); inputLine.add(Double.parseDouble(value)); } inputs.add(inputLine); } parser.close(); return inputs; }
From source file:com.vsthost.rnd.commons.math.ext.linear.IOUtils.java
/** * Reads a matrix of double values from the reader provided. * * @param reader The reader which the values to be read from. * @return A matrix//from w ww . ja v a 2 s .co m * @throws IOException As thrown by the CSV parser. */ public static RealMatrix readMatrix(Reader reader) throws IOException { // Initialize the return value: List<double[]> retval = new ArrayList<>(); // Parse and get the iterarable: Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(reader); // Iterate over the records and populate return value: for (CSVRecord record : records) { double[] row = new double[record.size()]; for (int i = 0; i < record.size(); i++) { row[i] = Double.parseDouble(record.get(i)); } retval.add(row); } // Convert the list to an array: double[][] retvalArray = new double[retval.size()][]; retval.toArray(retvalArray); // Done, return the array: return MatrixUtils.createRealMatrix(retvalArray); }
From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.EvalHelper.java
/** * Reads predicted and gold data from predictions CSV file and fills&returns the confusion matrix * * @param csvPredictions predictions (gold; predicted) * @return matrix/*from w w w . ja v a 2 s. com*/ */ public static ConfusionMatrix createConfusionMatrixFromCSVPredictions(File csvPredictions) throws IOException { CSVParser csvParser = new CSVParser(new FileReader(csvPredictions), CSVFormat.DEFAULT.withCommentMarker('#')); ConfusionMatrix result = new ConfusionMatrix(); for (CSVRecord csvRecord : csvParser) { // copy record // update confusion matrix result.increaseValue(csvRecord.get(0), csvRecord.get(1)); } return result; }
From source file:com.mycompany.match.gender.app.PersonModel.java
static HashMap<String, Person> getPersonsData() throws IOException { InputStream in = Thread.currentThread().getClass().getResourceAsStream("/data/name_email_sample.csv"); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); Iterable<CSVRecord> records = readCsv(reader); for (CSVRecord record : records) { Person person;/* w w w . ja v a 2 s . co m*/ if (record.getRecordNumber() != 1) { if (record.size() > 1) { person = new Person(record); persons.put(record.get(9), person); } } } return persons; }
From source file:io.github.seiferma.jameica.hibiscus.dkb.creditcard.synchronize.csvparser.DKBCsvParser.java
private static DKBTransaction createTransaction(CSVRecord record) throws IOException { String bookedString = record.get(0); boolean isBooked = "Ja".equals(bookedString); String valueDateString = record.get(1); Date valueDate;// w w w . j a va 2 s. co m try { valueDate = DateUtils.parseDate(valueDateString); } catch (ParseException e) { throw new IOException("Could not parse value date.", e); } String bookingDateString = record.get(2); Date bookingDate; try { bookingDate = DateUtils.parseDate(bookingDateString); } catch (ParseException e) { throw new IOException("Could not parse booking date.", e); } String description = record.get(3); description = description.replaceAll(" ", ""); String amountString = record.get(4); amountString = amountString.replace(".", ""); Pattern amountPattern = Pattern.compile("(-?[0-9]+),([0-9]{1,2})"); Matcher amountMatcher = amountPattern.matcher(amountString); if (!amountMatcher.matches() || amountMatcher.groupCount() != 2) { throw new IOException(String.format("Could not parse transaction amount (%s).", amountString)); } int amountInCents = getCentsFromString(amountMatcher.group(1), amountMatcher.group(2)); return DKBTransactionImpl.create().setIsBooked(isBooked).setValueDate(valueDate).setBookingDate(bookingDate) .setDescription(description).setAmountInCents(amountInCents).build(); }
From source file:main.StratioENEI.java
private static void readFromCSV(String filename, float[][] outCustos, List<String> outCidades) throws IOException { CSVParser c = new CSVParser(new FileReader(filename), CSVFormat.EXCEL.withDelimiter(';').withNullString("")); int lineNumber; for (CSVRecord record : c) { // System.out.println(record); if ((lineNumber = (int) record.getRecordNumber()) == 1) { continue; }//from w w w.ja v a 2 s . c o m outCidades.add(record.get(0)); // System.out.printf("\n%10s", record.get(0)); for (int i = lineNumber - 1; i < outCustos.length + 1; i++) { // System.out.printf("\t%-6s|", (record.get(i) == null) ? "null" : record.get(i)); outCustos[lineNumber - 2][i - 1] = outCustos[i - 1][lineNumber - 2] = Float .parseFloat((record.get(i) == null) ? "0.0" : record.get(i)); } } }
From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.GenerateCrossDomainCVReport.java
/** * Merges id2outcome files from sub-folders with cross-domain and creates a new folder * with overall results/*from ww w . j a v a2 s . c o m*/ * * @param folder folder * @throws java.io.IOException */ public static void aggregateDomainResults(File folder, String subDirPrefix, final String taskFolderSubText, String outputFolderName) throws IOException { // list all sub-folders File[] folders = folder.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && pathname.getName().contains(taskFolderSubText); } }); if (folders.length == 0) { throw new IllegalArgumentException("No sub-folders 'SVMHMMTestTask*' found in " + folder); } // write to a file File outFolder = new File(folder, outputFolderName); File output = new File(outFolder, subDirPrefix); output.mkdirs(); File outCsv = new File(output, TOKEN_LEVEL_PREDICTIONS_CSV); CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(outCsv), SVMHMMUtils.CSV_FORMAT); csvPrinter.printComment(SVMHMMUtils.CSV_COMMENT); ConfusionMatrix cm = new ConfusionMatrix(); for (File domain : folders) { File tokenLevelPredictionsCsv = new File(domain, subDirPrefix + "/" + TOKEN_LEVEL_PREDICTIONS_CSV); if (!tokenLevelPredictionsCsv.exists()) { throw new IllegalArgumentException( "Cannot locate tokenLevelPredictions.csv: " + tokenLevelPredictionsCsv); } CSVParser csvParser = new CSVParser(new FileReader(tokenLevelPredictionsCsv), CSVFormat.DEFAULT.withCommentMarker('#')); for (CSVRecord csvRecord : csvParser) { // copy record csvPrinter.printRecord(csvRecord); // update confusion matrix cm.increaseValue(csvRecord.get(0), csvRecord.get(1)); } } // write to file FileUtils.writeStringToFile(new File(outFolder, "confusionMatrix.txt"), cm.toString() + "\n" + cm.printNiceResults() + "\n" + cm.printLabelPrecRecFm() + "\n" + cm.printClassDistributionGold()); // write csv IOUtils.closeQuietly(csvPrinter); }