Skip to content

Commit 1055d41

Browse files
committedDec 13, 2019
More filters!
1 parent 7e8b2c8 commit 1055d41

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed
 

‎config-sample.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
"blocked": [
2424
"somebody@example.com"
2525
],
26-
"wordFilterURL": "http://monoroch.net/kinshi/housouKinshiYougo.xml"
26+
"filtering": true,
27+
"wordFilterURL": "http://monoroch.net/kinshi/housouKinshiYougo.xml",
28+
"wordFilterFiles": [
29+
"./filter.txt"
30+
]
2731
},
2832
"math": {
2933
"size": 20

‎filter.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// You type your desired words that you don't want the computer to learn.
2+
// Each line should only have one word.
3+
// Thus the whole sentence that contains these words won't be learned.
4+
// For example,
5+
6+
てすとてすてすてすてすとてすて // <- A string containing this sequence won't be learned.

‎src/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ type Config = {
3232
* It only prevents from learning. Still send a reply to them.
3333
*/
3434
blocked: [string]
35+
filtering: boolean
3536
wordFilterURL: string
37+
wordFilterFiles: [string]
3638
}
3739
math: {
3840
size: number

‎src/modules/markov-speaking/word-filter.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as XML from 'xml2js'
22
import config from '../../config'
33
import fetch from 'node-fetch'
4+
import * as fs from 'fs'
5+
46
export default class WordFilter {
57
private filterURL: string
68
private initialized: boolean = false
@@ -25,10 +27,26 @@ export default class WordFilter {
2527
dict.push(i.word[0]._)
2628
dict.push(i.word[0].$.reading)
2729
}
30+
31+
for(let filePath of config.markovSpeaking.wordFilterFiles) {
32+
console.info(`Loading filtered word list from ${filePath}`)
33+
const fileContent = fs.readFileSync(filePath, 'utf-8')
34+
for(let word of fileContent.split('\n')) {
35+
let m = word.match('//')
36+
if(m) {
37+
/*
38+
* "abra // cadabra" => "abra"
39+
*/
40+
word = word.substring(0, m.index).trim()
41+
}
42+
if(word) dict.push(word)
43+
}
44+
}
2845
return dict
2946
}
3047

3148
async init() {
49+
if(!config.markovSpeaking.filtering) return false
3250
try {
3351
this.wordDict = await this.fetchDict()
3452
} catch(e) {
@@ -44,10 +62,11 @@ export default class WordFilter {
4462
this.wordRegex = RegExp(regexStr)
4563

4664
this.initialized = true
65+
return true
4766
}
4867

4968
isBad(str: string) {
50-
if(this.wordRegex.exec(str)) return true
69+
if(this.initialized && this.wordRegex.exec(str)) return true
5170
else return false
5271
}
5372
}

‎update.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#!/bin/bash
22
git pull
3-
npm run build
3+
npm install
4+
npm run build

0 commit comments

Comments
 (0)
Please sign in to comment.