mirror of
https://github.com/toolgood/ToolGood.Words.git
synced 2026-10-17 04:40:38 +08:00
修改 IllegalWordsSearch
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
@@ -155,7 +155,7 @@ ToolGood.Words: 本项目源代码
|
||||
## 性能对比
|
||||
执行10万次性能对比,结果如下:
|
||||
|
||||

|
||||

|
||||
|
||||
注:C#自带正则很慢,`StringSearchEx2.ContainsAny`是`Regex.IsMatch`效率的8.8万倍多,跟关键字数量有关。
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace ToolGood.Words.Contrast
|
||||
static WordsSearchEx3 wordsSearchEx3 = new WordsSearchEx3();
|
||||
|
||||
|
||||
//static IllegalWordsSearch illegalWordsSearch = new IllegalWordsSearch();
|
||||
static IllegalWordsSearch illegalWordsSearch = new IllegalWordsSearch();
|
||||
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace ToolGood.Words.Contrast
|
||||
Run("StringSearchEx(ContainsAny)--- WordsSearchEx(ContainsAny)代码相同", () => { stringSearchEx.ContainsAny(text); });
|
||||
Run("StringSearchEx2(ContainsAny)--- WordsSearchEx2(ContainsAny)代码相同", () => { stringSearchEx2.ContainsAny(text); });
|
||||
Run("StringSearchEx3(ContainsAny)--- WordsSearchEx3(ContainsAny)代码相同", () => { stringSearchEx3.ContainsAny(text); });
|
||||
//Run("IllegalWordsSearch(ContainsAny)", () => { illegalWordsSearch.ContainsAny(text); });
|
||||
Run("IllegalWordsSearch(ContainsAny)", () => { illegalWordsSearch.ContainsAny(text); });
|
||||
|
||||
Run("StringSearch(FindFirst)", () => { stringSearch.FindFirst(text); });
|
||||
Run("StringSearchEx(FindFirst)", () => { stringSearchEx.FindFirst(text); });
|
||||
@@ -55,7 +55,7 @@ namespace ToolGood.Words.Contrast
|
||||
Run("WordsSearchEx(FindFirst)", () => { wordsSearchEx.FindFirst(text); });
|
||||
Run("WordsSearchEx2(FindFirst)", () => { wordsSearchEx2.FindFirst(text); });
|
||||
Run("WordsSearchEx3(FindFirst)", () => { wordsSearchEx3.FindFirst(text); });
|
||||
//Run("IllegalWordsSearch(FindFirst)", () => { illegalWordsSearch.FindFirst(text); });
|
||||
Run("IllegalWordsSearch(FindFirst)", () => { illegalWordsSearch.FindFirst(text); });
|
||||
|
||||
|
||||
Console.Write("-------------------- Find All 100000次 --------------------\r\n");
|
||||
@@ -70,7 +70,7 @@ namespace ToolGood.Words.Contrast
|
||||
Run("WordsSearchEx(FindAll)", () => { wordsSearchEx.FindAll(text); });
|
||||
Run("WordsSearchEx2(FindAll)", () => { wordsSearchEx2.FindAll(text); });
|
||||
Run("WordsSearchEx3(FindAll)", () => { wordsSearchEx3.FindAll(text); });
|
||||
//Run("IllegalWordsSearch(FindAll)", () => { illegalWordsSearch.FindAll(text); });
|
||||
Run("IllegalWordsSearch(FindAll)", () => { illegalWordsSearch.FindAll(text); });
|
||||
|
||||
Console.Write("-------------------- Replace 100000次 --------------------\r\n");
|
||||
Run("TrieFilter(Replace)", () => { tf1.Replace(text); });
|
||||
@@ -80,7 +80,7 @@ namespace ToolGood.Words.Contrast
|
||||
Run("StringSearchEx(Replace)--- WordsSearchEx(Replace)代码相同", () => { stringSearchEx.Replace(text); });
|
||||
Run("StringSearchEx2(Replace)--- WordsSearchEx2(Replace)代码相同", () => { stringSearchEx2.Replace(text); });
|
||||
Run("StringSearchEx3(Replace)--- WordsSearchEx3(Replace)代码相同", () => { stringSearchEx3.Replace(text); });
|
||||
//Run("IllegalWordsSearch(Replace)", () => { illegalWordsSearch.Replace(text); });
|
||||
Run("IllegalWordsSearch(Replace)", () => { illegalWordsSearch.Replace(text); });
|
||||
|
||||
Console.Write("-------------------- Regex 100次 --------------------\r\n");
|
||||
Run(100, "Regex.IsMatch", () => { re.IsMatch(text); });
|
||||
@@ -142,7 +142,7 @@ namespace ToolGood.Words.Contrast
|
||||
wordsSearchEx.SetKeywords(list);
|
||||
wordsSearchEx2.SetKeywords(list);
|
||||
wordsSearchEx3.SetKeywords(list);
|
||||
//illegalWordsSearch.SetKeywords(list);
|
||||
illegalWordsSearch.SetKeywords(list);
|
||||
|
||||
list = list.OrderBy(q => q).ToList();
|
||||
var str = string.Join("|", list);
|
||||
|
||||
@@ -54,6 +54,7 @@ namespace ToolGood.Words.Test
|
||||
Assert.AreEqual("zg人", all[0].Keyword);
|
||||
|
||||
test = "fuck al[]l"; //未启用跳词
|
||||
iwords.UseSkipWordFilter = false;
|
||||
all = iwords.FindAll(test);
|
||||
Assert.AreEqual("fuck", all[0].Keyword);
|
||||
Assert.AreEqual(1, all.Count);
|
||||
@@ -73,6 +74,7 @@ namespace ToolGood.Words.Test
|
||||
Assert.AreEqual(1, all.Count);
|
||||
|
||||
test = "asssert all";
|
||||
iwords.UseDuplicateWordFilter = false; //启用重复词
|
||||
all = iwords.FindAll(test); //未启用重复词
|
||||
Assert.AreEqual("all", all[0].Keyword);
|
||||
Assert.AreEqual(1, all.Count);
|
||||
@@ -113,12 +115,12 @@ namespace ToolGood.Words.Test
|
||||
var ss = iwords.Replace(test, '*');
|
||||
Assert.AreEqual("我是【****", ss);
|
||||
|
||||
test = "我是中国人"; //使用黑名单
|
||||
iwords.SetBlacklist(bl);
|
||||
iwords.UseBlacklistFilter = true;
|
||||
all = iwords.FindAll(test, 1);
|
||||
Assert.AreEqual("中国", all[0].Keyword);
|
||||
Assert.AreEqual(1, all.Count);
|
||||
//test = "我是中国人"; //使用黑名单
|
||||
//iwords.SetBlacklist(bl);
|
||||
//iwords.UseBlacklistFilter = true;
|
||||
//all = iwords.FindAll(test, 1);
|
||||
//Assert.AreEqual("中国", all[0].Keyword);
|
||||
//Assert.AreEqual(1, all.Count);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,9 @@ namespace ToolGood.Words.Test
|
||||
illegalWordsSearch.SetKeywords(s.Split('|'));
|
||||
var str = illegalWordsSearch.Replace("我是中美国人厉害中国完美abcddb好的", '*');
|
||||
|
||||
Assert.AreEqual("我是中美国人厉害**完美***ddb好的", str);
|
||||
//Assert.AreEqual("我是中美国人厉害**完美***ddb好的", str);
|
||||
//注,abc先转abc,再判断abc左右是否为英文或数字,因为后面为d是英文,所以不能过滤
|
||||
Assert.AreEqual("我是中美国人厉害**完美abcddb好的", str);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,16 +42,19 @@ namespace ToolGood.Words.Test
|
||||
iws.UseIgnoreCase = true;
|
||||
iws.UseDBCcaseConverter = true;
|
||||
var iwsFirst = iws.FindFirst(text);
|
||||
Assert.AreEqual("吃", iwsFirst.Keyword);
|
||||
var iwsAll = iws.FindAll(text);
|
||||
Assert.AreEqual(9, iwsAll.Count);
|
||||
Assert.AreEqual(1, iwsAll.Count);// 因为1A20左右都是英文或数字,所以识别失败
|
||||
|
||||
ss.SetKeywords(keywords);
|
||||
var ssFirst = ss.FindFirst(text);
|
||||
Assert.AreEqual("吃", ssFirst);
|
||||
var ssAll = ss.FindAll(text);
|
||||
Assert.AreEqual(9, ssAll.Count);
|
||||
|
||||
sse.SetKeywords(keywords);
|
||||
var sseFirst = sse.FindFirst(text);
|
||||
Assert.AreEqual("吃", sseFirst);
|
||||
var sseAll = sse.FindAll(text);
|
||||
Assert.AreEqual(9, sseAll.Count);
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -2,20 +2,14 @@
|
||||
{
|
||||
public class IllegalWordsSearchResult
|
||||
{
|
||||
internal IllegalWordsSearchResult(string keyword, int start, int end, string srcText)
|
||||
|
||||
internal IllegalWordsSearchResult(string keyword, int start, int end, int index, string matchKeyword, int type)
|
||||
{
|
||||
MatchKeyword = keyword;
|
||||
MatchKeyword = matchKeyword;
|
||||
End = end;
|
||||
Start = start;
|
||||
Keyword = srcText.Substring(Start, end - Start + 1);
|
||||
BlacklistType = BlacklistType.None;
|
||||
}
|
||||
internal IllegalWordsSearchResult(string keyword, int start, int end, string srcText, BlacklistType type)
|
||||
{
|
||||
MatchKeyword = keyword;
|
||||
End = end;
|
||||
Start = start;
|
||||
Keyword = srcText.Substring(Start, end - Start + 1);
|
||||
Index = index;
|
||||
Keyword = keyword;
|
||||
BlacklistType = type;
|
||||
}
|
||||
|
||||
@@ -31,6 +25,12 @@
|
||||
/// 原始文本
|
||||
/// </summary>
|
||||
public string Keyword { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 索引
|
||||
/// </summary>
|
||||
public int Index { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 关键字
|
||||
/// </summary>
|
||||
@@ -39,7 +39,7 @@
|
||||
/// <summary>
|
||||
/// 黑名单类型
|
||||
/// </summary>
|
||||
public BlacklistType BlacklistType { get; private set; }
|
||||
public int BlacklistType { get; private set; }
|
||||
|
||||
|
||||
public override string ToString()
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<RootNamespace>ToolGood.Words</RootNamespace>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<Version>3.0.1.3</Version>
|
||||
<Version>3.0.2.0</Version>
|
||||
<PackageReleaseNotes></PackageReleaseNotes>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
|
||||
Reference in New Issue
Block a user