Android敏感词标红

检测敏感词并标红处理

敏感词检测(敏感词过滤)我们选用的阿里云:
链接: https://market.aliyun.com/products/57124001/cmapi026055.html?spm=5176.2020520132.101.5.3b1b7218Rcn4wh#sku=yuncode2005500002

当初做的时候并没有返回那些字是敏感词,只返回了处理结果,所以需要做处理
示例:尼玛是你大爷
检测结果返回:**是你大爷

处理敏感词并标红展示:


private String isResult; //带*结果
private String reason;//待检测字符串
if (isResult.contains("*")){//*
            Object newName = StringUtil.checkKeyWord(reason,isResult);
            if (newName instanceof Spanned) {
                etCancelReason.setText((Spanned) newName);
            } else {
                etCancelReason.setText(reason);
            }
            ToastUtils.showShort("输入内容包含敏感词");
            return;
        }
 /**
     * 敏感词替换颜色
     * @param check
     * @param afterContent
     * @return
     */
    public static Object checkKeyWord(String check,String afterContent) {
        if (null == afterContent)
            return check;
         afterContent = afterContent.replace("*","");
        if (check != null && check.contains(afterContent)) {
            int index = check.indexOf(afterContent);
            int len = afterContent.length();
                    Spanned temp = Html.fromHtml(
                    check.substring(0, index) + "" + check.substring(index, index + len)
                            + "" + check.substring(index + len, check.length()));
            return temp;
        }
        return check;
    }

第二种方式,

将两个字符串找出不同,并将不同处高亮显示


/**
 * Data: 2019/1/9
 * Description:
 * 将两个字符串找出不同,并将不同处高亮显示
 */
public class CompareStrUtil {


    public static String[] getHighLightDifferent(String a,String b){
        String[] temp=getDiff(a, b);
        String[] result={getHighLight(a, temp[0]),getHighLight(b, temp[1])};
        return result;
    }

    private static String getHighLight(String source,String temp){
        StringBuffer sb=new StringBuffer();
        char[] sourceChars=source.toCharArray();
        char[] tempChars=temp.toCharArray();
        boolean flag=false;
        for(int i=0;i").append(sourceChars[i]);
                else if(flag) sb.append(sourceChars[i]);
                else sb.append("").append(sourceChars[i]);
                flag=true;
                if(i==sourceChars.length-1) sb.append("");
            }
            else if(flag==true){
                sb.append("").append(sourceChars[i]);
                flag=false;
            }else sb.append(sourceChars[i]);
        }
        return sb.toString();
    }
    public static String[] getDiff(String a, String b) {
        String[] result = null;
        //选取长度较小的字符串用来穷举子串
        if (a.length() < b.length()) {
            result = getDiff(a, b, 0, a.length());
        } else {
            result = getDiff(b, a, 0, b.length());
            result = new String[]{result[1],result[0]};
        }
        return result;
    }

    //将a的指定部分与b进行比较生成比对结果
    private static String[] getDiff(String a, String b, int start, int end){
        String[] result = new String[]{a, b};
        int len = result[0].length();
        while (len > 0) {
            for (int i = start; i < end - len + 1; i++) {
                String sub = result[0].substring(i, i + len);
                int idx = -1;
                if ((idx = result[1].indexOf(sub)) != -1) {
                    result[0] = setEmpty(result[0], i, i + len);
                    result[1] = setEmpty(result[1], idx, idx + len);
                    if (i > 0) {
                        //递归获取空白区域左边差异
                        result = getDiff(result[0], result[1], 0, i);
                    }
                    if (i + len < end) {
                        //递归获取空白区域右边差异
                        result = getDiff(result[0], result[1], i + len, end);
                    }
                    len=0;//退出while循环
                    break;
                }
            }
            len = len / 2;
        }
        return result;
    }

    //将字符串s指定的区域设置成空格
    public static String setEmpty(String s, int start, int end) {
        char[] array = s.toCharArray();
        for (int i = start; i < end; i++) {
            array[i] = ' ';
        }
        return new String(array);
    }

}

你可能感兴趣的:(Android)