diff --git a/examples/lookup.js b/examples/lookup.js index 1289fe4..b97fb4e 100755 --- a/examples/lookup.js +++ b/examples/lookup.js @@ -4,6 +4,7 @@ const wordnet = require('../lib/wordnet.js'); program .version('0.0.1') .usage('') + .option('-d, --database ', 'Location of WordNet index and data files.') .parse(process.argv); /* Word to look up */ diff --git a/lib/wordnet.js b/lib/wordnet.js index a2f05e9..66ce5a5 100644 --- a/lib/wordnet.js +++ b/lib/wordnet.js @@ -34,18 +34,26 @@ function getKey(word) { return `${KEY_PREFIX}${word}` } * * @return {Promise} Empty promise object. */ -module.exports.init = async function() { +module.exports.init = async function(databaseDir) { const extensions = Object.keys(EXTENSIONS_MAP); + if (!databaseDir) { + databaseDir = __dirname + '/../db'; + } + // Read data from all index files into memory. // NOTE: We have to run this serially in order to ensure consistent results. await extensions.reduce((p, ext) => { - return p.then(() => readIndex(ext)); + return p.then(() => { + return readIndex(`${databaseDir}/index.${ext}`); + }); }, Promise.resolve()); // Load the contents from the data files into memory. await Promise.all(extensions.map((ext) => { - return readDatabase(ext).then((fileHandle) => _data[EXTENSIONS_MAP[ext]] = fileHandle); + return readDatabase(`${databaseDir}/data.${ext}`).then((fileHandle) => { + _data[EXTENSIONS_MAP[ext]] = fileHandle; + }); })); return Promise.resolve(); @@ -107,9 +115,9 @@ function readData(definition, skipPointers) { }); } -function readIndex(extension) { +function readIndex(filePath) { return new Promise((resolve) => { - reader.read(__dirname + '/../db/index.' + extension, (line) => { + reader.read(filePath, (line) => { let result = parseIndexLine(line); if (!result.isComment) { @@ -130,8 +138,8 @@ function readIndex(extension) { * @param {String} extension WordNet DB extension. * @return {Promise} Resolves with FileHandle object. */ -function readDatabase(extension) { - return fsp.open(__dirname + '/../db/data.' + extension, 'r'); +function readDatabase(filePath) { + return fsp.open(filePath, 'r'); } /** diff --git a/test/wordnet.js b/test/wordnet.js index 87810f8..86b4ad8 100644 --- a/test/wordnet.js +++ b/test/wordnet.js @@ -11,6 +11,7 @@ test('api', async (t) => { // - Calling API functions before init. // - Searching for a word not in the database. + // TODO: Add test for initializing with a custom path. await wordnet.init(); let list = await wordnet.list();