java违禁词过滤违禁词识别,可以对评论的内容进行一个过滤,避免出现违禁词
部分核心代码,用到了DFA算法
public static void init(){ Arrays.asList(arr).stream().forEach(vocabulary->{ HashMap<String,Object> currentMap = new HashMap(); for(int i = 0;i<vocabulary.length();i ){ String ch = vocabulary.substring(i,i 1); if(i==0){ Object tempObj = bannedWordMap.get(ch); if(tempObj==null){ bannedWordMap.put(ch,currentMap); }else{ currentMap = (HashMap<String,Object>)tempObj; } }else{ HashMap<String,Object> tempMap = null; Object tempObj = currentMap.get(ch); if(tempObj==null){ tempMap = new HashMap<>(); currentMap.put(ch,tempMap); }else{ tempMap = (HashMap<String, Object>) tempObj; } currentMap = tempMap; } if(i 1==vocabulary.length()){ currentMap.put("isEnd",true); } } }); System.out.println(bannedWordMap); }
评论