List of usage examples for org.apache.commons.csv CSVPrinter printComment
public void printComment(final String comment) throws IOException
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 av a 2 s . c om*/ * * @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); }
From source file:com.bigtester.ate.tcg.controller.TrainingFileDB.java
/** * Write cache csv file./*from www . j a v a2s . co m*/ * * @param absoluteCacheFilePath * the absolute cache file path * @param beginningComments * the beginning comments * @param endingComments * the ending comments * @param trainedRecords * the trained records * @param append * the append * @throws IOException */ public static void writeCacheCsvFile(String absoluteCacheFilePath, String beginningComments, String endingComments, List<UserInputTrainingRecord> trainedRecords, boolean append) throws IOException { // Create new students objects FileWriter fileWriter = null;// NOPMD CSVPrinter csvFilePrinter = null;// NOPMD // Create the CSVFormat object with "\n" as a record delimiter CSVFormat csvFileFormat = getCSVFormat(); try { if (trainedRecords.isEmpty()) { fileWriter = new FileWriter(absoluteCacheFilePath, append); // initialize CSVPrinter object csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat); // Write a new student object list to the CSV file csvFilePrinter.printComment(beginningComments); csvFilePrinter.printComment(endingComments); fileWriter.flush(); fileWriter.close(); csvFilePrinter.close(); return; } // initialize FileWriter object fileWriter = new FileWriter(absoluteCacheFilePath, append); // initialize CSVPrinter object csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat); // Write a new student object list to the CSV file csvFilePrinter.printComment(beginningComments); for (UserInputTrainingRecord student : trainedRecords) { List<String> studentDataRecord = new ArrayList<String>(); studentDataRecord.add(student.getInputLabelName()); studentDataRecord.add(student.getInputMLHtmlCode()); csvFilePrinter.printRecord(studentDataRecord); } csvFilePrinter.printComment(endingComments); // System.out.println("CSV file was created successfully !!!"); } catch (Exception e) {// NOPMD throw new IOException("Error in CsvFileWriter !!!");// NOPMD // e.printStackTrace(); } finally { //NOPMD try { if (null != fileWriter) { fileWriter.flush(); fileWriter.close(); } if (null != csvFilePrinter) csvFilePrinter.close(); } catch (IOException e) {//NOPMD //System.out throw new IOException("Error while flushing/closing fileWriter/csvPrinter !!!");//NOPMD //e.printStackTrace(); } } }
From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.report.SVMHMMOutcomeIDReport.java
@Override public void execute() throws Exception { // load gold and predicted labels loadGoldAndPredictedLabels();/*from w w w . j av a2s . c o m*/ File testFile = locateTestFile(); // original tokens List<String> originalTokens = SVMHMMUtils.extractOriginalTokens(testFile); // sequence IDs List<Integer> sequenceIDs = SVMHMMUtils.extractOriginalSequenceIDs(testFile); // sanity check if (goldLabels.size() != originalTokens.size() || goldLabels.size() != sequenceIDs.size()) { throw new IllegalStateException("Gold labels, original tokens or sequenceIDs differ in size!"); } File evaluationFile = new File( getContext().getStorageLocation(TEST_TASK_OUTPUT_KEY, StorageService.AccessMode.READWRITE), SVMHMMUtils.GOLD_PREDICTED_OUTCOMES_CSV); // write results into CSV // form: gold;predicted;token;seqID CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(evaluationFile), SVMHMMUtils.CSV_FORMAT); csvPrinter.printComment(SVMHMMUtils.CSV_COMMENT); for (int i = 0; i < goldLabels.size(); i++) { csvPrinter.printRecord(goldLabels.get(i), predictedLabels.get(i), originalTokens.get(i), sequenceIDs.get(i).toString()); } IOUtils.closeQuietly(csvPrinter); }
From source file:de.tudarmstadt.ukp.dkpro.argumentation.sequence.report.TokenLevelEvaluationReport.java
@Override public void execute() throws Exception { // load gold and predicted labels loadGoldAndPredictedLabels();//from ww w . jav a2 s .c o m File testFile = locateTestFile(); // sequence IDs List<Integer> sequenceIDs = SVMHMMUtils.extractOriginalSequenceIDs(testFile); // meta data original List<SortedMap<String, String>> metaDataFeatures = SVMHMMUtils.extractMetaDataFeatures(testFile); // sanity check if (goldLabels.size() != sequenceIDs.size() || goldLabels.size() != metaDataFeatures.size()) { throw new IllegalStateException("check consistency"); } File evaluationFile = new File( getContext().getStorageLocation(TEST_TASK_OUTPUT_KEY, StorageService.AccessMode.READWRITE), TOKEN_LEVEL_PREDICTIONS_CSV); // write results into CSV // form: gold;predicted;token;seqID CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(evaluationFile), SVMHMMUtils.CSV_FORMAT); csvPrinter.printComment(SVMHMMUtils.CSV_COMMENT); // confusion matrix for evaluation ConfusionMatrix confusionMatrix = new ConfusionMatrix(); for (int i = 0; i < goldLabels.size(); i++) { String predictedLabelSentenceLevel = predictedLabels.get(i); // get gold token labels for this sentence List<String> goldTokenLabels = AbstractSequenceMetaDataFeatureGenerator.decodeFromString( metaDataFeatures.get(i).get(OrigBIOTokenSequenceMetaDataFeatureGenerator.FEATURE_NAME)); // get tokens for this sentence List<String> tokens = AbstractSequenceMetaDataFeatureGenerator.decodeFromString( metaDataFeatures.get(i).get(OrigTokenSequenceMetaDataFeatureGenerator.FEATURE_NAME)); // predicted token labels List<String> recreatedPredictedTokenLabels = ReportTools .recreateTokenLabels(predictedLabelSentenceLevel, goldTokenLabels.size()); for (int j = 0; j < goldTokenLabels.size(); j++) { String tokenGold = goldTokenLabels.get(j); String tokenPredicted = recreatedPredictedTokenLabels.get(j); // write to csv csvPrinter.printRecord(tokenGold, tokenPredicted, tokens.get(j), sequenceIDs.get(i).toString()); // add to matrix confusionMatrix.increaseValue(tokenGold, tokenPredicted); } } IOUtils.closeQuietly(csvPrinter); // and write to the output writeResults(getContext(), confusionMatrix); }
From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.report.SVMHMMBatchCrossValidationReport.java
protected void aggregateResults(String testTaskCSVFile, String outputPrefix) throws Exception { StorageService storageService = getContext().getStorageService(); // aggregate rows from all CSVs from all folds List<List<String>> allOutcomes = new ArrayList<>(); List<TaskContextMetadata> testTasks = collectTestTasks(); // we need test tasks! if (testTasks.isEmpty()) { throw new IllegalStateException("No test tasks found. Make sure you properly " + "define the test task in getTestTaskClass() (currently: " + getTestTaskClass().getName()); }//from w ww . j a v a 2s .c o m // iterate over all sub tasks for (TaskContextMetadata subContext : testTasks) { // locate CSV file with outcomes (gold, predicted, token, etc.) File csvFile = storageService.getStorageFolder(subContext.getId(), Constants.TEST_TASK_OUTPUT_KEY + File.separator + testTaskCSVFile); // load the CSV CSVParser csvParser = new CSVParser(new FileReader(csvFile), CSVFormat.DEFAULT.withCommentMarker('#')); // and add the all rows for (CSVRecord csvRecord : csvParser) { // row for particular instance List<String> row = new ArrayList<>(); for (String value : csvRecord) { row.add(value); } allOutcomes.add(row); } IOUtils.closeQuietly(csvParser); } // store aggregated outcomes again to CSV File evaluationFile = new File(getContext().getStorageLocation(Constants.TEST_TASK_OUTPUT_KEY, StorageService.AccessMode.READWRITE), testTaskCSVFile); log.debug("Evaluation file: " + evaluationFile.getAbsolutePath()); CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(evaluationFile), SVMHMMUtils.CSV_FORMAT); csvPrinter.printComment(SVMHMMUtils.CSV_COMMENT); csvPrinter.printRecords(allOutcomes); IOUtils.closeQuietly(csvPrinter); // compute confusion matrix ConfusionMatrix cm = new ConfusionMatrix(); for (List<String> singleInstanceOutcomeRow : allOutcomes) { // first item is the gold label String gold = singleInstanceOutcomeRow.get(0); // second item is the predicted label String predicted = singleInstanceOutcomeRow.get(1); cm.increaseValue(gold, predicted); } // and write all reports SVMHMMUtils.writeOutputResults(getContext(), cm, outputPrefix); // and print detailed results log.info(outputPrefix + "; " + cm.printNiceResults()); log.info(outputPrefix + "; " + cm.printLabelPrecRecFm()); }
From source file:act.installer.wikipedia.ImportantChemicalsWikipedia.java
/** * This function writes the important chemicals set to a TSV file. * @param outputPath a String indicating where the file should be written (including its name) *///from ww w . ja va 2 s . com public void writeToTSV(String outputPath) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(outputPath)); CSVPrinter printer = new CSVPrinter(writer, TSV_FORMAT); printer.printComment("This file has been generated by the ImportantChemicalsWikipedia.java script."); printer.printComment("Format: WIKIPEDIA<tab><wikipedia url><tab><inchi><tab><metadata>"); for (ImportantChemical importantChemical : importantChemicalsWikipedia) { List<String> nextLine = new ArrayList<>(); nextLine.add(importantChemical.getType()); nextLine.add(importantChemical.getDbid()); nextLine.add(importantChemical.getInchi()); nextLine.add(mapper.writeValueAsString(importantChemical.getMetadata())); printer.printRecord(nextLine); } printer.flush(); writer.close(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:com.itemanalysis.jmetrik.file.JmetrikFileImporter.java
private void convertFile() { CSVParser parser = null;//from w w w.ja v a2 s .c o m Reader reader = null; CSVPrinter printer = null; Writer writer = null; try { if (outputFile.exists()) { if (!overwrite) { theException = new IOException("File already exists and overwrite==false"); return; } } else { outputFile.createNewFile(); } //For debugging // System.out.println("CREATED: " + outputFile.getAbsolutePath()); //Writer header to file writer = new OutputStreamWriter(new FileOutputStream(outputFile)); printer = new CSVPrinter(writer, CSVFormat.DEFAULT.withCommentMarker('#')); printer.printComment("VERSION"); printer.printRecord(new String[] { "jmetrik1" }); printer.printComment("METADATA"); printer.printRecord(new String[] { Integer.valueOf(nrow).toString() }); printer.printComment("ATTRIBUTES"); for (VariableName v : variableAttributeMap.keySet()) { printer.printRecord(variableAttributeMap.get(v).getAttributeArray()); } printer.printComment("DATA"); //Write data to file reader = new InputStreamReader(new BOMInputStream(new FileInputStream(dataFile)), "UTF-8"); parser = new CSVParser(reader, dataFileFormat); if (hasHeader) { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames).withSkipHeaderRecord(true)); } else { parser = new CSVParser(reader, dataFileFormat.withHeader(colNames)); } Iterator<CSVRecord> iter = parser.iterator(); CSVRecord csvRecord = null; VariableAttributes variableAttributes = null; DataType dataType = null; String temp = ""; while (iter.hasNext()) { csvRecord = iter.next(); for (VariableName v : variableAttributeMap.keySet()) { temp = csvRecord.get(v.toString()); variableAttributes = variableAttributeMap.get(v); dataType = variableAttributes.getDataType(); if (!variableAttributes.isMissing(temp)) { if (DataType.INTEGER == dataType) { printer.print(Double.valueOf(Double.parseDouble(temp)).intValue()); } else if (DataType.DOUBLE == dataType) { printer.print(Double.parseDouble(temp)); } else { printer.print(temp); } } else { printer.print(temp); } } printer.println(); } } catch (IOException ex) { theException = ex; } finally { try { if (parser != null) parser.close(); if (reader != null) reader.close(); if (printer != null) printer.close(); if (writer != null) writer.close(); } catch (IOException ex) { theException = ex; logger.fatal(ex); } } }
From source file:org.servalproject.maps.export.CsvAsyncTask.java
private Integer doLocationExport() { if (V_LOG) {/*ww w. j a v a 2 s .com*/ Log.v(TAG, "doLocationExport called: "); } // reset the progress bar progressBar.setProgress(0); Integer mRecordCount = 0; updateUI = true; updateForLocation = true; // get all of the location data ContentResolver mContentResolver = context.getApplicationContext().getContentResolver(); // get the content Cursor mCursor = mContentResolver.query(LocationsContract.CONTENT_URI, null, null, null, null); // check on what was returned if (mCursor.getCount() > 0) { progressBar.setMax(mCursor.getCount()); mRecordCount = mCursor.getCount(); // get the export directory // get the path for the output files String mOutputPath = Environment.getExternalStorageDirectory().getPath(); mOutputPath += context.getString(R.string.system_path_export_data); if (FileUtils.isDirectoryWritable(mOutputPath) == false) { Log.e(TAG, "unable to access the required output directory"); mCursor.close(); return 0; } // build the output file name String mFileName = "serval-maps-export-locations-" + TimeUtils.getToday() + ".csv"; // write the data to the file BufferedWriter mOutput = null; String[] mLine = new String[LocationsContract.Table.COLUMNS.length]; try { //mOutput = new BufferedOutputStream(new FileOutputStream(mOutputPath + mFileName, false)); mOutput = new BufferedWriter(new FileWriter(mOutputPath + mFileName, false)); CSVPrinter mPrinter = new CSVPrinter(mOutput, csvFormat); // write the comment line mPrinter.printComment("Location data sourced from the Serval Maps application"); mPrinter.printComment("File created: " + TimeUtils.getToday()); mPrinter.printComment(Arrays.toString(LocationsContract.Table.COLUMNS)); while (mCursor.moveToNext()) { for (int i = 0; i < LocationsContract.Table.COLUMNS.length; i++) { mLine[i] = mCursor.getString(mCursor.getColumnIndex(LocationsContract.Table.COLUMNS[i])); } mPrinter.println(mLine); publishProgress(mCursor.getPosition()); // check to see if we need to cancel this task if (isCancelled() == true) { break; } } } catch (FileNotFoundException e) { Log.e(TAG, "unable to open the output file", e); } catch (IOException e) { Log.e(TAG, "unable to write the message at '" + mCursor.getPosition() + "' in the cursor", e); } finally { // play nice and tidy up try { if (mOutput != null) { mOutput.close(); } } catch (IOException e) { Log.e(TAG, "unable to close the output file", e); } mCursor.close(); } } return mRecordCount; }
From source file:org.servalproject.maps.export.CsvAsyncTask.java
private Integer doPoiExport() { // reset the progress bar progressBar.setProgress(0);//from www .j av a 2s . c o m Integer mRecordCount = 0; updateUI = true; updateForPoi = true; if (V_LOG) { Log.v(TAG, "doPoiExport called: "); } // get all of the location data ContentResolver mContentResolver = context.getApplicationContext().getContentResolver(); // get the content Cursor mCursor = mContentResolver.query(PointsOfInterestContract.CONTENT_URI, null, null, null, null); // check on what was returned if (mCursor.getCount() > 0) { progressBar.setMax(mCursor.getCount()); mRecordCount = mCursor.getCount(); // get the export directory // get the path for the output files String mOutputPath = Environment.getExternalStorageDirectory().getPath(); mOutputPath += context.getString(R.string.system_path_export_data); if (FileUtils.isDirectoryWritable(mOutputPath) == false) { Log.e(TAG, "unable to access the required output directory"); mCursor.close(); return 0; } // build the output file name String mFileName = "serval-maps-export-pois-" + TimeUtils.getToday() + ".csv"; // write the data to the file BufferedWriter mOutput = null; String[] mLine = new String[PointsOfInterestContract.Table.COLUMNS.length]; try { //mOutput = new BufferedOutputStream(new FileOutputStream(mOutputPath + mFileName, false)); mOutput = new BufferedWriter(new FileWriter(mOutputPath + mFileName, false)); CSVPrinter mPrinter = new CSVPrinter(mOutput, csvFormat); // write the comment line mPrinter.printComment("Location data sourced from the Serval Maps application"); mPrinter.printComment("File created: " + TimeUtils.getToday()); mPrinter.printComment(Arrays.toString(PointsOfInterestContract.Table.COLUMNS)); while (mCursor.moveToNext()) { for (int i = 0; i < PointsOfInterestContract.Table.COLUMNS.length; i++) { mLine[i] = mCursor .getString(mCursor.getColumnIndex(PointsOfInterestContract.Table.COLUMNS[i])); } mPrinter.println(mLine); publishProgress(mCursor.getPosition()); // check to see if we need to cancel this task if (isCancelled() == true) { break; } } } catch (FileNotFoundException e) { Log.e(TAG, "unable to open the output file", e); } catch (IOException e) { Log.e(TAG, "unable to write the message at '" + mCursor.getPosition() + "' in the cursor", e); } finally { // play nice and tidy up try { if (mOutput != null) { mOutput.close(); } } catch (IOException e) { Log.e(TAG, "unable to close the output file", e); } mCursor.close(); } } return mRecordCount; }
From source file:password.pwm.event.AuditManager.java
public int outputVaultToCsv(OutputStream outputStream, final Locale locale, final boolean includeHeader) throws IOException { final Configuration config = null; final CSVPrinter csvPrinter = Helper.makeCsvPrinter(outputStream); csvPrinter.printComment(" " + PwmConstants.PWM_APP_NAME + " audit record output "); csvPrinter.printComment(" " + PwmConstants.DEFAULT_DATETIME_FORMAT.format(new Date())); if (includeHeader) { final List<String> headers = new ArrayList<>(); headers.add("Type"); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_EventCode", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_Timestamp", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_GUID", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_Message", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_Instance", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_PerpetratorID", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_PerpetratorDN", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_TargetID", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_TargetDN", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_SourceAddress", config, password.pwm.i18n.Admin.class)); headers.add(LocaleHelper.getLocalizedMessage(locale, "Field_Audit_SourceHost", config, password.pwm.i18n.Admin.class)); csvPrinter.printRecord(headers); }//ww w. ja v a 2 s . c o m int counter = 0; for (final Iterator<AuditRecord> recordIterator = readVault(); recordIterator.hasNext();) { final AuditRecord loopRecord = recordIterator.next(); counter++; final List<String> lineOutput = new ArrayList<>(); lineOutput.add(loopRecord.getEventCode().getType().toString()); lineOutput.add(loopRecord.getEventCode().toString()); lineOutput.add(PwmConstants.DEFAULT_DATETIME_FORMAT.format(loopRecord.getTimestamp())); lineOutput.add(loopRecord.getGuid()); lineOutput.add(loopRecord.getMessage() == null ? "" : loopRecord.getMessage()); if (loopRecord instanceof SystemAuditRecord) { lineOutput.add(((SystemAuditRecord) loopRecord).getInstance()); } if (loopRecord instanceof UserAuditRecord) { lineOutput.add(((UserAuditRecord) loopRecord).getPerpetratorID()); lineOutput.add(((UserAuditRecord) loopRecord).getPerpetratorDN()); lineOutput.add(""); lineOutput.add(""); lineOutput.add(((UserAuditRecord) loopRecord).getSourceAddress()); lineOutput.add(((UserAuditRecord) loopRecord).getSourceHost()); } if (loopRecord instanceof HelpdeskAuditRecord) { lineOutput.add(((HelpdeskAuditRecord) loopRecord).getPerpetratorID()); lineOutput.add(((HelpdeskAuditRecord) loopRecord).getPerpetratorDN()); lineOutput.add(((HelpdeskAuditRecord) loopRecord).getTargetID()); lineOutput.add(((HelpdeskAuditRecord) loopRecord).getTargetDN()); lineOutput.add(((HelpdeskAuditRecord) loopRecord).getSourceAddress()); lineOutput.add(((HelpdeskAuditRecord) loopRecord).getSourceHost()); } csvPrinter.printRecord(lineOutput); } csvPrinter.flush(); return counter; }