
核心代码
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);
}
});
}
});
}

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





