node实现敏感词过滤及敏感词库

本文介绍了一个用于过滤敏感词汇的Node.js程序实现细节。该程序能够读取多个文本文件中的敏感词库,并在输入文本中查找并替换这些词汇。特别地,程序还考虑到了图片标签的处理,确保了其不会被误判为敏感内容。

核心代码 

import * as fs from 'fs';
import * as path from 'path';
import * as readline from 'readline';

export default class BadWords {
    private static _instance: BadWords;
    private data: Array<string> = [];

    constructor () {
      const files = fs.readdirSync(path.resolve(__dirname, './libs/'));
      files.forEach(file => {
        if (path.extname(file) === '.txt') {
          const rl = readline.createInterface({
            input: fs.createReadStream(path.resolve(__dirname, './libs/', file))
          });
          rl.on('line', line => {
            if (line) {
              if (line.substring(line.length - 1) === ',') {
                line = line.substring(0, line.length - 1);
              }
              this.data.push(line);
            }
          });
        }
      });
    }

 
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值