敏感詞過濾

2022-06-07 06:06:08 字數 2457 閱讀 7024

分享乙個敏感詞過濾方法

1

public

class

wordfiltercontroller : basecontroller

225 result_str =result;

2627

if (s_filters == null)28

3132

bool check = false;33

foreach (string str in

s_filters)

3440

41bool bfiltered = true;42

while

(bfiltered)

4354

if (result_index_end + 1 >=result.length)

5559

int new_index = result.indexof(one_s, result_index_end + 1

, stringcomparison.ordinalignorecase);

60if (new_index == -1)61

65if (idx > 0 && new_index - result_index_end > filter_deep + 1)66

70 result_index_end =new_index;

7172

if (result_index_start == -1)73

76 idx++;77}

7879

if(bfiltered)

8085 check = true;86

string result_left = result.substring(0

, result_index_start);

87for (int i = result_index_start; i <= result_index_end; i++)

8891

string result_right = result.substring(result_index_end + 1

);92 result = result_left +result_right;93}

94}95}

96 result_str =result;

97return

check;98}

99100 }

我用的是mvc框架,直接重寫defaultmodelbinder裡面的bindmodel方法

1

public

class

filtermodelbinder : defaultmodelbinder215

}16return

value;17}

18 }

在網上還查到另一種重寫imodelbinder的方法也可以實現,這邊就不記錄了

modelbinders.binders.defaultbinder = new filtermodelbinder();

搞定~【2018.04.09】更新

在後續專案測試中發現,這樣給全部的model都綁上敏感詞過濾有些蠢。主要是1.開發敏感詞管理模組時,會自己把自己和諧了2.一些敏感詞是數字或字元可能會和guid衝突導致值傳遞出現bug

所以稍微改了下,只針對部分entity進行過濾,並且排除id

這是新的modelbinder,這次只重寫setproperty方法

1

public

class

wordfiltermodelbinder : defaultmodelbinder214

}15base

.setproperty(controllercontext, bindingcontext, propertydescriptor, value);16}

17 }

1

= new ssoct.library.custommodelbinder();

2 modelbinders.binders.add(typeof(dal.models.activity), new

wordfiltermodelbinder());

3 modelbinders.binders.add(typeof(dal.models.comment), new

wordfiltermodelbinder());

這樣就可以針對性的進行過濾了,但是過濾層級只到entity,不能精確到只針對某個字段

可能有更好的辦法,現在想是用attribute應該可以實現針對字段過濾,等後續再更新……

敏感詞過濾

最近需要實現對聊天裡的敏感詞過濾,要求比較簡單,只需要對字型檔中存在的關鍵字進行匹配,所以不需要非常複雜的實現,但是需要能夠快速地對乙個關鍵字集合進行匹配。搜了一下相關的資料,比較簡單的乙個演算法是使用aho corasick演算法,以下簡稱ac演算法。該演算法的基本思想中包含了kmp演算法,即利用...

敏感詞過濾

敏感詞過濾 防沉迷,基本所有上線系統都會用的基礎功能吧,網上挺多 的,也很多種方式,看的有點繞寫了個自己感覺比較乾淨清楚的,結構也比較簡單,記錄一下insert 和 審查 檢查 都是遞迴,效率還好,win release版本5700的樣本,length 1000的 str 大概0.2ms支援比如敏感...

敏感詞過濾

function function buildtree words 子節點指標 val null,當前節點的字元,null表示根節點 back null,跳躍指標,也稱失敗指標 parent null,父節點指標,accept false 是否形成了乙個完整的詞彙,中間節點也可能為true make...