#!/usr/bin/env node import { basename, join } from 'path'; import { mkdir, readFile, writeFile } from 'fs/promises'; import { debuglog } from 'util'; const internals = { kParseRegex: /[0-9]+-(?[anrv])\s+lemma\s+(?.*)/, kTermMapping: { a: 'adjectives', n: 'nouns', v: 'verbs', r: 'adverbs' }, debug: debuglog('wordnet_to_json'), // Validates that inputs are present validateInputs() { if (process.argv.length < 4) { internals.printUsage(); throw new Error(`Insufficient arguments, expected 2, found ${process.argv.length - 2}`); } }, // Prints the usage of the program printUsage() { console.error('Usage:'); console.error(`${basename(process.argv[1])} `); }, // Loads the contents of a wordnet tab file. async load(pathToTab) { try { return await readFile(pathToTab, { encoding: 'utf8' }); } catch (error) { internals.debug(error.stack); throw new Error(`Could not read tab file at ${pathToTab}`); } }, // Parses a wordner tab file and turns it into a JSON structure async parse(wordnetTab) { try { return wordnetTab.split('\n').reduce((parsedTerms, currentTerm) => { const matches = currentTerm.match(internals.kParseRegex); if (matches) { parsedTerms[internals.kTermMapping[matches.groups.type]].push(matches.groups.term); } return parsedTerms; }, { adjectives: [], nouns: [], verbs: [], adverbs: [] }); } catch (error) { internals.debug(error.stack); throw new Error('Could not parse wordnet data.'); } }, async write(parsedTerms, dataDirectory) { await internals.createDataDirectory(dataDirectory); try { for (const [type, terms] of Object.entries(parsedTerms)) { const targetFile = join(dataDirectory, `${type}.json`); await writeFile(targetFile, JSON.stringify(terms, null, 2)); } } catch (error) { internals.debug(error.stack); throw new Error(`Could not write wordnet data to ${dataDirectory}.`); } }, async createDataDirectory(dataDirectory) { try { await mkdir(dataDirectory, { recursive: true }); } catch (error) { internals.debug(error.stack); throw new Error(`Could not create data directory at ${dataDirectory}`); } }, async run() { internals.validateInputs(); const wordnetTab = await internals.load(process.argv[2]); const parsedTerms = await internals.parse(wordnetTab); await internals.write(parsedTerms, process.argv[3]); } }; internals.run() .then(() => process.exit(0)) .catch((err) => { console.error(err.message || err); process.exit(1); });