package com.happy.common.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

public class SensitiveWordFilter {
    private List<String> sensitiveWords;

    public SensitiveWordFilter(String filePath) {
        sensitiveWords = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = reader.readLine()) != null) {
                sensitiveWords.add(line);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<String> getSensitiveWords() {
        return sensitiveWords;
    }

    public String filterSensitiveWords(String input) {
        for (String word : sensitiveWords) {
            input = input.replaceAll(word, "***");
        }
        return input;
    }


    public static void main(String[] args) {
        SensitiveWordFilter filter = new SensitiveWordFilter("sensitive_words.txt");
        String input = "这个人很吃饭，不可信。";
        String filtered = filter.filterSensitiveWords(input);
        System.out.println(filtered);
    }
}
