From d8d380ed12675d2367facaf59011a009d2998975 Mon Sep 17 00:00:00 2001 From: Dariusz Dziuk Date: Tue, 5 Nov 2013 22:51:03 +0100 Subject: [PATCH] Better example. --- examples/lookup.js | 19 +++++++++++++++++-- lib/wordnet.js | 29 +++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/examples/lookup.js b/examples/lookup.js index 7670bc5..60303de 100755 --- a/examples/lookup.js +++ b/examples/lookup.js @@ -20,13 +20,28 @@ if (!word) { program.help(); } -wordnet.lookup(word, function(err, definition) { +wordnet.lookup(word, function(err, definitions) { if (err) { console.log('An error occured: %s', err); return; } - console.log('[%s]\n\n', word, definition); + console.log('\n %s\n', word); + + /* Definitions */ + definitions.forEach(function(definition) { + + console.log(' type : %s', definition.meta.synsetType); + var words = ''; + definition.meta.words.forEach(function(word) { + words += word.word + ' '; + }); + console.log(' words: %s', words.trim()); + console.log(' %s', definition.glossary); + + console.log(); + + }); }); \ No newline at end of file diff --git a/lib/wordnet.js b/lib/wordnet.js index 43561d8..932bd30 100644 --- a/lib/wordnet.js +++ b/lib/wordnet.js @@ -42,6 +42,20 @@ var _extensions = [ 'verb' ]; +/** + * Synset types map + * + * @type {Object} + */ + +var _synsetTypeMap = { + 'n': 'noun', + 'v': 'verb', + 's': 'adjective', + 's': 'adjective satellite', + 'r': 'adverb' +}; + /** * Index lookup * @@ -129,7 +143,7 @@ function parseDataLine(line, callback) { var meta = {}; meta.synsetOffset = parseInt(metadata.shift(), 10); meta.lexFilenum = parseInt(metadata.shift(), 10); - meta.synsetType = metadata.shift(); + meta.synsetType = _synsetTypeMap[metadata.shift()]; meta.wordCount = parseInt(metadata.shift(), 16); meta.words = []; @@ -141,9 +155,20 @@ function parseDataLine(line, callback) { }); } + /* Parse the pointers */ + meta.pointerCount = parseInt(metadata.shift(), 10); + meta.pointers = []; + for (var pointerIdx = 0; pointerIdx < meta.pointerCount; pointerIdx++) { + meta.pointers.push({ + pointerSymbol: metadata.shift(), + synsetOffset: parseInt(metadata.shift(), 10), + pos: metadata.shift(), + sourceTargetHex: metadata.shift() + }); + } + callback(null, { meta: meta, - rest: metadata, glossary: glossary });