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

import cn.reghao.jutil.jdk.io.TextFile;
import com.huaban.analysis.jieba.JiebaSegmenter;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-08-25 17:26:37
 */
public class TextProcessor {
    JiebaSegmenter segmenter = new JiebaSegmenter();
    TextFile textFile = new TextFile();
    String baseDir;

    public TextProcessor(String baseDir) {
        this.baseDir = baseDir;
    }

    void write(String filePath, List<String> lines) {
        textFile.write(filePath, lines);
    }

    List<String> getTextLines(String filePath) {
        return textFile.read(filePath);
    }

    List<String> getWordList(String contentPath) {
        contentPath = contentPath.replace("..", baseDir);
        String content = readFile(contentPath);

        List<String> tmpList = segmenter.sentenceProcess(content);
        return tmpList.stream().filter(text -> text.length() > 1).collect(Collectors.toList());
    }

    String readFile(String filePath) {
        String charset = "gbk";
        StringBuffer sb = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), charset));
            String line = "";
            int flag = 0;
            while ((line = br.readLine()) != null) {
                if (flag == 1) {
                    sb.append(line);
                }

                if (line.equals("") || line.length() == 0) {
                    flag = 1;
                }
            }
        } catch (Exception e) {
            // ignore
        }

        return sb.toString();
    }
}
