我想根据其内容将某些数据分类为不同的类。我使用朴素贝叶斯分类器完成了它,我得到了一个输出作为它所属的最佳类别。但是现在我想将训练集中的新闻以外的新闻分类到“其他”类中。我不能手动将训练数据以外的每个/每个数据添加到某个类中,因为它有大量其他类别。那么有什么方法可以对其他数据进行分类吗?
private static File TRAINING_DIR = new File("4news-train");
private static File TESTING_DIR = new File("4news-test");
private static String[] CATEGORIES = { "c1", "c2", "c3", "others" };
private static int NGRAM_SIZE = 6;
public static void main(String[] args) throws ClassNotFoundException, IOException {
DynamicLMClassifier<NGramProcessLM> classifier = DynamicLMClassifier.createNGramProcess(CATEGORIES, NGRAM_SIZE);
for (int i = 0; i < CATEGORIES.length; ++i) {
File classDir = new File(TRAINING_DIR, CATEGORIES[i]);
if (!classDir.isDirectory()) {
String msg = "Could not find training directory=" + classDir + "\nTraining directory not found";
System.out.println(msg); // in case exception gets lost in shell
throw new IllegalArgumentException(msg);
}
String[] trainingFiles = classDir.list();
for (int j = 0; j < trainingFiles.length; ++j) {
File file = new File(classDir, trainingFiles[j]);
String text = Files.readFromFile(file, "ISO-8859-1");
System.out.println("Training on " + CATEGORIES[i] + "/" + trainingFiles[j]);
Classification classification = new Classification(CATEGORIES[i]);
Classified<CharSequence> classified = new Classified<CharSequence>(text, classification);
classifier.handle(classified);
}
}
}