环境设置见如何使用和https://limfx.pro相同的后端技术?
limfx坏词过滤功能可以帮你快速过滤任何对象中的一切string
/string[]
/List<string>
中所包含的脏字,中英文皆适用
在正确创建asp项目并且安装limfx的nuget包以后,在startup.cs
中,你应当可以在ConfigureServices
方法中添加以下语句以完成依赖注入:
services.AddSingleton<BadWordService>(new BadWordService()
{
//自定义脏字
censoredWords = new[] {"five","stupid"}.ToList()
});
BadWordService
中预设了默认脏字,但是比较少。
[ApiController]
[Route("[controller]")]
public class BadWordSampleController : ControllerBase
{
public Bad GetNewBad()
{
//设定坏字
return new Bad()
{
BadArray = new[] { "****", "****" },
BadList = new List<string>(new[] { "****", "****" }),
BadString = "****"
};
}
//设定深度3的坏字
public void SetNewDeepBad()
{
bad = GetNewBad();
bad.InnerBad = GetNewBad();
bad.InnerBad.InnerBad = GetNewBad();
}
private readonly BadWordService badWordService;
Bad bad;
public BadWordSampleController(BadWordService badWordService)
{
this.badWordService = badWordService;
}
//未处理结果
[HttpGet("bad")]
public Bad GetBad()
{
SetNewDeepBad();
return bad;
}
//浅处理结果
[HttpGet("****edbad")]
public Bad GetFuckedBad()
{
SetNewDeepBad();
return badWordService.BadwordsFucker(bad);
}
//深处理结果
[HttpGet("deeply****edbad")]
public Bad GetDeeplyFuckedBad()
{
SetNewDeepBad();
return badWordService.BadwordsFuckerDeep(bad);
}
}
//自定义的包含各种坏字的类
public class Bad
{
public string BadString { get; set; }
public string[] BadArray { get; set; }
public int a { get; set; }
public decimal b { get; set; }
public List<string> BadList { get; set; }
public Bad InnerBad { get; set; }
}
badWordService.BadwordsFucker(bad);
会把bad
中的所有直接的属性中的string
/string[]
/List<string>
类型的对象中的脏字替换掉,而不会替换对象的属性内部的脏字badWordService.BadwordsFuckerDeep(bad);
则会替换bad
中一切string
/string[]
/List<string>
类型中的脏字。
结果举例:
未处理结果:
浅处理结果:
深处理结果:
benchmark结果:
测试使用文本:bad.txt
badwordservice在开发时以高效为目标,其速度较快,但内存占用较高。
本文章使用limfx的vsocde插件快速发布