Java implements the function _java of the shielding word

Source: Internet
Author: User

will often see the post bar to screen a variety of users posted content, then think how to achieve this. I had wanted to use string to replace the way (ReplaceAll) to achieve, but this efficiency is very low, also can not guarantee the longest match, this is their original idea. Recently, I did a project, you need to block some of the content, and I did an analysis of the problem, the final form of the following code.

 /** * @Description: The function of shielding words to achieve * * 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 (); 
 Shielding word hashset private hashset<string> keystring = new hashset<string> (); 
 Private final static int maxLength = Character.max_value; 
  
 Shielding 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; 
  /** * @param str * @return * @Description: The input string is handled by shielding to achieve maximum length matching/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)) {//MAX length match 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 (); /** * @param path * @Description: Initialize load shielding word * @Description: The data format logic for storage screen words is as follows * @Description: Build a HASHSET&L T String> used to store all the shielding words * @Description: Build length = MaxLength = ChaRacter. Max_value hashset<integer> Array * @Description: Converts the first character in the loaded mask to an int value, where the relevant information is stored in the array, * @Description: "Hello" get ' you ' The int value is 20320, that is, "Hello!" related information is stored in the array's No. 20320 position * @Description: Array each bit stored in the hashset<integer> structure at that location the length of the shielding word/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) {//Shielding 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 actress Nihao")); 
  int i = ' you '; 
 System.out.println (i);  } 
}

The above is the Java implementation of the key Code screen Word function, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.