new deleteMisspelled function
This commit is contained in:
parent
d6865498bf
commit
bd498ae966
Binary file not shown.
|
@ -0,0 +1,39 @@
|
|||
import Knex from 'knex';
|
||||
import Spellchecker, { isMisspelled } from 'spellchecker'
|
||||
|
||||
const db = Knex({
|
||||
client: 'sqlite3', // or 'better-sqlite3'
|
||||
connection: {
|
||||
filename: "../database"
|
||||
}
|
||||
})
|
||||
|
||||
const getWords = async (db) => {
|
||||
return db
|
||||
.select("word")
|
||||
.from("dictionary")
|
||||
}
|
||||
|
||||
const words = await getWords(db)
|
||||
console.dir(words)
|
||||
for (const element of words) {
|
||||
if(isMisspelled(element.word)){
|
||||
try {
|
||||
await db('dictionary')
|
||||
.where('word', element.word)
|
||||
.del()
|
||||
console.log("deleted non-word " + element.word)
|
||||
} catch (error) {
|
||||
console.error(error.message)
|
||||
} finally {
|
||||
console.log()
|
||||
}
|
||||
}
|
||||
console.dir(element.word)
|
||||
}
|
||||
|
||||
try {
|
||||
db.destroy()
|
||||
} catch (error) {
|
||||
console.error(error.message)
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
const Spellchecker = require("spellchecker")
|
||||
const sqlite3 = require("sqlite3").verbose()
|
||||
const db = new sqlite3.Database("../database")
|
||||
|
||||
db.serialize(() => {
|
||||
db.each("SELECT * FROM dictionary", [],
|
||||
function (err, row) {
|
||||
if (err) {
|
||||
return console.error(err.message)
|
||||
}
|
||||
if (Spellchecker.isMisspelled(row.word)) {
|
||||
db.run("DELETE from dictionary WHERE word=?", [row.word], function (err) { if (err) { return console.error(err.message) } })
|
||||
console.log(`deleted non-word ${row.word}`)
|
||||
}
|
||||
},
|
||||
function (err, rows) {
|
||||
if (err) { return console.error(err.message) }
|
||||
console.log(`${rows} rows`)
|
||||
db.close()
|
||||
console.log("db closed")
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue