aboutsummaryrefslogtreecommitdiff
path: root/bin/wordnet_to_json.js
blob: 0a3689dca046506b22252cb1692c8ed40b365aa5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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);
  });