node-wordnet
Advanced tools
Comparing version 0.1.9 to 0.1.10
@@ -5,2 +5,10 @@ Release history | ||
Version 0.1.10 | ||
------------- | ||
* Total rewrite of the object inheritance for files | ||
* Added a working .close() method - #13 | ||
* Partial work on improving error handling - #8 | ||
Version 0.1.9 | ||
@@ -7,0 +15,0 @@ ------------- |
@@ -1,2 +0,4 @@ | ||
var DataFile, WordNetFile, fs, get, util; | ||
var DataFile, WordNetFile, fs, util, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
@@ -9,57 +11,62 @@ WordNetFile = require('./wordnet_file'); | ||
get = function(location, callback) { | ||
var buff; | ||
buff = new Buffer(4096); | ||
return this.open(function(err, fd) { | ||
return WordNetFile.appendLineChar(fd, location, 0, buff, function(line) { | ||
var data, definition, element, examples, glossArray, i, k, ptrOffset, ptrs, synonyms, tokens, wCnt, _i, _j, _k, _len, _ref, _ref1; | ||
data = line.split('| '); | ||
tokens = data[0].split(/\s+/); | ||
ptrs = []; | ||
wCnt = parseInt(tokens[3], 16); | ||
synonyms = []; | ||
for (i = _i = 0, _ref = wCnt - 1; _i <= _ref; i = _i += 1) { | ||
synonyms.push(tokens[4 + i * 2]); | ||
module.exports = DataFile = (function(_super) { | ||
__extends(DataFile, _super); | ||
function DataFile(dataDir, name) { | ||
DataFile.__super__.constructor.call(this, dataDir, 'data.' + name); | ||
} | ||
DataFile.prototype.get = function(location, callback) { | ||
var buff, self; | ||
self = this; | ||
buff = new Buffer(4096); | ||
return this.open(function(err, fd) { | ||
if (err != null) { | ||
return callback.call(self, err, null); | ||
} | ||
ptrOffset = (wCnt - 1) * 2 + 6; | ||
for (i = _j = 0, _ref1 = parseInt(tokens[ptrOffset], 10) - 1; _j <= _ref1; i = _j += 1) { | ||
ptrs.push({ | ||
pointerSymbol: tokens[ptrOffset + 1 + i * 4], | ||
synsetOffset: parseInt(tokens[ptrOffset + 2 + i * 4], 10), | ||
pos: tokens[ptrOffset + 3 + i * 4], | ||
sourceTarget: tokens[ptrOffset + 4 + i * 4] | ||
return this.appendLineChar(fd, location, 0, buff, function(err, line) { | ||
var data, definition, element, examples, glossArray, i, k, ptrOffset, ptrs, synonyms, tokens, wCnt, _i, _j, _k, _len, _ref, _ref1; | ||
data = line.split('| '); | ||
tokens = data[0].split(/\s+/); | ||
ptrs = []; | ||
wCnt = parseInt(tokens[3], 16); | ||
synonyms = []; | ||
for (i = _i = 0, _ref = wCnt - 1; _i <= _ref; i = _i += 1) { | ||
synonyms.push(tokens[4 + i * 2]); | ||
} | ||
ptrOffset = (wCnt - 1) * 2 + 6; | ||
for (i = _j = 0, _ref1 = parseInt(tokens[ptrOffset], 10) - 1; _j <= _ref1; i = _j += 1) { | ||
ptrs.push({ | ||
pointerSymbol: tokens[ptrOffset + 1 + i * 4], | ||
synsetOffset: parseInt(tokens[ptrOffset + 2 + i * 4], 10), | ||
pos: tokens[ptrOffset + 3 + i * 4], | ||
sourceTarget: tokens[ptrOffset + 4 + i * 4] | ||
}); | ||
} | ||
glossArray = data[1].split("; "); | ||
definition = glossArray[0]; | ||
examples = glossArray.slice(1); | ||
for (k = _k = 0, _len = examples.length; _k < _len; k = ++_k) { | ||
element = examples[k]; | ||
examples[k] = examples[k].replace(/\"/g, '').replace(/\s\s+/g, ''); | ||
} | ||
return callback.call(self, null, { | ||
synsetOffset: parseInt(tokens[0], 10), | ||
lexFilenum: parseInt(tokens[1], 10), | ||
pos: tokens[2], | ||
wCnt: wCnt, | ||
lemma: tokens[4], | ||
synonyms: synonyms, | ||
lexId: tokens[5], | ||
ptrs: ptrs, | ||
gloss: data[1], | ||
def: definition, | ||
exp: examples | ||
}); | ||
} | ||
glossArray = data[1].split("; "); | ||
definition = glossArray[0]; | ||
examples = glossArray.slice(1); | ||
for (k = _k = 0, _len = examples.length; _k < _len; k = ++_k) { | ||
element = examples[k]; | ||
examples[k] = examples[k].replace(/\"/g, '').replace(/\s\s+/g, ''); | ||
} | ||
return callback({ | ||
synsetOffset: parseInt(tokens[0], 10), | ||
lexFilenum: parseInt(tokens[1], 10), | ||
pos: tokens[2], | ||
wCnt: wCnt, | ||
lemma: tokens[4], | ||
synonyms: synonyms, | ||
lexId: tokens[5], | ||
ptrs: ptrs, | ||
gloss: data[1], | ||
def: definition, | ||
exp: examples | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; | ||
DataFile = function(dataDir, name) { | ||
return WordNetFile.call(this, dataDir, 'data.' + name); | ||
}; | ||
return DataFile; | ||
util.inherits(DataFile, WordNetFile); | ||
DataFile.prototype.get = get; | ||
module.exports = DataFile; | ||
})(WordNetFile); |
@@ -1,2 +0,4 @@ | ||
var IndexFile, WordNetFile, find, findAt, findPrevEOL, fs, getFileSize, lookup, lookupFromFile, miss, readLine, util; | ||
var IndexFile, WordNetFile, fs, util, | ||
__hasProp = {}.hasOwnProperty, | ||
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; | ||
@@ -9,128 +11,125 @@ WordNetFile = require('./wordnet_file'); | ||
getFileSize = function(path) { | ||
var stat; | ||
stat = fs.statSync(path); | ||
return stat.size; | ||
}; | ||
module.exports = IndexFile = (function(_super) { | ||
var _findAt, _findPrevEOL, _getFileSize, _readLine; | ||
findPrevEOL = function(fd, pos, callback) { | ||
var buff; | ||
buff = new Buffer(1024); | ||
if (pos === 0) { | ||
return callback(0); | ||
} else { | ||
return fs.read(fd, buff, 0, 1, pos, function(err, count) { | ||
if (buff[0] === 10) { | ||
return callback(pos + 1); | ||
} else { | ||
return findPrevEOL(fd, pos - 1, callback); | ||
} | ||
}); | ||
__extends(IndexFile, _super); | ||
function IndexFile(dataDir, name) { | ||
IndexFile.__super__.constructor.call(this, dataDir, 'index.' + name); | ||
} | ||
}; | ||
readLine = function(fd, pos, callback) { | ||
var buff; | ||
buff = new Buffer(1024); | ||
return findPrevEOL(fd, pos, function(pos) { | ||
return WordNetFile.appendLineChar(fd, pos, 0, buff, callback); | ||
}); | ||
}; | ||
miss = function(callback) { | ||
return callback({ | ||
status: 'miss' | ||
}); | ||
}; | ||
findAt = function(fd, size, pos, lastPos, adjustment, searchKey, callback, lastKey) { | ||
if (lastPos === pos || pos >= size) { | ||
return miss(callback); | ||
} else { | ||
return readLine(fd, pos, function(line) { | ||
var key, tokens; | ||
tokens = line.split(/\s+/); | ||
key = tokens[0]; | ||
if (key === searchKey) { | ||
return callback({ | ||
status: 'hit', | ||
key: key, | ||
'line': line, | ||
tokens: tokens | ||
}); | ||
} else if (adjustment === 1 || key === lastKey) { | ||
return miss(callback); | ||
} else { | ||
adjustment = Math.ceil(adjustment * 0.5); | ||
if (key < searchKey) { | ||
return findAt(fd, size, pos + adjustment, pos, adjustment, searchKey, callback, key); | ||
_findPrevEOL = function(self, fd, pos, callback) { | ||
var buff; | ||
buff = new Buffer(1024); | ||
if (pos === 0) { | ||
return callback(null, 0); | ||
} else { | ||
return fs.read(fd, buff, 0, 1, pos, function(err, count) { | ||
if (err != null) { | ||
return callback(err, null); | ||
} | ||
if (buff[0] === 10) { | ||
return callback(null, pos + 1); | ||
} else { | ||
return findAt(fd, size, pos - adjustment, pos, adjustment, searchKey, callback, key); | ||
return _findPrevEOL(self, fd, pos - 1, callback); | ||
} | ||
} | ||
}); | ||
} | ||
}; | ||
_readLine = function(self, fd, pos, callback) { | ||
var buff; | ||
buff = new Buffer(1024); | ||
return _findPrevEOL(self, fd, pos, function(err, pos) { | ||
return self.appendLineChar(fd, pos, 0, buff, callback); | ||
}); | ||
} | ||
}; | ||
}; | ||
find = function(searchKey, callback) { | ||
var indexFile; | ||
indexFile = this; | ||
return indexFile.open(function(err, fd) { | ||
var pos, size; | ||
if (err) { | ||
return console.log(err); | ||
_findAt = function(self, fd, size, pos, lastPos, adjustment, searchKey, callback, lastKey) { | ||
if (lastPos === pos || pos >= size) { | ||
return callback({ | ||
status: 'miss' | ||
}); | ||
} else { | ||
size = getFileSize(indexFile.filePath) - 1; | ||
pos = Math.ceil(size / 2); | ||
return findAt(fd, size, pos, null, pos, searchKey, function(result) { | ||
return callback(result); | ||
return _readLine(self, fd, pos, function(err, line) { | ||
var key, tokens; | ||
tokens = line.split(/\s+/); | ||
key = tokens[0]; | ||
if (key === searchKey) { | ||
return callback({ | ||
status: 'hit', | ||
key: key, | ||
'line': line, | ||
tokens: tokens | ||
}); | ||
} else if (adjustment === 1 || key === lastKey) { | ||
return callback({ | ||
status: 'miss' | ||
}); | ||
} else { | ||
adjustment = Math.ceil(adjustment * 0.5); | ||
if (key < searchKey) { | ||
return _findAt(self, fd, size, pos + adjustment, pos, adjustment, searchKey, callback, key); | ||
} else { | ||
return _findAt(self, fd, size, pos - adjustment, pos, adjustment, searchKey, callback, key); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
}; | ||
lookupFromFile = function(word, callback) { | ||
return this.find(word, function(record) { | ||
var i, indexRecord, offsets, ptrs, _i, _j, _ref, _ref1; | ||
indexRecord = null; | ||
if (record.status === 'hit') { | ||
ptrs = []; | ||
offsets = []; | ||
for (i = _i = 0, _ref = parseInt(record.tokens[3]) - 1; _i <= _ref; i = _i += 1) { | ||
ptrs.push(record.tokens[i]); | ||
_getFileSize = function(path) { | ||
var stat; | ||
stat = fs.statSync(path); | ||
return stat.size; | ||
}; | ||
IndexFile.prototype.find = function(searchKey, callback) { | ||
var self; | ||
self = this; | ||
return this.open(function(err, fd) { | ||
var pos, size; | ||
if (err != null) { | ||
return callback(err, null); | ||
} | ||
for (i = _j = 0, _ref1 = parseInt(record.tokens[2]) - 1; _j <= _ref1; i = _j += 1) { | ||
offsets.push(parseInt(record.tokens[ptrs.length + 6 + i], 10)); | ||
size = _getFileSize(this.filePath) - 1; | ||
pos = Math.ceil(size / 2); | ||
return _findAt(self, fd, size, pos, null, pos, searchKey, function(result) { | ||
return callback.call(self, null, result); | ||
}); | ||
}); | ||
}; | ||
IndexFile.prototype.lookupFromFile = function(word, callback) { | ||
return this.find(word, function(err, record) { | ||
var i, indexRecord, offsets, ptrs, _i, _j, _ref, _ref1; | ||
indexRecord = null; | ||
if (record.status === 'hit') { | ||
ptrs = []; | ||
offsets = []; | ||
for (i = _i = 0, _ref = parseInt(record.tokens[3]) - 1; _i <= _ref; i = _i += 1) { | ||
ptrs.push(record.tokens[i]); | ||
} | ||
for (i = _j = 0, _ref1 = parseInt(record.tokens[2]) - 1; _j <= _ref1; i = _j += 1) { | ||
offsets.push(parseInt(record.tokens[ptrs.length + 6 + i], 10)); | ||
} | ||
indexRecord = { | ||
lemma: record.tokens[0], | ||
pos: record.tokens[1], | ||
ptrSymbol: ptrs, | ||
senseCnt: parseInt(record.tokens[ptrs.length + 4], 10), | ||
tagsenseCnt: parseInt(record.tokens[ptrs.length + 5], 10), | ||
synsetOffset: offsets | ||
}; | ||
} | ||
indexRecord = { | ||
lemma: record.tokens[0], | ||
pos: record.tokens[1], | ||
ptrSymbol: ptrs, | ||
senseCnt: parseInt(record.tokens[ptrs.length + 4], 10), | ||
tagsenseCnt: parseInt(record.tokens[ptrs.length + 5], 10), | ||
synsetOffset: offsets | ||
}; | ||
} | ||
return callback(indexRecord); | ||
}); | ||
}; | ||
return callback.call(this, null, indexRecord); | ||
}); | ||
}; | ||
lookup = function(word, callback) { | ||
return this.lookupFromFile(word, callback); | ||
}; | ||
IndexFile.prototype.lookup = function(word, callback) { | ||
return this.lookupFromFile(word, callback); | ||
}; | ||
IndexFile = function(dataDir, name) { | ||
return WordNetFile.call(this, dataDir, 'index.' + name); | ||
}; | ||
return IndexFile; | ||
util.inherits(IndexFile, WordNetFile); | ||
IndexFile.prototype.lookupFromFile = lookupFromFile; | ||
IndexFile.prototype.lookup = lookup; | ||
IndexFile.prototype.find = find; | ||
IndexFile.prototype._findAt = findAt; | ||
module.exports = IndexFile; | ||
})(WordNetFile); |
@@ -1,2 +0,2 @@ | ||
var WordNetFile, appendLineChar, close, fs, open, path, util; | ||
var WordNetFile, fs, path, util; | ||
@@ -9,58 +9,60 @@ fs = require('fs'); | ||
appendLineChar = function(fd, pos, buffPos, buff, callback) { | ||
var length, space; | ||
length = buff.length; | ||
space = length - buffPos; | ||
return fs.read(fd, buff, buffPos, space, pos, function(err, count, buffer) { | ||
var i, newBuff, _i, _ref; | ||
if (err) { | ||
return console.log(err); | ||
} else { | ||
for (i = _i = 0, _ref = count - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { | ||
if (buff[i] === 10) { | ||
return callback(buff.slice(0, i).toString('ASCII')); | ||
module.exports = WordNetFile = (function() { | ||
function WordNetFile(dataDir, fileName) { | ||
this.dataDir = dataDir; | ||
this.fileName = fileName; | ||
this.filePath = path.join(this.dataDir, this.fileName); | ||
} | ||
WordNetFile.prototype.open = function(callback) { | ||
var filePath, self; | ||
self = this; | ||
if (this.fd) { | ||
return callback.call(self, null, this.fd); | ||
} | ||
filePath = this.filePath; | ||
return fs.open(filePath, 'r', null, (function(_this) { | ||
return function(err, fd) { | ||
if (err) { | ||
console.log('Unable to open %s', filePath, err); | ||
return callback.call(self, err, null); | ||
} | ||
} | ||
newBuff = new Buffer(length * 2); | ||
buff.copy(newBuff, 0, 0, length); | ||
return appendLineChar(fd, pos + length, length, newBuff, callback); | ||
_this.fd = fd; | ||
return callback.call(self, err, fd); | ||
}; | ||
})(this)); | ||
}; | ||
WordNetFile.prototype.close = function() { | ||
if (this.fd != null) { | ||
fs.close(this.fd); | ||
return delete this.fd; | ||
} | ||
}); | ||
}; | ||
}; | ||
close = function() { | ||
fs.close(this.fd); | ||
return delete this.fd; | ||
}; | ||
open = function(callback) { | ||
var filePath; | ||
if (this.fd) { | ||
return callback(null, this.fd); | ||
} | ||
filePath = this.filePath; | ||
return fs.open(filePath, 'r', null, (function(_this) { | ||
return function(err, fd) { | ||
WordNetFile.prototype.appendLineChar = function(fd, pos, buffPos, buff, callback) { | ||
var length, self, space; | ||
self = this; | ||
length = buff.length; | ||
space = length - buffPos; | ||
return fs.read(fd, buff, buffPos, space, pos, function(err, count, buffer) { | ||
var i, newBuff, _i, _ref; | ||
if (err) { | ||
console.log('Unable to open %s', filePath); | ||
return; | ||
console.log(self, fd, err); | ||
return callback.call(self, err, null); | ||
} else { | ||
for (i = _i = 0, _ref = count - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; i = 0 <= _ref ? ++_i : --_i) { | ||
if (buff[i] === 10) { | ||
return callback.call(self, null, buff.slice(0, i).toString('ASCII')); | ||
} | ||
} | ||
newBuff = new Buffer(length * 2); | ||
buff.copy(newBuff, 0, 0, length); | ||
return self.appendLineChar(fd, pos + length, length, newBuff, callback); | ||
} | ||
_this.fd = fd; | ||
return callback(err, fd, function() { | ||
return void 0; | ||
}); | ||
}; | ||
})(this)); | ||
}; | ||
}); | ||
}; | ||
WordNetFile = function(dataDir, fileName) { | ||
this.dataDir = dataDir; | ||
this.fileName = fileName; | ||
return this.filePath = require('path').join(this.dataDir, this.fileName); | ||
}; | ||
return WordNetFile; | ||
WordNetFile.prototype.open = open; | ||
WordNetFile.appendLineChar = appendLineChar; | ||
module.exports = WordNetFile; | ||
})(); |
@@ -98,3 +98,3 @@ var DataFile, IndexFile, LRU, Promise, WordNet, async, fs, path, | ||
dataFile = wordnet.getDataFile(pos); | ||
return dataFile.get(synsetOffset, function(result) { | ||
return dataFile.get(synsetOffset, function(err, result) { | ||
if (query) { | ||
@@ -242,3 +242,3 @@ wordnet.cache.set(query, result); | ||
file = files.pop(); | ||
return file.index.lookup(word, function(record) { | ||
return file.index.lookup(word, function(err, record) { | ||
if (record) { | ||
@@ -261,3 +261,3 @@ return wordnet.pushResults(file.data, results, record.synsetOffset, function() { | ||
} else { | ||
return data.get(offsets.pop(), function(record) { | ||
return data.get(offsets.pop(), function(err, record) { | ||
results.push(record); | ||
@@ -264,0 +264,0 @@ return wordnet.pushResults(data, results, offsets, callback); |
{ | ||
"name": "node-wordnet", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"description": "Node.js interface for Wordnet", | ||
@@ -29,2 +29,3 @@ "main": "lib/wordnet.js", | ||
"gulp-mocha": "^2.0.0", | ||
"mocha": "^2.2.5", | ||
"wndb-with-exceptions": "^3.0.1" | ||
@@ -31,0 +32,0 @@ }, |
@@ -141,2 +141,10 @@ Wordnet | ||
### close() | ||
Closes all the file handles being used by this instance. If new queries are | ||
done, the files may be silently re-opened, but that probably isn't a very good | ||
plan. It should be assumed that re-use of an instance after close is | ||
deprecated. | ||
Future work | ||
@@ -143,0 +151,0 @@ ----------- |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90280
20
807
153
8