我正在尝试通过TextDirectoryToArff.java
将txt文件转换为arff。我在Windows操作系统上使用eclipse,用法告诉我TextDirectoryToArff <directory path>
。但我不确定这意味着什么。
有人可以帮我这个程序吗?
TextDirectoryToArff.java: -
import java.io.*;
import weka.core.*;
/**
* Builds an arff dataset from the documents in a given directory.
* Assumes that the file names for the documents end with ".txt".
*
* Usage:<p>
*
* TextDirectoryToArff <directory path><p>
*
* @author Richard Kirkby (rkirkby at cs.waikato.ac.nz)
* @version 1.0
*/
public class TextDirectoryToArff {
public Instances createDataset(String directoryPath) throws Exception {
FastVector atts = new FastVector(2);
atts.addElement(new Attribute("filename", (FastVector) null));
atts.addElement(new Attribute("contents", (FastVector) null));
Instances data = new Instances("text_files_in_" + directoryPath, atts, 0);
File dir = new File(directoryPath);
String[] files = dir.list();
for (int i = 0; i < files.length; i++) {
if (files[i].endsWith(".txt")) {
try {
double[] newInst = new double[2];
newInst[0] = (double)data.attribute(0).addStringValue(files[i]);
File txt = new File(directoryPath + File.separator + files[i]);
InputStreamReader is;
is = new InputStreamReader(new FileInputStream(txt));
StringBuffer txtStr = new StringBuffer();
int c;
while ((c = is.read()) != -1) {
txtStr.append((char)c);
}
newInst[1] = (double)data.attribute(1).addStringValue(txtStr.toString());
data.add(new Instance(1.0, newInst));
} catch (Exception e) {
//System.err.println("failed to convert file: " + directoryPath + File.separator + files[i]);
}
}
}
return data;
}
public static void main(String[] args) {
if (args.length == 1) {
TextDirectoryToArff tdta = new TextDirectoryToArff();
try {
Instances dataset = tdta.createDataset(args[0]);
System.out.println(dataset);
}
catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
else {
System.out.println("Usage: java TextDirectoryToArff <directory name>");
}
}
}
答案 0 :(得分:0)
Weka要求您使用JAVA执行TextDirectoryToArff作为目录的参数传递。
您有两种选择:
1)生成名为“TextDirectoryToArff”的JAR,然后通过Windows中的控制台使用JAVA并执行java TextDirectoryToArff将目录作为paratemer传递。
2)从Eclipse中你可以直接传递你的目录,比如修改数据集,然后进行Run,run
Instances dataset = tdta.createDataset(args[0]);
要:
Instances dataset = tdta.createDataset("C:\\yourDir");