python用类实现文章敏感词的过滤方法示例


Posted in Python onOctober 27, 2019

过滤一遍并将敏感词替换之后剩余字符串中新组成了敏感词语,这种情况就要用递归来解决,直到过滤替换之后的结果和过滤之前一样时才算结束

第一步:建立一个敏感词库(.txt文本)

python用类实现文章敏感词的过滤方法示例

第二步:编写代码在文章中过滤敏感词(递归实现)

# -*- coding: utf-8 -*-
# author 代序春秋
import os
import chardet

# 获取文件目录和绝对路径
curr_dir = os.path.dirname(os.path.abspath(__file__))
# os.path.join()拼接路径
sensitive_word_stock_path = os.path.join(curr_dir, 'sensitive_word_stock.txt')


# 获取存放敏感字库的路径
# print(sensitive_word_stock_path)


class ArticleFilter(object):
  # 实现文章敏感词过滤
  def filter_replace(self, string):
    # string = string.decode("gbk")
    #  存放敏感词的列表
    filtered_words = []
    #  打开敏感词库读取敏感字
    with open(sensitive_word_stock_path) as filtered_words_txt:
      lines = filtered_words_txt.readlines()
      for line in lines:
        # strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
        filtered_words.append(line.strip())
    # 输出过滤好之后的文章
    print("过滤之后的文字:" + self.replace_words(filtered_words, string))

  # 实现敏感词的替换,替换为*
  def replace_words(self, filtered_words, string):
    #  保留新字符串
    new_string = string
    #  从列表中取出敏感词
    for words in filtered_words:
      # 判断敏感词是否在文章中
      if words in string:
        # 如果在则用*替换(几个字替换几个*)
        new_string = string.replace(words, "*" * len(words))
    # 当替换好的文章(字符串)与被替换的文章(字符串)相同时,结束递归,返回替换好的文章(字符串)
    if new_string == string:
      #  返回替换好的文章(字符串)
      return new_string
    # 如果不相同则继续替换(递归函数自己调用自己)
    else:
      #  递归函数自己调用自己
      return self.replace_words(filtered_words, new_string)


def main():
  while True:
    string = input("请输入一段文字:")
    run = ArticleFilter()
    run.filter_replace(string)
    continue


if __name__ == '__main__':
  main()

运行结果:

python用类实现文章敏感词的过滤方法示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python实现生成简单的Makefile文件代码示例
Mar 10 Python
Python json模块使用实例
Apr 11 Python
python实现的简单FTP上传下载文件实例
Jun 30 Python
python实现颜色空间转换程序(Tkinter)
Dec 31 Python
python定时复制远程文件夹中所有文件
Apr 30 Python
利用python在大量数据文件下删除某一行的例子
Aug 21 Python
使用OpenCV-python3实现滑动条更新图像的Canny边缘检测功能
Dec 12 Python
python+requests接口压力测试500次,查看响应时间的实例
Apr 30 Python
如何配置关联Python 解释器 Anaconda的教程(图解)
Apr 30 Python
Python进行统计建模
Aug 10 Python
Python如何获取文件路径/目录
Sep 22 Python
解决Django transaction进行事务管理踩过的坑
Apr 24 Python
通过字符串导入 Python 模块的方法详解
Oct 27 #Python
python实现树的深度优先遍历与广度优先遍历详解
Oct 26 #Python
python图的深度优先和广度优先算法实例分析
Oct 26 #Python
python单例模式原理与创建方法实例分析
Oct 26 #Python
Python aiohttp百万并发极限测试实例分析
Oct 26 #Python
python实现淘宝购物系统
Oct 25 #Python
DJANGO-URL反向解析REVERSE实例讲解
Oct 25 #Python
You might like
PHP的一个完整SMTP类(解决邮件服务器需要验证时的问题)
2006/10/09 PHP
示例详解Laravel重置密码代码重构
2016/08/10 PHP
PHP大文件分割上传 PHP分片上传
2017/08/28 PHP
phpStorm+XDebug+chrome 配置详解
2019/04/01 PHP
php中字符串和整数比较的操作方法
2019/06/06 PHP
JQuery select标签操作代码段
2010/05/16 Javascript
兼容IE与firefox火狐的回车事件(js与jquery)
2010/10/20 Javascript
jQuery实现表格展开与折叠的方法
2015/05/04 Javascript
js实现的倒计时按钮实例
2015/06/24 Javascript
jQuery ajaxSubmit 实现ajax提交表单局部刷新
2016/07/04 Javascript
js微信扫描二维码登录网站技术原理
2016/12/01 Javascript
vue通过路由实现页面刷新的方法
2018/01/25 Javascript
vue2.0 axios跨域并渲染的问题解决方法
2018/03/08 Javascript
ES6 Proxy实现Vue的变化检测问题
2019/06/11 Javascript
Vue的路由及路由钩子函数的实现
2019/07/02 Javascript
Vue项目环境搭建详细总结
2019/09/26 Javascript
[04:52]2015国际邀请赛LGD战队晋级之路
2015/08/14 DOTA
Python中基本的日期时间处理的学习教程
2015/10/16 Python
详解Python的Lambda函数与排序
2016/10/25 Python
Python使用sorted排序的方法小结
2017/07/28 Python
Python实现的插入排序算法原理与用法实例分析
2017/11/22 Python
解决Python一行输出不显示的问题
2018/12/03 Python
python编写简单端口扫描器
2019/09/04 Python
python数据化运营的重要意义
2019/11/25 Python
Python如何基于selenium实现自动登录博客园
2019/12/16 Python
python爬虫实现获取下一页代码
2020/03/13 Python
基于python图书馆管理系统设计实例详解
2020/08/05 Python
Python在线和离线安装第三方库的方法
2020/10/31 Python
Matplotlib中rcParams使用方法
2021/01/05 Python
使用css实现android系统的loading加载动画
2019/07/25 HTML / CSS
ECCO爱步官方旗舰店:丹麦鞋履品牌
2018/01/02 全球购物
印尼综合在线预订网站:Tiket.com(机票、酒店、火车、租车和娱乐)
2018/10/11 全球购物
迅雷Cued工作心得体会
2014/01/27 职场文书
银行贷款委托书范本
2014/10/11 职场文书
2014年财务工作总结与计划
2014/12/08 职场文书
上课睡觉万能检讨书
2015/02/17 职场文书