我正在编写一个程序来将给定的测试实例分类为“正面”或“负面”;使用 j48 算法。我使用 Weka 工具创建了模型,并在我的 java 程序中使用它。
我的 Java 程序
public static void main(String[] args) {
double result = -1;
try {
ArrayList<Attribute> attributeList = new ArrayList<>(2);
Attribute tweet = new Attribute("tweet", true);
ArrayList<String> classVal = new ArrayList<>();
classVal.add("positive");
classVal.add("negative");
attributeList.add(tweet);
attributeList.add(new Attribute("class", classVal));
Instances data = new Instances("TestInstances", attributeList, 1);
Instance inst_co = new DenseInstance(data.numAttributes());
data.add(inst_co);
data.setClassIndex(data.numAttributes() - 1);
inst_co.setValue(tweet, "I love my grandmom");
StringToWordVector filter = new StringToWordVector();
Classifier cls_co = (Classifier) weka.core.SerializationHelper
.read("G:/love.model");
result = cls_co.classifyInstance(data.firstInstance());
System.out.println(result);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
我的训练数据文件
@RELATION love
@ATTRIBUTE tweet string
@ATTRIBUTE class {positive,negative}
@DATA
"I love my sister", positive
"I love my brother", positive
"I love my father", positive
"I love my mother", positive
"I love my uncle", positive
"I love my grandmom", positive
"I hate my cats", negative
"I hate my car", negative
"I hate my van", negative
"I hate my rice", negative
"I hate my bun", negative
我从 ArrayIndexOutOfBoundsExceptionresult = cls_co.classifyInstance(data.firstInstance());
我尝试了很多在互联网上找到的例子。但我仍然遇到同样的错误。
提前致谢。