/ support / question_samples / filter_questions.mjs
filter_questions.mjs
 1  import { parse } from "file://c:/Users/william/AppData/Roaming/npm/node_modules/csv/dist/cjs/index.cjs"
 2  import { promises as fs } from "fs"
 3  import os from "os"
 4  
 5  function makeTextBlock(text) {
 6    let output = ``
 7    for (let i = 0; i < text.length; i++) {
 8      if (i % 60 == 0) {
 9        // while (text[i] != " ") {
10        //   i--
11        //   console.log(text[i], i)
12        // }
13        // output += text.substr(Math.max(output.length-1, 0), output.length+i)
14        output += "\n"
15      }
16      output += text[i]
17    }
18    output += "\n"
19    return output
20  }
21  
22  function validateQuestion(questionText) {
23    const failedTests = new Array()
24    ,     questionMarks = questionText.match(/\?+/g)
25    
26    if (!(/^[\w,'"\.\-\!\?\$\&\\\s]+$/ig).test(questionText)) {
27      failedTests.push(true)
28    }
29    if ((/\b(cocks)\b|\b(cock)\b|\b(pussy)\b|\b(bitch)\b|\b(fuck)\b|\b(faggot)\b/g).test(questionText)) {
30      failedTests.push(true)
31    }
32    if (questionMarks && questionMarks.length > 1) {
33      failedTests.push(true)
34    }
35    if (!(/\?/g).test(questionText)) {
36      failedTests.push(true)
37    }
38    if (questionText.length > 240 || questionText.length < 75) {
39      failedTests.push(true)
40    }
41  
42    return failedTests.length == 0
43  }
44  
45  const allQuestions = await fs.readFile(`./questions.csv`)
46  ,     records = parse(allQuestions)
47  
48  const questions = new Map
49  records.on('readable', () => {
50    let record
51    while ((record = records.read()) !== null) {
52      if (validateQuestion(record[1])) {
53        questions.set(record[0],record[1])
54      }
55    }
56  })
57  
58  let output = `const questions = new Map([\n`
59  
60  records.on('end', () => {
61    let limit = 500
62    let skip = 8888
63  
64    for (const [asker, question] of questions.entries()) {
65      if (skip != 0) {
66        skip--
67        continue
68      }
69      if (limit > 0) {
70        output += `\t[\`${asker}\`,\`${question}\`],\n`
71      } else {
72        break
73      }
74      limit--
75    }
76  
77    output += `])\n`
78  
79    fs.writeFile("questionSamples.js", output)
80  })