我希望我的机器人删除包含关键字或包含相似字符的消息
I would like my bot to delete the message that contains a keyword or that contains similar characters
在我的机器人中,我实现了一个关键字过滤器,机器人会在聊天中写的每条消息中进行审查,直到现在它仍然有效,但我想改进它,出于尊重的原因,我不会在这里写字,所以我会举一些其他的例子,
机器人会检测您是否写了例如 "vulgar"、"badword"、"hello"
但我想要实现的是检测他们是否写 "hellooo"、"vuulgarr"、vulg4rr
这是我存储单词的基地:
badwords.js
var words = ["vulgar", "vulg4r", "hello", "badword4", "badword5"]
module.exports = words;
这是我的函数,用于检查途中是否出现坏词,拆分任何词,然后如果找到结果则删除消息,使用 indexOf()
index.js
const _ = require('lodash');
const badwords = require('./badwords');
/**
* Functions
*/
// compares every word to badWords array from badWords.js
function checkWord(word) {
return badwords.indexOf(word) > -1;
}
/**
* Main Module
*/
module.exports = function (self, nick, channel, message) {
'use strict';
message = message.toLowerCase();
message = message.split(' ');
nick = nick;
channel = channel.toLowerCase();
for (var i = 0, len = message.length; i < len; i++) {
if (checkWord(message[i])) {
self.send('.ban', channel, nick);
}
}
}
有改进的想法吗?谢谢
更复杂的方法
我们可以在两个字符串上有两个指针进行比较,但在重复时跳过偏移量:
function checkString(message, keyword) {
while(message.length > 0) {
if(checkPrefix(message, keyword)) return true
message = message.substr(1)
}
}
function checkPrefix(message, keyword) { // keyword is one of the keywords
let om = 0, ok = 0
while (true) {
if (ok >= keyword.length)
return true // we have finished reading keyword, and everything matched
if(om >= message.length)
return false // message is shorter than keyword
while (om + 1 < message.length && message.charAt(om) === message.charAt(om + 1))
om++ // skip consecutive repetitions in message
while (ok + 1 < keyword.length && keyword.charAt(ok) === keyword.charAt(ok + 1))
ok++ // skip consecutive repetitions in keyword
if (message.charAt(om) !== message.charAt(ok)) return false // encountered an inconsistent character
}
}
更简单的方法
只需扫描字符串中的重复项,然后先将其删除。
function removeDuplicates(string) {
for (let i = 0; i < string.length - 1; ) {
if (string.charAt(i) === string.charAt(i + 1)) {
string = string.substr(0, i) + string.substr(i + 1) // skip string[i]
} else {
i++ // not duplicate, proceed to next pair
}
}
}
那你可以直接比较:
removeDuplicates(message).indexOf(removeDuplicates(keyword)) !== -1
你可以这样应用:
for (const part in message.split(" ")) {
for (word in words) {
if (removeDuplicates(part).indexOf(removeDuplicates(word)) !== -1)
self.send(".ban", ...)
break
}
}
在我的机器人中,我实现了一个关键字过滤器,机器人会在聊天中写的每条消息中进行审查,直到现在它仍然有效,但我想改进它,出于尊重的原因,我不会在这里写字,所以我会举一些其他的例子,
机器人会检测您是否写了例如 "vulgar"、"badword"、"hello"
但我想要实现的是检测他们是否写 "hellooo"、"vuulgarr"、vulg4rr
这是我存储单词的基地:
badwords.js
var words = ["vulgar", "vulg4r", "hello", "badword4", "badword5"]
module.exports = words;
这是我的函数,用于检查途中是否出现坏词,拆分任何词,然后如果找到结果则删除消息,使用 indexOf()
index.js
const _ = require('lodash');
const badwords = require('./badwords');
/**
* Functions
*/
// compares every word to badWords array from badWords.js
function checkWord(word) {
return badwords.indexOf(word) > -1;
}
/**
* Main Module
*/
module.exports = function (self, nick, channel, message) {
'use strict';
message = message.toLowerCase();
message = message.split(' ');
nick = nick;
channel = channel.toLowerCase();
for (var i = 0, len = message.length; i < len; i++) {
if (checkWord(message[i])) {
self.send('.ban', channel, nick);
}
}
}
有改进的想法吗?谢谢
更复杂的方法
我们可以在两个字符串上有两个指针进行比较,但在重复时跳过偏移量:
function checkString(message, keyword) {
while(message.length > 0) {
if(checkPrefix(message, keyword)) return true
message = message.substr(1)
}
}
function checkPrefix(message, keyword) { // keyword is one of the keywords
let om = 0, ok = 0
while (true) {
if (ok >= keyword.length)
return true // we have finished reading keyword, and everything matched
if(om >= message.length)
return false // message is shorter than keyword
while (om + 1 < message.length && message.charAt(om) === message.charAt(om + 1))
om++ // skip consecutive repetitions in message
while (ok + 1 < keyword.length && keyword.charAt(ok) === keyword.charAt(ok + 1))
ok++ // skip consecutive repetitions in keyword
if (message.charAt(om) !== message.charAt(ok)) return false // encountered an inconsistent character
}
}
更简单的方法
只需扫描字符串中的重复项,然后先将其删除。
function removeDuplicates(string) {
for (let i = 0; i < string.length - 1; ) {
if (string.charAt(i) === string.charAt(i + 1)) {
string = string.substr(0, i) + string.substr(i + 1) // skip string[i]
} else {
i++ // not duplicate, proceed to next pair
}
}
}
那你可以直接比较:
removeDuplicates(message).indexOf(removeDuplicates(keyword)) !== -1
你可以这样应用:
for (const part in message.split(" ")) {
for (word in words) {
if (removeDuplicates(part).indexOf(removeDuplicates(word)) !== -1)
self.send(".ban", ...)
break
}
}