async def match_text(request): if request.method == "GET": text = request.args.get("text") algorithm = request.args.get("algorithm") keywords = request.args.get("keywords") else: text = get_post_args(request, "text") algorithm = get_post_args(request, "algorithm", "bs") keywords = get_post_args(request, "keywords") if not text: return Response.json({ "code": 400, "message": "You should specify necessary params `text`", "data": None }) else: gfw = DFAFilter() if algorithm == "dfa" else BSFilter() if keywords: for ele in keywords: gfw.add(ele) else: gfw.parse("filter/keywords") result = list(gfw.match(message=text)) return Response.json({ "code": 200, "message": "success", "data": { "result": result } })
from filter import DFAFilter f = DFAFilter() f.add("sexy") y = f.filter("hello sexy baby") print(y)