Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Totally reorganized the file access classes
This allows .close() to work. Resolves #13.
- Loading branch information
Showing
11 changed files
with
429 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,71 @@ | ||
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; }; | ||
|
||
WordNetFile = require('./wordnet_file'); | ||
|
||
fs = require('fs'); | ||
|
||
util = require('util'); | ||
|
||
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); | ||
}; | ||
|
||
util.inherits(DataFile, WordNetFile); | ||
}; | ||
|
||
DataFile.prototype.get = get; | ||
return DataFile; | ||
|
||
module.exports = DataFile; | ||
})(WordNetFile); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,134 @@ | ||
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; }; | ||
|
||
WordNetFile = require('./wordnet_file'); | ||
|
||
fs = require('fs'); | ||
|
||
util = require('util'); | ||
|
||
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); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
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); | ||
}); | ||
}; | ||
__extends(IndexFile, _super); | ||
|
||
miss = function(callback) { | ||
return callback({ | ||
status: 'miss' | ||
}); | ||
}; | ||
function IndexFile(dataDir, name) { | ||
IndexFile.__super__.constructor.call(this, dataDir, 'index.' + name); | ||
} | ||
|
||
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); | ||
}); | ||
}; | ||
|
||
lookup = function(word, callback) { | ||
return this.lookupFromFile(word, callback); | ||
}; | ||
|
||
IndexFile = function(dataDir, name) { | ||
return WordNetFile.call(this, dataDir, 'index.' + name); | ||
}; | ||
|
||
util.inherits(IndexFile, WordNetFile); | ||
|
||
IndexFile.prototype.lookupFromFile = lookupFromFile; | ||
|
||
IndexFile.prototype.lookup = lookup; | ||
return callback.call(this, null, indexRecord); | ||
}); | ||
}; | ||
|
||
IndexFile.prototype.find = find; | ||
IndexFile.prototype.lookup = function(word, callback) { | ||
return this.lookupFromFile(word, callback); | ||
}; | ||
|
||
IndexFile.prototype._findAt = findAt; | ||
return IndexFile; | ||
|
||
module.exports = IndexFile; | ||
})(WordNetFile); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.