aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorRuben Beltran del Rio <ruben@unlimited.pizza>2021-08-29 21:49:37 +0200
committerRuben Beltran del Rio <ruben@unlimited.pizza>2021-08-29 21:49:37 +0200
commit38431710cfb1cc1ffd7297085d069be1328f083b (patch)
treeb28fba3eb891d8e1f383d4faa358ee7f067be93b /bin
Add project
Diffstat (limited to 'bin')
-rwxr-xr-xbin/wordnet_to_json.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/bin/wordnet_to_json.js b/bin/wordnet_to_json.js
new file mode 100755
index 0000000..0a3689d
--- /dev/null
+++ b/bin/wordnet_to_json.js
@@ -0,0 +1,113 @@
+#!/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]+-(?<type>[anrv])\s+lemma\s+(?<term>.*)/,
+ 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])} <path/to/wordnet_file.tab> <path/to/data/dir>`);
+ },
+
+ // 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);
+ });