Backend Development 13 min read

Using the Sensitive‑Word Java Library for Sensitive Content Detection and Replacement

This article demonstrates how to integrate the open‑source Sensitive‑Word Java library via Maven, showcases core find/replace methods, explains advanced detection features such as email, URL, IP and numeric filtering, and provides a custom replacement strategy with complete code examples.

Java Architect Essentials
Java Architect Essentials
Java Architect Essentials
Using the Sensitive‑Word Java Library for Sensitive Content Detection and Replacement

The article introduces the Sensitive‑Word Java library, a tool for detecting and handling sensitive words in text, and provides a step‑by‑step guide for adding it to a Maven project.

1) Maven Dependency

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>sensitive-word</artifactId>
    <version>0.18.0</version>
</dependency>

2) Core Method Usage – The library offers methods to check for sensitive words, find the first occurrence, list all matches, and replace them. Example code demonstrates regular replacement, custom symbols, and case‑insensitive matching.

package com.example.demo;

import com.github.houbb.sensitive.word.api.IWordContext;
import com.github.houbb.sensitive.word.api.IWordReplace;
import com.github.houbb.sensitive.word.api.IWordResult;
import com.github.houbb.sensitive.word.bs.SensitiveWordBs;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.github.houbb.sensitive.word.support.result.WordResultHandlers;
import com.github.houbb.sensitive.word.utils.InnerWordCharUtils;

import java.util.List;

public class SensitiveWordTestDemo {
    public static void main(String[] args) {
        testMoreFeatures();
    }

    // regular usage example
    public static void testNormal() {
        final String text = "五星红旗迎风飘扬,毛主席的画像屹立在天安门前。";
        System.out.println("是否包含铭感词:" + SensitiveWordHelper.contains(text));
        System.out.println("查找第一个铭感词:" + SensitiveWordHelper.findFirst(text));
        System.out.println("查找所有铭感词:" + SensitiveWordHelper.findAll(text));
        System.out.println("替换所有铭感词:" + SensitiveWordHelper.replace(text));
        System.out.println("替换所有铭感词(指定替换符号):" + SensitiveWordHelper.replace(text, '⭐'));
    }

    // custom replace strategy example
    public static void testDefineReplace() {
        final String text = "五星红旗迎风飘扬,毛主席的画像屹立在天安门前。";
        MySensitiveWordReplace replace = new MySensitiveWordReplace();
        String result = SensitiveWordHelper.replace(text, replace);
        System.out.println("自定义替换策略结果:" + result);
    }
}

class MySensitiveWordReplace implements IWordReplace {
    @Override
    public void replace(StringBuilder stringBuilder, char[] chars, IWordResult wordResult, IWordContext iWordContext) {
        String sensitiveWord = InnerWordCharUtils.getString(chars, wordResult);
        if ("五星红旗".equals(sensitiveWord)) {
            stringBuilder.append("国家旗帜");
        } else if ("毛主席".equals(sensitiveWord)) {
            stringBuilder.append("教员");
        } else {
            int wordLength = wordResult.endIndex() - wordResult.startIndex();
            for (int i = 0; i < wordLength; i++) {
                stringBuilder.append('*');
            }
        }
    }
}

3) Advanced Detection Strategies – The library can optionally enable checks for email addresses, URLs, IPv4 addresses, numeric sequences, and custom repeat‑character detection. Sample code shows how to turn each feature on and retrieve matches.

// email detection
String text = "楼主好人,邮箱 [email protected]";
List
emailList = SensitiveWordBs.newInstance()
    .enableEmailCheck(true).init().findAll(text);
System.out.println("是否存在邮箱:" + emailList);

// numeric sequence detection (default 8‑digit)
text = "你懂得:12345678";
List
numList = SensitiveWordBs.newInstance()
    .enableNumCheck(true).init().findAll(text);
System.out.println("是否存在连续数字字符串:" + numList);

// URL detection (v0.18.0 improves accuracy)
text = "点击链接 https://www.baidu.com 查看答案";
SensitiveWordBs urlBs = SensitiveWordBs.newInstance().enableUrlCheck(true).init();
List
urlList = urlBs.findAll(text);
System.out.println("是否存在网址信息:" + urlList);
System.out.println("是否存在网址信息2并替换:" + urlBs.replace(text));

// IPv4 detection
text = "个人网站,如果网址打不开可以访问 127.0.0.1。";
SensitiveWordBs ipBs = SensitiveWordBs.newInstance().enableIpv4Check(true).init();
List
ipList = ipBs.findAll(text);
System.out.println("是否存在 IPv4:" + ipList);

The article concludes with sample console output illustrating detection results and provides the open‑source repository URL (https://github.com/houbb/sensitive-word) for further exploration.

Javamavenopen-sourcelibraryText FilteringSensitive Word
Java Architect Essentials
Written by

Java Architect Essentials

Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.