Python Show-Me-the-Code 第 0011 题 过滤敏感词

来源:互联网 发布:淘宝客买家入口 编辑:程序博客网 时间:2024/05/29 19:17

第 0011 题: 敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。

北京程序员公务员领导牛比牛逼你娘你妈lovesexjiangge

思路:让用户输入词语,然后查找输入中是否含有敏感词,如果是则打印出 Freedom,否则打印出 Human Rights即可。为了方便交互,使用了Python的CMD模块。


0011.过滤敏感词.py

#!/usr/bin/env python#coding: utf-8import cmdimport sys# 存放敏感词文件的路径filtered_words_filepath = '/home/bill/Desktop/filtered_words.txt'class CLI(cmd.Cmd):    def __init__(self):        # 初始化,提取敏感词列表        cmd.Cmd.__init__(self)        self.intro = 'Filtered Words Detective'        self.words = map(lambda i: i.strip('\n'), open(filtered_words_filepath).readlines())        self.prompt = "> "    # define command prompt    def default(self, line):        if any([i in line for i in self.words]):            print 'Freedom'        else:            print 'Human Rights'    def do_quit(self, arg):        exit()        return Trueif __name__ =="__main__":    cli = CLI()    cli.cmdloop()

这里写图片描述

0 0