package com.iamberry.wechat.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Component;


@Component("textFilterUtil")
public class TextFilterUtil {

	//默认编码格式
	  private static final String ENCODING = "utf-8";
	  //敏感词库的路径
	  private static final InputStream in = TextFilterUtil.class.getClassLoader().getResourceAsStream("keyWords.txt");
	 /* private static final InputStream in2 =new FileInputStream(new File("D:/sensitive/keyWords.txt"));
	  InputStreamReader lw=new InputStreamReader("D:/sensitive/keyWords.txt",ENCODING);*/
	 /*try{
		 InputStream in2 = new FileInputStream("D:\\demo.txt");//读取文件的数据。
	 }catch(Exception ex){
		 
	 }*/
	  
	  //脏字库
	  private static Set<Character> sensitiveCharSet = null;
	  //敏感词库
	  private static Set<String> sensitiveWordSet = null;
	  /**
	   * 初始化敏感词库
	   */
	  private static void init() {
	    //初始化容器
	    sensitiveCharSet = new HashSet<>();
	    sensitiveWordSet = new HashSet<>();
	    //读取文件 创建敏感词库
	    readSensitiveWords();
	  }
	  /**
	   * 读取本地的敏感词文件
	   *
	   * @return
	   */
	  public static void readSensitiveWords() {
		  if(sensitiveWordSet!=null){
			  return;
		  }
	    BufferedReader reader = null;
	    sensitiveWordSet=new HashSet();
	    sensitiveCharSet=new HashSet();
	    try {
	      //reader = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\KeyWords.txt"), "utf-8"));
	      reader = new BufferedReader(new InputStreamReader(in, "utf-8"));
	      String line;
	      while ((line = reader.readLine()) != null) {
	        String word = line.trim();
	       // System.out.println("nn:"+word);
	        String[] aa = word.split("\\|");
	        if(aa.length>0){
	        	 for(String st:aa){
	 	        	sensitiveWordSet.add(st);
	 	        }
	        }
	        for (Character c : word.toCharArray()) {
	        	if(c.equals("|")){
	        		continue;
	        	}
	          sensitiveCharSet.add(c);
	        }
	      }
	    } catch (UnsupportedEncodingException e) {
	      System.out.println("敏感词库文件转码失败!");
	    } catch (FileNotFoundException e) {
	    	System.out.println("敏感词库文件不存在!");
	    } catch (IOException e) {
	    	System.out.println("敏感词库文件读取失败!");
	    } finally {
	      if (reader != null) {
	        try {
	          reader.close();
	        } catch (IOException e) {
	          e.printStackTrace();
	        }
	        reader = null;
	      }
	    }
	    return;
	  }
	  /**
	   * 检查敏感词
	   *
	   * @return
	   */
	  public static List<String> checkSensitiveWord(String text) {
	    if (sensitiveWordSet == null || sensitiveCharSet == null) {
	      init();
	    }
	    List<String> sensitiveWords = new ArrayList<>();
	    for (int i = 0; i < text.length(); i++) {
	      Character word = text.charAt(i);
	      if (!sensitiveCharSet.contains(word)) {
	        continue;
	      }
	      int j = i;
	      while (j < text.length()) {
	        if (!sensitiveCharSet.contains(word)) {
	          break;
	        }
	        String key = text.substring(i, j + 1);
	        if (sensitiveWordSet.contains(key)) {
	          sensitiveWords.add(key);
	        }
	        j++;
	      }
	    }
	    return sensitiveWords;
	  }
	
	  /**
	   * 替换敏感词为**
	   *
	   * @return
	   */
	  public static String checkAndReplaceSensitiveWord(String text) {
	    if (sensitiveWordSet == null || sensitiveCharSet == null) {
	      init();
	    }

	    for (int i = 0; i < text.length(); i++) {
	      Character word = text.charAt(i);
	      if (!sensitiveCharSet.contains(word)) {
	        continue;
	      }
	      int j = i;
	      while (j < text.length()) {
	        if (!sensitiveCharSet.contains(word)) {
	          break;
	        }
	        String key = text.substring(i, j + 1);
	        if (sensitiveWordSet.contains(key)) {
	          text=text.replace(key, "**");
	        }
	        j++;
	      }
	    }
	    return text;
	  }
	  

//	public static void main(String[] args) throws ParseException {
//	
//		String str="他妈的日本鬼子，都是干嘛混的！你就知道牛逼哄哄的，是不是";
//		readSensitiveWords();
//		List<String> list=checkSensitiveWord(str);
//		
//		for(String len:list){
//			System.out.println(len);
//		}
//		System.out.println("================================");
//		System.out.println(checkAndReplaceSensitiveWord(str));
//	}
}



