广告词检测demo

package com.yjj;

import cn.hutool.http.HttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yjj
 * @description: 广告词检测
 * @date 2021/6/25 11:08
 */
public class TestAdvertising {
    
    
    public static final String SPAN_START = "<span style=\"color:red;\">";
    public static final String SPAN_END = "</span>";

    public static final Integer START_LENGTH = SPAN_START.length();
    public static final Integer END_LENGTH = SPAN_END.length();

    public static final String WATERMARK_URK = "";

    public static void main(String[] args) {
    
    
        String text = "我们的房子是全世界最好的房子,有极高的品质,经常在这个房子居住,会有美白的效果,非常惊人,仅需3天,就能看到效果,比去医院强多了,医疗器械价钱太贵,不如买套房子,来养生和造福后代。";

        Map<String, String> colorMap = new HashMap<>();
        for (CheckType checkType : CheckType.values()) {
    
    
            String type = checkType.name;
            Map<String, Object> map = new HashMap<>(2);
            map.put("text", text);
            map.put("type", type);
            String resp = HttpUtil.post(WATERMARK_URK, map, 3000);
            List<String> keyWord = findKeyWord(resp);
            for (int i = 0; i < keyWord.size() - 1; i++) {
    
    
                colorMap.put(keyWord.get(i), checkType.color);
            }
        }

        for(String key : colorMap.keySet()){
    
    
            String star = "<span style=\"color:" + colorMap.get(key) + ";\">";
            String end = "</span>";
            text = text.replaceAll(key, star+key+end);
        }
        System.out.println(text);
    }



    public String delWatermark(String text){
    
    
        Map<String, String> colorMap = new HashMap<>();
        for (CheckType checkType : CheckType.values()) {
    
    
            String type = checkType.name;
            Map<String, Object> map = new HashMap<>(2);
            map.put("text", text);
            map.put("type", type);
            String resp = HttpUtil.post(WATERMARK_URK, map, 3000);
            List<String> keyWord = findKeyWord(resp);
            for (int i = 0; i < keyWord.size() - 1; i++) {
    
    
                colorMap.put(keyWord.get(i), checkType.color);
            }
        }

        for(String key : colorMap.keySet()){
    
    
            String star = "<span style=\"color:" + colorMap.get(key) + ";\">";
            String end = "</span>";
            text = text.replaceAll(key, star+key+end);
        }
        System.out.println(text);
        return text;
    }

    private static List<String> findKeyWord(String text) {
    
    
        List<String> words = new ArrayList<>();
        int start;
        int end;

        start = text.indexOf(SPAN_START);
        end = text.indexOf(SPAN_END);
        while (true) {
    
    
            if (start != -1) {
    
    
                words.add(text.substring(start + START_LENGTH, end));
                start = text.indexOf(SPAN_START, end + END_LENGTH);
                end = text.indexOf(SPAN_END, end + END_LENGTH);
            } else {
    
    
                break;
            }
        }
        return words;
    }

}

enum CheckType {
    
    
    // 类型
    UN_LIMITED(0, "不限", "red"),
    CURRENCY(1, "通用", "green"),
    MEDICAL_CARE(2, "医疗", "cyan"),
    NEWS(3, "新闻", "blue"),
    COSMETICS(4, "化妆品", "yellow"),
    REAL_ESTATE(5, "房地产", "violet");
//    REAL_ESTATE(0, "房地产", "violet"),
//    COSMETICS(1, "化妆品", "yellow"),
//    NEWS(2, "新闻", "blue"),
//    MEDICAL_CARE(3, "医疗", "cyan"),
//    CURRENCY(4, "通用", "green"),
//    UN_LIMITED(5, "不限", "origin");

    Integer id;
    String name;
    String color;

    CheckType(Integer id, String name, String color) {
    
    
        this.id = id;
        this.name = name;
        this.color = color;
    }
}

package com.yjj;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * @author yjj
 * @description:
 * @date 2021/6/29 10:07
 */
@Data
public class CheckResult implements Serializable {
    
    

    /**
     * 分类名
     */
    String name;

    /**
     * 分类颜色
     */
    String color;

    /**
     * 检测单词
     */
    List<String> words;

    /**
     * 单词个数
     */
    Integer num;

    /**
     * 优先级
     */
    Integer order;

    public Integer getNum() {
    
    
        return words.size();
    }
}

http://s.tool.chinaz.com/ajaxsync.aspx?at=vabkw

Guess you like

Origin blog.csdn.net/asd521a65sdf12/article/details/121923774