/** * 训练时或者测试时使用,自动将类别信息加上 为语料库中所有文件计算特征值,并保存在本地文件(libsvm格式) * * <p>类别序号从1开始 * * @param dicPath 词典目录 * @param libPath 语料库目录,其下存放待分类类的语料。 * @param resultFilePath 保存为libsvm格式的语料文档特征值文件 */ public void CalcFeaturesForAllFiles_Training( FeatureMaker FM, FeatureCounter FC, String dicPath, String libPath, String resultFilePath) { CategorizingDataManager CDM = new CategorizingDataManager(FC, dicPath, libPath, true); Dictionary dicEx = new Dictionary(); String[] docs = CDM.getDocs(); try { FileWriter fw = new FileWriter(resultFilePath); int docID = 0; // 文本ID在同一目录中递增 // fw.write(CDM.N+"\n"); for (int i = 0; i < docs.length; i++) { // 对每一个类别 for (String doc : CDM.getFilesPath(docs[i])) { // 对每一篇文档 // 写入特征向量 String temp = FeatureVectorOfDoc(FM, dicEx.readAll(doc), docID++, CDM); // System.out.println((i+1)+" "+temp+"\n"); fw.write((i * 2 - 1) + " " + temp + "\n"); } } fw.flush(); fw.close(); } catch (IOException e) { e.printStackTrace(); } }