getNewPrompt function

This commit is contained in:
Andrzej Stepien 2023-08-07 00:24:49 +02:00
parent 77ec06b59e
commit 3d434a570c
3 changed files with 70 additions and 0 deletions

Binary file not shown.

41
src/getNewPrompt.mjs Normal file
View File

@ -0,0 +1,41 @@
import { db } from "./db.mjs"
import { randomSkewNormal, randomSkewNormalTrimmed } from "./skewNormal.mjs"
const maxCount = 30000000
const minCount = 200000
export default async function getNewPrompt() {
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')
.orderBy('count')
const randomEntry = (array) => {
//const random = (randomSkewNormal(Math.random,0,2,0)/6)
const random = Math.random()
return array[
parseInt(
array.length * random
)
]
}
db.destroy()
return randomEntry(prompts)
}
console.log(await getNewPrompt())

29
src/skewNormal.mjs Normal file
View File

@ -0,0 +1,29 @@
const randomNormals = (rng) => {
let u1 = 0, u2 = 0;
//Convert [0,1) to (0,1)
while (u1 === 0) u1 = rng();
while (u2 === 0) u2 = rng();
const R = Math.sqrt(-2.0 * Math.log(u1));
const Θ = 2.0 * Math.PI * u2;
return [R * Math.cos(Θ), R * Math.sin(Θ)];
}
export const randomSkewNormal = (rng, ξ, ω, α = 0) => {
const [u0, v] = randomNormals(rng);
if (α === 0) {
return ξ + ω * u0;
}
const 𝛿 = α / Math.sqrt(1 + α * α);
const u1 = 𝛿 * u0 + Math.sqrt(1 - 𝛿 * 𝛿) * v;
const z = u0 >= 0 ? u1 : -u1;
return ξ + ω * z;
}
export const randomSkewNormalTrimmed = ()=>{
let random = 1
do {
random = randomSkewNormal(Math.random,0,2,0)
} while (random<0 || random>0.999999)
return random
}