This repository has been archived on 2024-07-19. You can view files and clone it, but cannot push or open issues or pull requests.
infinite-wordle/script/buildDictionary.ts
Marc Cataford e501f01dfa
ci: parallelizes build tasks (#4)
* ci: parallelizes build tasks

* fix: duplicated attr

* ci: do not yarn if cache

* fix: artifact pull path

* chore: lint

* build: lint script

* ci: linting step

* ci: url extraction for PR comment
2022-01-25 23:21:59 -05:00

46 lines
1.3 KiB
TypeScript

/* eslint-disable no-console */
import { promises as fs } from 'fs'
import * as sqlite3 from 'sqlite3'
async function getDictionarySource(
url: string,
databasePath: string,
): Promise<void> {
const database = new sqlite3.Database(databasePath)
const queries: string[] = []
queries.push(
'CREATE TABLE puzzles (id INTEGER PRIMARY KEY AUTOINCREMENT, solution TEXT NOT NULL);',
)
const src = await fs.readFile(url, 'utf-8')
const lines = src.split('\n').map((word) => word.toLowerCase())
console.group('Building database')
const dbStart = Date.now()
const queryParts: string[] = []
for (const line of lines) {
// Reject anything non-alpha
if (!line.match(/^[a-z]{3,}$/)) continue
queryParts.push(`(NULL, "${line}")`)
}
console.info(`Selected ${queryParts.length} words from file`)
queries.push(`INSERT INTO puzzles VALUES ${queryParts.join(',')};`)
database.serialize(() => {
for (const query of queries) database.run(query)
})
const dbEnd = Date.now() - dbStart
console.info(`Inserted ${queries.length} records (${dbEnd} ms)`)
console.groupEnd()
}
const WORD_LIST = './dictionary_common.txt'
const DATABASE_PATH = './puzzles.sqlite'
getDictionarySource(WORD_LIST, DATABASE_PATH).catch((e) => {
throw e
})