A Sensitive Detect tool(EN)
Installation
npm i wx-sensitive-detection
Use
<template>
<v-textarea v-bind="textareaProps" v-model="internalValue" @compositionend="handleCompositionEnd"
@compositionstart="handleCompositionStart" @input="handleInput" />
</template>
<script>
import { replaceAll } from './index.js';
import { ref, computed, watch, defineComponent } from 'vue';
export default defineComponent({
name: 'SensitiveTextarea',
props: {
modelValue: {
type: [String, Number],
default: ''
},
},
setup(props) {
const internalValue = ref(props.modelValue);
// 计算属性
const textareaProps = computed(() => {
const { modelValue, style, ...restProps } = props;
console.log(modelValue, style)
return restProps;
});
watch(() => props.modelValue, (newValue) => {
internalValue.value = newValue;
});
return {
internalValue,
textareaProps,
};
},
data() {
return {
ifTyping: false,
}
},
methods: {
handleCompositionStart() {
this.ifTyping = true
},
handleCompositionEnd() {
this.ifTyping = false
},
handleInput() {
if (this.ifTyping) {//如果正在打字,则不做处理
} else {//没有打字,检测
setTimeout(() => {
const result = replaceAll(this.internalValue);
for (const word of result) {
let replaceStr = '*'.repeat(word.length);
this.internalValue = this.internalValue.replace(word, replaceStr);
}
}, 100)
}
}
}
});
</script>
Some Bug
- 1.0.1
for ABC when AB is sensitive word while ABC is sensitive word, the filter result will be **C, which means the algorithm will filter the shorter word first
- 1.0.2
modify the version 1.0.1 BUG and found a new BUG
for ABC when AB is sensitive word while BC is sensitive word,the filter result will be **C, which means the algorithm will filter the fronter word first
基于前缀树的高效敏感词检测工具(CN)
安装
npm i wx-sensitive-detection
使用
<template>
<v-textarea v-bind="textareaProps" v-model="internalValue" @compositionend="handleCompositionEnd"
@compositionstart="handleCompositionStart" @input="handleInput" />
</template>
<script>
import { replaceAll } from './index.js';
import { ref, computed, watch, defineComponent } from 'vue';
export default defineComponent({
name: 'SensitiveTextarea',
props: {
modelValue: {
type: [String, Number],
default: ''
},
},
setup(props) {
const internalValue = ref(props.modelValue);
// 计算属性
const textareaProps = computed(() => {
const { modelValue, style, ...restProps } = props;
console.log(modelValue, style)
return restProps;
});
watch(() => props.modelValue, (newValue) => {
internalValue.value = newValue;
});
return {
internalValue,
textareaProps,
};
},
data() {
return {
ifTyping: false,
}
},
methods: {
handleCompositionStart() {
this.ifTyping = true
},
handleCompositionEnd() {
this.ifTyping = false
},
handleInput() {
if (this.ifTyping) {//如果正在打字,则不做处理
} else {//没有打字,进行检测
setTimeout(() => {
const result = replaceAll(this.internalValue);
for (const word of result) {
let replaceStr = '*'.repeat(word.length);
this.internalValue = this.internalValue.replace(word, replaceStr);
}
}, 100)
}
}
}
});
</script>
一些错误
- 1.0.1
对于 ABC,当 AB 是敏感词而 ABC 也是敏感词时,过滤结果将是 **C,这意味着算法会优先过滤较短的词
- 1.0.2
修改了 1.0.1 版本的 BUG,发现了一个新 BUG
对于 ABC,当 AB 是敏感词而 BC 是敏感词时,过滤结果将是 **C,这意味着算法会优先过滤前面的词