micro365/src/data/scripts/flagScientific.mjs

47 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-08-05 20:11:43 +00:00
import { db, getWords } from './db.mjs'
const words = await getWords(db)
const allDefinitionsAreScientific = (meanings) => {
const scientificTags = [
"sciences",
"mathematics",
"natural-sciences",
"algebra"
]
2023-08-05 20:11:43 +00:00
let scientific = 0
let totalDefs = 0
for (const meaning of meanings) {
for (const definition of meaning.definitions) {
totalDefs++
if (definition.topics) {
for (const topic of definition.topics) {
if (scientificTags.includes(topic)) {
2023-08-05 20:11:43 +00:00
scientific++
break
}
}
}
}
}
return scientific === totalDefs
}
let updated = []
for (const word of words) {
const res = await db('dictionary')
.select('meanings')
.where('word', word.word)
const meanings = JSON.parse(res[0].meanings)
if (allDefinitionsAreScientific(meanings)) {
await db('dictionary')
.where('word', word.word)
.update('scientific', 1)
updated.push(word.word)
}
}
console.log(`${updated.length} words with only scientific definitions found.`)
db.destroy()