Package com.tradeaider.app.utils;
Import com.tradeaider.app.activity.MyApplication;
Import java.util.ArrayList;
Import Java.util.Arrays;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
/**
* @name Tradeaider-andriod
* @class describe
* @anthor fengy Email:[email protected]
* @time 2017/3/22 11:36
* @class describe: Filter Sensitive words tool class
*/
public class Sensitivewordutils {
private static String ENCODING = "Utf-8"; /* Character encoding */
static arraylist<string> first = new arraylist<string> ();
Static list<string> keys=new arraylist<string> ();
Static string[] Sortfirst;
Static char[] Charfirst;
Static hashmap<string, arraylist<string>> map = new hashmap<string, arraylist<string>> ();
Static hashmap<string, string[]> Sortmap = new hashmap<string, string[]> ();
Static hashmap<string, char[]> charmap = new hashmap<string, char[]> ();
Static Arraylist<string> temp;
Static String key, value;
int length;
private static Boolean issingleflag=false;
private static MyApplication application=myapplication.getinstance ();
static {
String general=application.getsensitivegeneral ();
String contact=application.getsensitivecontact ();
Issingleflag=application.getissinglesession ();
String[] Generalarray=general.split (";");
String[] Contactarray=contact.split (";");
for (int i=0;i<generalarray.length;i++) {
Keys.add (Generalarray[i]);
}
if (!issingleflag) {
for (int k=0;k<contactarray.length;k++) {
Keys.add (Contactarray[k]);
}
}
}
/**
* Constructors with parameters
*
* @param tcontent What you need to filter
*/
Public sensitivewordutils (String tcontent) {
for (String K:keys) {
if (!first.contains (k.substring (0, 1))) {
First.add (k.substring (0, 1));
}
Length = K.length ();
for (int i = 1; i < length; i++) {
Key = k.substring (0, I);
Value = k.substring (i, i + 1);
if (i = = 1 &&!first.contains (key)) {
First.add (key);
}
/* There, add */
if (Map.containskey (key)) {
if (!map.get (key). Contains (value)) {
Map.get (Key). Add (value);
}
}
/* not added */
else {
temp = new arraylist<string> ();
Temp.add (value);
Map.put (key, temp);
}
}
}
Sortfirst = First.toarray (New String[first.size ()]);
Arrays.sort (Sortfirst); /* Sort */
Charfirst = new Char[first.size ()];
for (int i = 0; i < charfirst.length; i++) {
Charfirst[i] = First.get (i). charAt (0);
}
Arrays.sort (Charfirst); /* Sort */
String[] Sortvalue;
Arraylist<string> v;
Map.entry<string, arraylist<string>> Entry;
Iterator<map.entry<string, arraylist<string>>> iter = Map.entryset ()
. iterator ();
while (Iter.hasnext ()) {
Entry = (map.entry<string, arraylist<string>>) Iter.next ();
v = (arraylist<string>) entry.getvalue ();
Sortvalue = V.toarray (New String[v.size ()]);
Arrays.sort (Sortvalue); /* Sort */
Sortmap.put (Entry.getkey (), sortvalue);
}
Char[] Charvalue;
iter = Map.entryset (). iterator ();
while (Iter.hasnext ()) {
Entry = (map.entry<string, arraylist<string>>) Iter.next ();
v = (arraylist<string>) entry.getvalue ();
Charvalue = new Char[v.size ()];
for (int i = 0; i < charvalue.length; i++) {
Charvalue[i] = V.get (i). charAt (0);
}
Arrays.sort (Charvalue); /* Sort */
Charmap.put (Entry.getkey (), charvalue);
}
}
/**
* Replace the sensitive words with *
*
* @param content needs to be filtered
* @return The content that meets the requirements after the filter is finished
*/
public string Replace (string content) {
String r = null, F, c = content;
String Replacedword = content;
Char G;
Char[] temps;
int length = C.length ();
for (int i = 0; i < length-1; i++) {
g = C.charat (i);
/* Two-point lookup */
if (Arrays.binarysearch (Charfirst, G) >-1) {
Tag
for (int j = i + 1; j < length; J + +) {
f = c.substring (I, j);
g = C.charat (j);
Temps = Charmap.get (f);
if (temps = = null)/* found */{/* SYSTEM.OUT.PRINTLN ("OK"); */
R = f;
String str = "";
for (int m = 1; M <= r.length (); m++) {
str = str + "*";
}
Replacedword = C.replace (r, str);
c = Replacedword;
Break tag;
}
/* Two-point lookup */
if (Arrays.binarysearch (Temps, g) >-1) {
if (j = = length-1) {
/*
* Print ("find!");
* SYSTEM.OUT.PRINTLN ("find!");
*/
R = c.substring (i, j + 1);
String str = "";
for (int m = 1; M <= r.length (); m++) {
str = str + "*";
}
Replacedword = C.replace (r, str);
c = Replacedword;
Break tag;
}
} else {/* not found */
Break
}
}
}
}
return (Replacedword);
}
}
Android Sensitive word filter main class