package cn.reghao.tnb.sb.bayes.income;

import java.util.ArrayList;
import java.util.List;

/**
 * @author reghao
 * @date 2025-08-25 13:27:32
 */
public class BayesMain {
    String originalTrain = "/home/reghao/Dropbox/dataset/income/original/adult.data";
    String processedTrain = "/home/reghao/Dropbox/dataset/income/processed/adultResult.txt";
    String originalTest = "/home/reghao/Dropbox/dataset/income/original/adult1000.test";
    String processedTest = "/home/reghao/Dropbox/dataset/income/processed/adult1000Result.txt";
    String originalTest100 = "/home/reghao/Dropbox/dataset/income/original/adult100.test";
    String processedTest100 = "/home/reghao/Dropbox/dataset/income/processed/adult100Result.txt";
    String finalStr = "";
    int wrongNumber = 0; // 记录错误的数量
    List<List<String>> trainList;
    List<List<String>> testList;

    void process() {
        boolean removeNoise = true; // 是否排除干扰数据

        DataConverter dataConverter = new DataConverter();
        dataConverter.processFile(originalTrain, processedTrain, removeNoise);
        dataConverter.processFile(originalTest, processedTest, removeNoise);

        trainList = dataConverter.readFile(processedTrain);
        testList = dataConverter.readFile(processedTest);

        Bayes bayes = new Bayes();
        for (List<String> strList : testList) {
            List<String> tmp = new ArrayList<>(strList);
            String label = tmp.get(tmp.size() - 1);
            tmp.remove(tmp.size() - 1);
            finalStr = bayes.predictClass(trainList, tmp);

            if (!label.equals(finalStr)) {
                wrongNumber++;
            }
        }
        System.out.printf("预测总数: %s, 错误数: %s\n", testList.size(), wrongNumber);

        // 保留 10 位小数
        double divResult = DecimalCalculator.divide(wrongNumber, testList.size(), 10);
        double correctRate = DecimalCalculator.subtract(1.00000000, divResult);
        System.out.println("正确率: " + correctRate*100 + "%");
    }

    public static void main(String[] args) {
        BayesMain bayesMain = new BayesMain();
        bayesMain.process();
    }
}
