자바 민감 어 필터

3668 단어
package test.java.lang;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @title KeywordFilter
 * @description TODO 
 * @author 
 * @date 2014-4-17
 * @version 1.0
 */
public class KeywordFilter {
    /**      
     * { ={isEnd=0,  ={isEnd=1}},  ={isEnd=0,  ={isEnd=0,  ={isEnd=1},  ={isEnd=0,  ={isEnd=1}}}}}
     * */
    private HashMap keysMap = new HashMap();

    /**
     *      
     * @param keywords
     */
    public void addKeywords(List keywords) {
        for (int i = 0; i < keywords.size(); i++) {
            String key = keywords.get(i).trim();
            HashMap nowhash = keysMap;//        
            for (int j = 0; j < key.length(); j++) {
                char word = key.charAt(j);
                Object wordMap = nowhash.get(word);
                if (wordMap != null) {
                    nowhash = (HashMap) wordMap;
                } else {
                    HashMap newWordHash = new HashMap();
                    newWordHash.put(\"isEnd\", \"0\");
                    nowhash.put(word, newWordHash);
                    nowhash = newWordHash;
                }
                if (j == key.length() - 1) {
                    nowhash.put(\"isEnd\", \"1\");
                }
            }
        }
    }

    /** 
     *         begin        keyword  ,
     *     ,   0
     *       keyword ,    ,    isEnd = 1,     keyword   , 
     */
    private int checkKeyWords(String txt, int begin) {
        HashMap nowhash = keysMap;
        int res = 0;
        for (int i = begin; i < txt.length(); i++) {
            char word = txt.charAt(i);
            Object wordMap = nowhash.get(word);//        HashMap
            if (wordMap == null) {
                return 0;//          HashMap,return 0
            }

            res++;//        HashMap  null,          ,+1
            nowhash = (HashMap) wordMap;//    HashMap        HashMap
            if (((String) nowhash.get(\"isEnd\")).equals(\"1\")) {//              ,    
                return res;
            } else {
                continue;
            }
        }
        return res;
    }

    /** 
     *   txt        
     */
    public boolean isContentKeyWords(String txt) {
        for (int i = 0; i < txt.length(); i++) {
            int len = checkKeyWords(txt, i);
            if (len > 0) {
                return true;
            }
        }
        return false;
    }

    /** 
     *   txt        
     */
    public List getTxtKeyWords(String txt) {
        List list = new ArrayList();
        int l = txt.length();
        for (int i = 0; i < l;) {
            int len = checkKeyWords(txt, i);
            if (len > 0) {
                String tt = txt.substring(i, i + len);
                list.add(tt);
                i += len;
            } else {
                i++;
            }
        }
        return list;
    }

    /** 
     *          
     * */
    public void initfiltercode() {
        List keywords = new ArrayList();
        keywords.add(\"   \");
        keywords.add(\"    \");
        keywords.add(\"  \");
        this.addKeywords(keywords);
    }

    public static void main(String[] args) throws IOException {
        KeywordFilter filter = new KeywordFilter();
        filter.initfiltercode();
        String txt = \"  ,         ,   ,\";
        boolean boo = filter.isContentKeyWords(txt);
        System.out.println(boo);
        List set = filter.getTxtKeyWords(txt);
        System.out.println(\"        :\" + set);
    }
}

좋은 웹페이지 즐겨찾기