Browse Source

ft: 代码调整

master
SisMaker 4 years ago
parent
commit
1aa845df28
4 changed files with 25 additions and 14 deletions
  1. +14
    -3
      README.md
  2. +8
    -8
      src/eAcs.erl
  3. +2
    -2
      src/genAcs.erl
  4. +1
    -1
      src/test/acTest.erl

+ 14
- 3
README.md View File

@ -1,9 +1,20 @@
eAcs
=====
An OTP library
基于ac算法实现的快速高效的erl版本敏感词匹配,检查,过滤代码
Build
-----
$ rebar3 escriptize
$ rebar3 escriptize -> genAcs
$ rebar3 compile
Uses
-----
创建 acsTree.erl
脚本生成:./genAcs SWord.txtFile OutputDir 或者函数嗲用
函数调用: genAcs:main([SWord.txtFile, OutputDir])
匹配 检查 过滤 敏感词
eAcs:matchSw/1 %% 返回匹配的敏感词列表
eAcs:isHasSw/1 %% 检查是否包含敏感词
eAcs:replaceSw/1 %% 替换敏感词

+ 8
- 8
src/eAcs.erl View File

@ -21,14 +21,14 @@ doMatch(<>, State, Index, MatchList) ->
doMatch(Tail, NewState, Index + 1, NewMatchList).
matchInner(Word, State, Index, MatchList) ->
Node = acsTrees:goto(State),
Node = acsTree:goto(State),
case Node of
undefined ->
case State of
0 ->
{State, MatchList};
_ ->
{NextState, _} = acsTrees:failOut(State),
{NextState, _} = acsTree:failOut(State),
matchInner(Word, NextState, Index, MatchList)
end;
_ ->
@ -41,7 +41,7 @@ matchInner(Word, State, Index, MatchList) ->
0 ->
{State, MatchList};
_ ->
{NextState, _} = acsTrees:failOut(State),
{NextState, _} = acsTree:failOut(State),
matchInner(Word, NextState, Index, MatchList)
end
end
@ -50,7 +50,7 @@ matchInner(Word, State, Index, MatchList) ->
getOutput(0, _Index, MatchList) ->
MatchList;
getOutput(State, Index, MatchList) ->
{FailState, Pattern} = acsTrees:failOut(State),
{FailState, Pattern} = acsTree:failOut(State),
case Pattern of
undefined ->
getOutput(FailState, Index, MatchList);
@ -76,14 +76,14 @@ doMatch(<>, State) ->
end.
matchInner(Word, State) ->
Node = acsTrees:goto(State),
Node = acsTree:goto(State),
case Node of
undefined ->
case State of
0 ->
State;
_ ->
{NextState, _} = acsTrees:failOut(State),
{NextState, _} = acsTree:failOut(State),
matchInner(Word, NextState)
end;
_ ->
@ -100,7 +100,7 @@ matchInner(Word, State) ->
0 ->
State;
_ ->
{NextState, _} = acsTrees:failOut(State),
{NextState, _} = acsTree:failOut(State),
matchInner(Word, NextState)
end
end
@ -109,7 +109,7 @@ matchInner(Word, State) ->
getOutput(0) ->
false;
getOutput(State) ->
{FailState, Pattern} = acsTrees:failOut(State),
{FailState, Pattern} = acsTree:failOut(State),
case Pattern == undefined orelse FailState == 0 of
true ->
false;

+ 2
- 2
src/genAcs.erl View File

@ -120,7 +120,7 @@ findFailNode(Word, State, FailState, Goto, Fail) ->
end.
genHead() ->
<<"-module(acsTrees).\n\n-compile([deterministic, no_line_info]).\n\n-export([goto/1, failOut/1]).\n\n">>.
<<"-module(acsTree).\n\n-compile([deterministic, no_line_info]).\n\n-export([goto/1, failOut/1]).\n\n">>.
genGoto(Goto, StrAcc) ->
Kvs = maps:to_list(Goto),
@ -171,5 +171,5 @@ genErl(WriteDir, Goto, Fail, Output) ->
HeadStr = genHead(),
GotoStr = genGoto(Goto, HeadStr),
FailStr = genFailOut(lists:sort(maps:keys(Goto)), Fail, Output, GotoStr),
FileName = filename:join([WriteDir, "acsTrees.erl"]),
FileName = filename:join([WriteDir, "acsTree.erl"]),
file:write_file(FileName, FailStr).

+ 1
- 1
src/test/acTest.erl View File

@ -5,7 +5,7 @@
-compile([export_all, nowarn_export_all]).
test1() ->
acTc:ts(1000000, acsTrees, goto, [63870]).
acTc:ts(1000000, acsTree, goto, [63870]).
test2() ->
acTc:ts(1000000, eAcs, matchSw, [<<"fdsfads拉法叶舰fds淫秽ffdsfdsffdddd"/utf8>>]).

Loading…
Cancel
Save