我尝试使用Stanford Classifier进行基本的文本分类。我的示例数据集基于Ham or Spam。
这是我的代码:
Properties props = new Properties();ColumnDataClassifier cdc = new ColumnDataClassifier(props);Classifier<String, String> cl = cdc.makeClassifier(cdc.readTrainingExamples("data.train"));for (String line : ObjectBank.getLineIterator("data.test", "utf-8")) { Datum<String, String> d = cdc.makeDatumFromLine(line); System.out.println(line + " ==> " + cl.classOf(d));}
然而,无论我尝试分类什么文本,它总是将其分类为Ham。以下句子显然是Spam,但仍然被分类为Ham:
免费消息通过回复此消息并输入单词FREE来激活您的500条免费文本消息。如需条款和条件,请访问www.example.com
我的错误在哪里?
回答:
我的错误是我没有提供任何属性:
Properties props = new Properties();ColumnDataClassifier cdc = new ColumnDataClassifier(props);
您可以直接在代码中指定属性:
// 设置管道属性Properties props = new Properties();// 设置要运行的注释器列表props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,depparse,coref,kbp,quote");// 为注释器设置属性,在本例中,coref注释器被设置为使用神经算法props.setProperty("coref.algorithm", "neural");
或者您可以提供一个属性文件:
ColumnDataClassifier cdc = new ColumnDataClassifier(propFile);