下面列出了怎么用weka.core.converters.CSVSaver的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Load the embedding from a given arff file. First converts the ARFF to a temporary CSV file and
* continues the loading mechanism with the CSV file afterwards
*
* @param path Path to the ARFF file
*/
private void loadEmbeddingFromArff(String path) {
// Try loading ARFF file
try {
Instances insts = new Instances(new FileReader(path));
CSVSaver saver = new CSVSaver();
saver.setFieldSeparator(" ");
saver.setInstances(insts);
final File tmpFile =
Paths.get(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString() + ".csv")
.toFile();
saver.setFile(tmpFile);
saver.setNoHeaderRow(true);
saver.writeBatch();
loadEmbeddingFromCSV(tmpFile);
tmpFile.delete();
} catch (Exception e) {
throw new RuntimeException(
"ARFF file could not be read (" + wordVectorLocation.getAbsolutePath() + ")", e);
}
}
/**
* Load the embedding from a given arff file. First converts the ARFF to a temporary CSV file and
* continues the loading mechanism with the CSV file afterwards
*
* @param path Path to the ARFF file
*/
private void loadEmbeddingFromArff(String path) {
// Try loading ARFF file
try {
Instances insts = new Instances(new FileReader(path));
CSVSaver saver = new CSVSaver();
saver.setFieldSeparator(" ");
saver.setInstances(insts);
final File tmpFile =
Paths.get(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString() + ".csv")
.toFile();
saver.setFile(tmpFile);
saver.setNoHeaderRow(true);
saver.writeBatch();
loadEmbeddingFromCSV(tmpFile);
tmpFile.delete();
} catch (Exception e) {
throw new RuntimeException(
"ARFF file could not be read (" + wordVectorLocation.getAbsolutePath() + ")", e);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
ArffLoader loader = new ArffLoader();
loader.setSource(new File("/Users/admin/Documents/NetBeansProjects/Datasets/weather.arff"));
Instances data = loader.getDataSet();
CSVSaver saver = new CSVSaver();
saver.setInstances(data);
saver.setFile(new File("weather.csv"));
saver.writeBatch();
}
/**
* Save @param data to the CSV file at @param path
*/
public static void saveDataToCsvFile(String path, Instances data) throws IOException{
System.out.println("\nSaving to file " + path + "...");
CSVSaver saver = new CSVSaver();
saver.setInstances(data);
saver.setFile(new File(path));
saver.writeBatch();
}
/**
* Returns the action lister to use in the menu.
*
* @param history the current history
* @param index the selected history item
* @return the listener
*/
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
final Result result = history.getResultAt(index);
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Instances performance = (Instances) result.getMeasurement(IncrementalPerformance.RESULTS_SAMPLED_OVER_TIME);
int retVal = getFileChooser().showSaveDialog(null);
if (retVal != MekaFileChooser.APPROVE_OPTION)
return;
File file = getFileChooser().getSelectedFile();
try {
CSVSaver saver = new CSVSaver();
saver.setInstances(performance);
saver.setFile(getFileChooser().getSelectedFile());
saver.writeBatch();
} catch (Exception ex) {
String msg = "Failed to write to '" + file + "'!";
System.err.println(msg);
ex.printStackTrace();
JOptionPane.showMessageDialog( null, msg + "\n" + e);
}
}
};
}
/**
* Returns the action lister to use in the menu.
*
* @param history the current history
* @param index the selected history item
* @return the listener
*/
@Override
public ActionListener getActionListener(final ResultHistoryList history, final int index) {
final Result result = history.getResultAt(index);
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int retVal = getFileChooser().showSaveDialog(null);
if (retVal != MekaFileChooser.APPROVE_OPTION)
return;
File file = getFileChooser().getSelectedFile();
try {
CSVSaver saver = new CSVSaver();
Instances performance = Result.getPredictionsAsInstances(result);
saver.setInstances(performance);
saver.setFile(getFileChooser().getSelectedFile());
saver.writeBatch();
} catch (Exception ex) {
String msg = "Failed to write to '" + file + "'!";
System.err.println(msg);
ex.printStackTrace();
JOptionPane.showMessageDialog( null, msg + "\n" + e);
}
}
};
}