📄 clustergenerator.java
字号:
*/
public int getNumExamplesAct() { return m_NumExamplesAct; }
/**
* Sets the print writer.
* @param newOutput the new print writer
*/
public void setOutput(PrintWriter newOutput) {
m_Output = newOutput;
}
/**
* Gets the print writer.
* @return print writer object
*/
public PrintWriter getOutput() { return m_Output; }
/**
* Sets the format of the dataset that is to be generated.
* @param the new dataset format of the dataset
*/
protected void setFormat(Instances newFormat) {
m_Format = new Instances(newFormat, 0);
}
/**
* Gets the format of the dataset that is to be generated.
* @return the dataset format of the dataset
*/
protected Instances getFormat() {
Instances format = new Instances(m_Format, 0);
return format;
}
/**
* Returns a string representing the dataset in the instance queue.
* @return the string representing the output data format
*/
protected String toStringFormat() {
if (m_Format == null)
return "";
return m_Format.toString();
}
/**
* Calls the data generator.
*
* @param dataGenerator one of the data generators
* @param options options of the data generator
* @exception Exception if there was an error in the option list
*/
public static void makeData(ClusterGenerator generator, String [] options)
throws Exception {
PrintWriter output = null;
// read options /////////////////////////////////////////////////
try {
setOptions(generator, options);
} catch (Exception ex) {
String specificOptions = "";
if (generator instanceof OptionHandler) {
specificOptions = generator.listSpecificOptions(generator);
}
String genericOptions = listGenericOptions(generator);
throw new Exception('\n' + ex.getMessage()
+ specificOptions + genericOptions);
}
// define dataset format ///////////////////////////////////////
// computes actual number of examples to be produced
generator.setFormat(generator.defineDataFormat());
// get print writer /////////////////////////////////////////////
output = generator.getOutput();
// output of options ////////////////////////////////////////////
output.println("% ");
output.print("% " + generator.getClass().getName() + " ");
String [] outOptions = generator.getGenericOptions();
for (int i = 0; i < outOptions.length; i++) {
output.print(outOptions[i] + " ");
}
outOptions = ((OptionHandler) generator).getOptions();
for (int i = 0; i < outOptions.length; i++) {
output.print(outOptions[i] + " ");
}
output.println("\n%");
// comment at beginning of ARFF File ////////////////////////////
String commentAtStart = generator.generateStart();
if (commentAtStart.length() > 0) {
output.println(commentAtStart);
}
// ask data generator which mode ////////////////////////////////
boolean singleMode = generator.getSingleModeFlag();
// start data producer ////////////////////////////////////////
if (singleMode) {
// output of dataset header //////////////////////////////////
output.println(generator.toStringFormat());
for (int i = 0; i < generator.getNumExamplesAct(); i++) {
// over all examples to be produced
Instance inst = generator.generateExample();
output.println(inst);
}
} else { // generator produces all instances at once
Instances dataset = generator.generateExamples();
// output of dataset ////////////////////////////////////////////
output.println(dataset);
}
// comment at end of ARFF File /////////////////////////////////////
String commentAtEnd = generator.generateFinished();
if (commentAtEnd.length() > 0) {
output.println(commentAtEnd);
}
if (output != null) {
output.close();
}
}
/**
* Makes a string with the options of the specific data generator.
*
* @param generator the datagenerator that is used
* @return string with the options of the data generator used
*/
private String listSpecificOptions(ClusterGenerator generator) {
String optionString = "";
if (generator instanceof OptionHandler) {
optionString += "\nData Generator options:\n\n";
Enumeration em = ((OptionHandler)generator).listOptions();
while (em.hasMoreElements()) {
Option option = (Option) em.nextElement();
optionString += option.synopsis() + '\n'
+ option.description() + "\n";
}
}
return optionString;
}
/**
* Sets the generic options and specific options.
*
* @param generator the data generator used
* @param options the generic options and the specific options
* @exception Exception if help request or any invalid option
*/
private static void setOptions(ClusterGenerator generator,
String[] options) throws Exception {
boolean helpRequest = false;
String outfileName = new String("");
PrintWriter output;
// get help
helpRequest = Utils.getFlag('h', options);
if (Utils.getFlag('d', options)) { generator.setDebug(true); }
// get relationname
String relationName = Utils.getOption('r', options);
// set relation name at end of method after all options are set
// get outputfilename
outfileName = Utils.getOption('o', options);
// get num of clusters
String num = Utils.getOption('k', options);
if (num.length() != 0)
generator.setNumClusters(Integer.parseInt(num));
// get class flag
if (Utils.getFlag('c', options))
generator.setClassFlag(true);
// get num of attributes
String numAttributes = Utils.getOption('a', options);
if (numAttributes.length() != 0)
generator.setNumAttributes(Integer.parseInt(numAttributes));
if (generator instanceof OptionHandler) {
((OptionHandler)generator).setOptions(options);
}
// all options are set, now set relation name
generator.setRelationName(relationName);
// End read options
Utils.checkForRemainingOptions(options);
if (helpRequest) {
throw new Exception("Help requested.\n");
}
if (outfileName.length() != 0) {
output = new PrintWriter(new FileOutputStream(outfileName));
} else {
output = new PrintWriter(System.out);
}
generator.setOutput(output);
}
/**
* Method for listing generic options.
*
* @param generator the data generator
* @return string with the generic data generator options
*/
private static String listGenericOptions (ClusterGenerator generator) {
String genericOptions = "\nGeneral options:\n\n"
+ "-h\n"
+ "\tGet help on available options.\n"
+ "-r <relation name>\n"
+ "\tThe name of the relation for the produced dataset.\n"
+ "-a <number of attributes>\n"
+ "\tThe number of attributes for the produced dataset.\n"
+ "-k <number of clusters>\n"
+ "\tThe number of clusters the dataset is produced in.\n"
+ "-c \n"
+ "\tThe class flag, if set, the cluster is listed in the class "
+ "attribute.\n"
+ "-o <file>\n"
+ "\tThe name of the file output instances will be written to.\n"
+ "\tIf not supplied the instances will be written to stdout.\n";
return genericOptions;
}
/**
* Gets the current generic settings of the datagenerator.
*
* @return an array of strings suitable for passing to setOptions
*/
private String [] getGenericOptions() {
String [] options = new String [10];
int i = 0;
String name = getRelationName();
if (name.length() > 0) {
options[i++] = "-r";
options[i++] = "" + getRelationName();
}
options[i++] = "-a"; options[i++] = "" + getNumAttributes();
options[i++] = "-k"; options[i++] = "" + getNumClusters();
if (getClassFlag()) {
options[i++] = "-c"; options[i++] = "";
}
while (i < options.length) {
options[i++] = "";
}
return options;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -