]>
git.r.bdr.sh - rbdr/prompt/blob - bin/wordnet_to_json.js
3 import { basename
, join
} from 'path';
4 import { mkdir
, readFile
, writeFile
} from 'fs/promises';
5 import { debuglog
} from 'util';
8 kParseRegex: /[0-9]+-(?<type>[anrv])\s+lemma\s+(?<term>.*)/,
16 debug: debuglog('wordnet_to_json'),
18 // Validates that inputs are present
21 if (process
.argv
.length
< 4) {
22 internals
.printUsage();
23 throw new Error(`Insufficient arguments, expected 2, found ${process.argv.length - 2}`);
27 // Prints the usage of the program
31 console
.error('Usage:');
32 console
.error(`${basename(process.argv[1])} <path/to/wordnet_file.tab> <path/to/data/dir>`);
35 // Loads the contents of a wordnet tab file.
37 async
load(pathToTab
) {
40 return await
readFile(pathToTab
, { encoding: 'utf8' });
43 internals
.debug(error
.stack
);
44 throw new Error(`Could not read tab file at ${pathToTab}`);
48 // Parses a wordner tab file and turns it into a JSON structure
50 async
parse(wordnetTab
) {
53 return wordnetTab
.split('\n').reduce((parsedTerms
, currentTerm
) => {
54 const matches
= currentTerm
.match(internals
.kParseRegex
);
56 parsedTerms
[internals
.kTermMapping
[matches
.groups
.type
]].push(matches
.groups
.term
);
67 internals
.debug(error
.stack
);
68 throw new Error('Could not parse wordnet data.');
72 async
write(parsedTerms
, dataDirectory
) {
74 await internals
.createDataDirectory(dataDirectory
);
76 for (const [type
, terms
] of Object
.entries(parsedTerms
)) {
77 const targetFile
= join(dataDirectory
, `${type}.json`);
78 await
writeFile(targetFile
, JSON
.stringify(terms
, null, 2));
82 internals
.debug(error
.stack
);
83 throw new Error(`Could not write wordnet data to ${dataDirectory}.`);
87 async
createDataDirectory(dataDirectory
) {
90 await
mkdir(dataDirectory
, { recursive: true });
93 internals
.debug(error
.stack
);
94 throw new Error(`Could not create data directory at ${dataDirectory}`);
100 internals
.validateInputs();
101 const wordnetTab
= await internals
.load(process
.argv
[2]);
102 const parsedTerms
= await internals
.parse(wordnetTab
);
103 await internals
.write(parsedTerms
, process
.argv
[3]);
108 .then(() => process
.exit(0))
111 console
.error(err
.message
|| err
);