micro365/src/getNewPrompt.mjs

46 lines
1.1 KiB
JavaScript
Raw Normal View History

2023-08-06 22:24:49 +00:00
import { db } from "./db.mjs"
2023-08-06 22:59:24 +00:00
export default async function getNewPrompt({minCount = 200000, maxCount = 30000000, rarity}) {
2023-08-06 22:24:49 +00:00
const badWords = await db('bad_words')
.select('word')
const prompts = await db('dictionary')
.select('*')
.where({
derivative: 0,
scientific: 0,
})
.andWhere('count', '<', maxCount)
.andWhere('count', '>', minCount)
.andWhere('word', 'not in', badWords)
.whereNotNull('pronunciation')
2023-08-06 22:59:24 +00:00
.orderByRaw('count desc')
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-06 22:59:24 +00:00
const random = getBiasedRng(0,1,rarity,1)
const mix = Math.random()
console.log("RANDOM: "+random)
2023-08-06 22:24:49 +00:00
return array[
parseInt(
array.length * random
)
]
}
2023-08-06 22:59:24 +00:00
2023-08-06 22:24:49 +00:00
db.destroy()
2023-08-06 22:59:24 +00:00
return randomEntry(prompts).count
2023-08-06 22:24:49 +00:00
}
2023-08-06 22:59:24 +00:00
console.log(await getNewPrompt({rarity:1}))
2023-08-06 22:24:49 +00:00