micro365/src/getNewPrompt.mjs

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-08-06 22:24:49 +00:00
import { db } from "./db.mjs"
2023-08-11 12:40:19 +00:00
import logger from "./logger.mjs"
2023-08-06 22:24:49 +00:00
const blocklist = db.union([
db('bad_words').select('word'),
2023-08-10 15:44:15 +00:00
db('medical_dictionary').select('word'),
db('published').select('word')
])
2023-08-06 22:24:49 +00:00
export default async function getNewPrompt({ minCount = 200000, maxCount = 30000000, rarityBias = 0.5 }) {
2023-08-11 12:40:19 +00:00
const childLogger = logger.child({minCount,maxCount,rarityBias})
childLogger.trace("getNewPrompt called")
2023-08-06 22:24:49 +00:00
const prompts = await db('dictionary')
.select('*')
.where({
derivative: 0,
scientific: 0,
})
.andWhere('count', '<', maxCount)
.andWhere('count', '>', minCount)
.andWhere('word', 'not in', blocklist)
.whereRaw('length(word) > 3')
2023-08-06 22:24:49 +00:00
.whereNotNull('pronunciation')
2023-08-06 22:59:24 +00:00
.orderByRaw('count desc')
2023-08-11 14:21:24 +00:00
.catch(error=>{throw error})
2023-08-08 14:52:17 +00:00
const getBiasedRng = (min, max, bias, influence) => {
const random = Math.random() * (max - min) + min
const mix = Math.random() * influence
return random * (1 - mix) + bias * mix
}
2023-08-06 22:24:49 +00:00
const randomEntry = (array) => {
2023-08-09 10:26:08 +00:00
const random = getBiasedRng(0, 1, rarityBias, 1)
2023-08-06 22:59:24 +00:00
const mix = Math.random()
2023-08-06 22:24:49 +00:00
return array[
parseInt(
array.length * random
)
]
}
2023-08-06 22:59:24 +00:00
//await db.destroy()
2023-08-09 10:26:08 +00:00
return await randomEntry(prompts)
2023-08-06 22:24:49 +00:00
}
//console.dir(await getNewPrompt({}))
2023-08-06 22:59:24 +00:00
2023-08-09 17:27:30 +00:00
//console.log(await blocklist)
2023-08-06 22:24:49 +00:00