2

请告诉我如何将文本文件转换为 .arff 格式(weka),因为我想对 1000 txt 文件进行数据聚类。

问候

4

2 回答 2

1

WEKA 中实现了一些转换器,只需找到正确的格式或对您的数据进行少量更改(使用 awk、sed...)。

以下是与该主题相关的 API 页面:http ://weka.sourceforge.net/doc.stable/weka/core/converters/package-summary.html

例如,这里是如何从 CSV 转换为 ARFF:

java weka.core.converters.CSVLoader filename.csv > filename.arff
于 2013-01-15T16:37:42.187 回答
0

这是您可以使用的代码

package text.Classification;
import java.io.*;
import weka.core.*;
public class TextDirectoryToArff {

public Instances createDataset(String directoryPath) throws Exception {
FastVector atts;
FastVector attVals;
atts = new FastVector();
atts.addElement(new Attribute("contents", (FastVector) null));
String[] s = { "class1", "class2", "class3" };
attVals = new FastVector();
for (String p : s)
    attVals.addElement(p);
atts.addElement(new Attribute("class", attVals));
Instances data = new Instances("MyRelation", atts, 0);
System.out.println(data);

InputStreamReader is = null;
File dir = new File(directoryPath);
String[] files = dir.list();
for (int i = 0; i < files.length; i++) {
    if (files[i].endsWith(".txt")) {
    double[] newInst = new double[2];
    File txt = new File(directoryPath + File.separator + files[i]);

    is = new InputStreamReader(new FileInputStream(txt));
    StringBuffer txtStr = new StringBuffer();

    int c;
    while ((c = is.read()) != -1) {
        txtStr.append((char) c);
    }
    newInst[0] = data.attribute(0).addStringValue(txtStr.toString());
    int j=i%(s.length-1);
    newInst[1] = attVals.indexOf(s[j]);
    data.add(new Instance(1.0, newInst));

    }
}
return data;
}



public static void main(String[] args) {

TextDirectoryToArff tdta = new TextDirectoryToArff();
try {
    Instances dataset = tdta.createDataset("/home/asadul/Desktop/Downloads/text_example/class5");
    PrintWriter fileWriter = new PrintWriter("/home/asadul/Desktop/Downloads/text_example/abc.arff", "UTF-8");
    fileWriter.println(dataset);
    fileWriter.close();
} catch (Exception e) {
    System.err.println(e.getMessage());
    e.printStackTrace();
}

}

}

于 2015-09-03T13:49:06.487 回答