InfoQServerless 实战:3 分钟实现文本敏感词过滤( 三 )
if p.isWord:result.append(p.word)p = self.rootcurrentposition += 1return result
# 加载敏感词库函数def parse(self, path):with open(path, encoding='utf-8') as f:for keyword in f:temp_root = self.rootfor char in str(keyword).strip():if char not in temp_root.next:temp_root.next[char] = Node()temp_root = temp_root.next[char]temp_root.isWord = Truetemp_root.word = str(keyword).strip()
# 敏感词替换函数def wordsFilter(self, text):""":param ah: AC 自动机:param text: 文本:return: 过滤敏感词之后的文本"""result = list(set(self.search(text)))for x in result:m = text.replace(x, '*' * len(x))text = mreturn text
def response(msg, error=False):return_data = http://news.hoteastday.com/a/{"uuid": str(uuid.uuid1()),"error": error,"message": msg}print(return_data)return return_data
acAutomation = AcAutomation()path = './sensitive_words'acAutomation.parse(path)
def main_handler(event, context):try:sourceContent = json.loads(event["body"])["content"]return response({"sourceContent": sourceContent,"filtedContent": acAutomation.wordsFilter(sourceContent)})except Exception as e:return response(str(e), True)
最后 , 为了方便本地测试 , 我们可以再增加以下代码:
def test():event = {"requestContext": {"serviceId": "service-f94sy04v","path": "/test/{path}","httpMethod": "POST","requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef","identity": {"secretId": "abdcdxxxxxxxsdfs"},"sourceIp": "14.17.22.34","stage": "release"},"headers": {"Accept-Language": "en-US,en,cn","Accept": "text/html,application/xml,application/json","Host": "service-3ei3tii4-251000691.ap-guangzhou.apigateway.myqloud.com","User-Agent": "User Agent String"},"body": "{\"content\":\" 这是一个测试的文本 , 我也就呵呵了\"}","pathParameters": {"path": "value"},"queryStringParameters": {"foo": "bar"},"headerParameters": {"Refer": "10.0.2.14"},"stageVariables": {"stage": "release"},"path": "/test/value","queryString": {"foo": "bar","bob": "alice"},"httpMethod": "POST"}print(main_handler(event, None))
if __name__ == "__main__":test()
完成之后 , 就可以进行测试运行 , 例如我的字典是:
呵呵测试
执行之后结果:
{'uuid': '9961ae2a-5cfc-11ea-a7c2-acde48001122', 'error': False, 'message': {'sourceContent': '这是一个测试的文本 , 我也就呵呵了', 'filtedContent': '这是一个 ** 的文本 , 我也就 ** 了'}}
接下来 , 我们将代码部署到云端 , 新建 serverless.yaml:
sensitive_word_filtering:component: "@serverless/tencent-scf"inputs:name: sensitive_word_filteringcodeUri: ./exclude:- .gitignore- .git/**- .serverless- .envhandler: index.main_handlerruntime: Python3.6region: ap-beijingdescription: 敏感词过滤memorySize: 64timeout: 2events:- apigw:name: serverlessparameters:environment: releaseendpoints:- path: /sensitive_word_filteringdescription: 敏感词过滤method: POSTenableCORS: trueparam:- name: contentposition: BODYrequired: 'FALSE'type: stringdesc: 待过滤的句子
然后通过 sls --debug 进行部署 , 部署结果:
本文插图
推荐阅读
- 快科技|联想拯救者电竞手机入网:首发90W快充 15分钟充满4000mAh
- 智能25 分钟超去年全天!华米科技 618 开门红一小时战报公布
- 增长京东汽车开门红首战告捷 米其林开场5分钟成交额破百万
- 电子商务实战专家|华为再强,还离不开世界工厂,自嗨不是最好的选择?
- 搜狐新闻|充电五分钟,看剧四小时!Reno4 Slogan再升级,这些必杀技很能打
- 分享科技小达人|华为EMUI10自带高级输入法,1分钟打200字,难怪都说华为手机好用
- |1599元迄今最便宜5G手机!Redmi 10X首销5分钟破亿
- 直播天猫6·18:熬夜看直播用户89分钟下单20亿
- 数码东东|真香打脸!NEX3s太贵?渠道分分钟砍掉748
- 诸神的恩宠|登上珠峰的5G手机,8分钟卖出10万台,揭秘荣耀X10到底牛在哪儿