Android敏感词过滤主要类

知是行的主意,行是知的功夫。这篇文章主要讲述Android敏感词过滤主要类相关的知识,希望能为你提供帮助。
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 FengyEmail:[email  protected]
* @time 2017/3/22 11:36
* @class describe: 过滤敏感词工具类
*/
public class SensitiveWordUtils {
private static String ENCODING = "utf-8"; /* 字符编码 */
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]);
}
}
}
 
/**
* 带参数的构造函数
*
* @param tContent 需要过滤的内容
*/
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 = https://www.songbingjia.com/android/k.substring(i, i + 1);
if (i == 1 & & !first.contains(key)) {
first.add(key);
}
/* 有,添加 */
if (map.containsKey(key)) {
if (!map.get(key).contains(value)) {
map.get(key).add(value);
}
}
/* 没有添加 */
else {
temp = new ArrayList< String> ();
temp.add(value);
map.put(key, temp);
}
}
}
sortFirst = first.toArray(new String[first.size()]);
Arrays.sort(sortFirst); /* 排序 */
charFirst = new char[first.size()];
for (int i = 0; i < charFirst.length; i++) {
charFirst[i] = first.get(i).charAt(0);
}
Arrays.sort(charFirst); /* 排序 */
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 = https://www.songbingjia.com/android/v.toArray(new String[v.size()]);
Arrays.sort(sortValue); /* 排序 */
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 = https://www.songbingjia.com/android/new char[v.size()];
for (int i = 0; i < charValue.length; i++) {
charValue[i] = v.get(i).charAt(0);
}
Arrays.sort(charValue); /* 排序 */
charMap.put(entry.getKey(), charValue);
}
}
【Android敏感词过滤主要类】
/**
* 把敏感词替换成*
*
* @param content 需要过滤的内容
* @return 过滤完后的符合要求的内容
*/
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);
/* 二分查找 */
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) /* 找到了 */ { /* 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;
}
/* 二分查找 */
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 { /* 没有找到了 */
break;
}
}
}
}
return (replacedword);
}
}

























































































































































    推荐阅读