Java to achieve the word function

  • 2020-04-01 04:34:26
  • OfStack

Often can see post bar inside block various user's post content, at that time think this how should realize. At that time, I thought of using string to replace the way (replaceAll) to achieve, but this efficiency is very low, also can't guarantee the longest match, this is my original idea at that time. I recently worked on a project where I needed to mask some of the content, so I analyzed the problem again and finally formed the following code.


  
package cn.yicha.novel.search.util; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.HashSet; 
 
import cn.yicha.novel.search.config.Config; 
 
public class Forbidden { 
  
 private static Forbidden forbidden = new Forbidden(); 
 //Block word HashSet
 private HashSet<String> keyString = new HashSet<String>(); 
 private final static int maxLength = Character.MAX_VALUE; 
 //The masking word length HashSet array
 @SuppressWarnings("unchecked") 
 private HashSet<Integer>[] keyLength = new HashSet[maxLength]; 
  
 private Forbidden() { 
  loadForbidden(Config.getClassRoot() + "forbidden.txt"); 
 } 
 public static Forbidden getForbidden(){ 
  return forbidden; 
 } 
  
  
 public String read(String str){ 
  if (str == null){ 
   return null; 
  } 
  StringBuffer stringBuffer = new StringBuffer(); 
  int start = 0; 
  for (int i = 0; i < str.length();){ 
   int at = str.charAt(i); 
   if (keyLength[at] == null){ 
    i++; 
    continue; 
   } else { 
    int ml = 0; 
    for (Object obj : keyLength[at].toArray()){ 
     int len = ((Integer)obj).intValue(); 
     if (i + len <= str.length()){ 
      String s = str.substring(i, i + len); 
      if (keyString.contains(s)){ 
       //Maximum length matching
       ml = len > ml ? len : ml; 
      } 
     } 
    } 
    if (ml > 0){ 
     stringBuffer.append(str.substring(start, i)).append("***"); 
     i += ml; 
     start = i; 
    } else { 
     i++; 
    } 
   } 
  } 
  if (start < str.length()){ 
   stringBuffer.append(str.substring(start)); 
  } 
  return stringBuffer.toString(); 
 } 
  
  
 public void loadForbidden(String path){ 
  File forbiddenFile = new File(path); 
  FileInputStream fileInputStream; 
  try { 
   fileInputStream = new FileInputStream(forbiddenFile); 
   InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "utf-8"); 
   BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 
   String s; 
   while ((s = bufferedReader.readLine()) != null){ 
    s = s.trim(); 
    if (s.length() > 0){ 
     keyString.add(s); 
     int i = s.charAt(0); 
     if (keyLength[i] == null){ 
      //The masking word length HashSet
      HashSet<Integer> a = new HashSet<Integer>(); 
      a.add(s.length()); 
      keyLength[i] = a; 
     } else { 
      keyLength[i].add(s.length()); 
     } 
    } 
   } 
   fileInputStream.close(); 
   bufferedReader.close(); 
   fileInputStream.close(); 
  } catch (FileNotFoundException e) { 
   e.printStackTrace(); 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
 } 
 
 public static void main(String[] args) { 
//System. Out. Println (Forbidden. GetForbidden (), read (AV star "nihao"));
  int i = ' you '; 
  System.out.println(i); 
 } 
} 

Above is the key code of Java to achieve the function of shielding words, I hope to help you learn.


Related articles: