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的curses库使用教程
Apr 11 Python
Python如何实现文本转语音
Aug 08 Python
Python实现的批量修改文件后缀名操作示例
Dec 07 Python
python opencv 二值化 计算白色像素点的实例
Jul 03 Python
Numpy数组array和矩阵matrix转换方法
Aug 05 Python
基于Python中isfile函数和isdir函数使用详解
Nov 29 Python
在win64上使用bypy进行百度网盘文件上传功能
Jan 02 Python
Python实现的北京积分落户数据分析示例
Mar 27 Python
python的json包位置及用法总结
Jun 21 Python
浅析Python 抽象工厂模式的优缺点
Jul 13 Python
python开发入门——set的使用
Sep 03 Python
python自动从arxiv下载paper的示例代码
Dec 05 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 Directory 函数的详解
2013/03/07 PHP
PHP中cookie和session的区别实例分析
2014/08/28 PHP
什么是OneThink oneThink后台添加插件步骤
2016/04/13 PHP
phpcms中的评论样式修改方法
2016/10/21 PHP
Laravel 5.5基于内置的Auth模块实现前后台登陆详解
2017/12/21 PHP
基于jquery的滚动条滚动固定div(附演示下载)
2012/10/29 Javascript
Document:getElementsByName()使用方法及示例
2013/10/28 Javascript
SuperSlide2实现图片滚动特效
2014/06/20 Javascript
javascript RegExp 使用说明
2016/05/21 Javascript
浅谈bootstrap源码分析之tab(选项卡)
2016/06/06 Javascript
深入浅析Vue不同场景下组件间的数据交流
2017/08/15 Javascript
vue2.0$nextTick监听数据渲染完成之后的回调函数方法
2018/09/11 Javascript
tsconfig.json配置详解
2019/05/17 Javascript
详解基于Vue/React项目的移动端适配方案
2019/08/23 Javascript
javascript 使用sleep函数的常见方法详解
2020/04/26 Javascript
python操作摄像头截图实现远程监控的例子
2014/03/25 Python
Python中的下划线详解
2015/06/24 Python
关于Tensorflow中的tf.train.batch函数的使用
2018/04/24 Python
Python中应该使用%还是format来格式化字符串
2018/09/25 Python
Python Web框架之Django框架cookie和session用法分析
2019/08/16 Python
python3 assert 断言的使用详解 (区别于python2)
2019/11/27 Python
tensorflow 自定义损失函数示例代码
2020/02/05 Python
django模型动态修改参数,增加 filter 字段的方式
2020/03/16 Python
在python3.64中安装pyinstaller库的方法步骤
2020/06/02 Python
Django web自定义通用权限控制实现方法
2020/11/24 Python
美国领先的在线旅游网站:Orbitz
2018/11/05 全球购物
Vans澳大利亚官网:购买鞋子、服装及配件
2019/09/05 全球购物
初中女生自我鉴定
2013/12/19 职场文书
会计系个人求职信范文分享
2013/12/20 职场文书
本科生的职业生涯规划范文
2014/01/09 职场文书
大学新生军训自我鉴定
2014/03/18 职场文书
国情备忘录观后感
2015/06/04 职场文书
企业宣传语大全
2015/07/13 职场文书
2015大一新生军训感言
2015/08/01 职场文书
环保建议书作文400字
2015/09/14 职场文书
2016入党积极分子党课学习心得体会
2015/10/09 职场文书