Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
Improved context a tad
Browse files Browse the repository at this point in the history
  • Loading branch information
tstone committed Jun 24, 2012
1 parent 02b8339 commit c2c4940
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
47 changes: 20 additions & 27 deletions src/lib.js
@@ -1,5 +1,5 @@

var _ = require('underscore');



exports.cleanText = function(source, removeCode, removeHtml) {
Expand All @@ -16,34 +16,27 @@ exports.cleanText = function(source, removeCode, removeHtml) {

};

exports.formatResult = function(resp, source) {

var sugObj = function(o) {
return {
offset: o.attrs.o,
confidence: o.attrs.s,
word: source.substr(o.attrs.o - 1, o.attrs.l),
words: o.chars.split('\t')
};
};

// Carry over root attributes
var root = _.extend({ suggestions: [] }, resp.attrs);

// Format suggestions
if (resp.c) {
if (Array.isArray(resp.c)) {
root.suggestions = resp.c.map(function(x){
return sugObj(x);
});
} else {
root.suggestions = [
sugObj(resp.c)
];
}
exports.getContext = function(source, word, start, leftCount, rightCount) {

var loffset = start - leftCount - 1;
var left = loffset > -1 ? source.substr(loffset, leftCount) : source.substr(0, start);
var right = source.substr(start - 1 + word.length, rightCount);

// Check for new lines
if (left.indexOf('\n') > -1) {
left = left.substr(left.lastIndexOf('\n'));
} else {
// Otherwise find the first space and chop from there
left = left.substr(left.indexOf(' '));
}

if (right.indexOf('\n') > -1) {
right = right.substr(0, right.indexOf('\n'));
} else {
right = right.substr(0, right.lastIndexOf(' '));
}

return root;
return (left + '[' + word + ']' + right).trim();
};

exports.reqXml = function(s, config) {
Expand Down
11 changes: 8 additions & 3 deletions src/result.js
@@ -1,6 +1,7 @@


var _ = require('underscore');
var _ = require('underscore'),
lib = require('./lib');


var Result = function(base, source, config) { this.__init(base, source, config); };
Expand All @@ -21,15 +22,18 @@ var Result = function(base, source, config) { this.__init(base, source, config);

cls.__parseSuggestions = function(xs){
var suggestions = [];
var source = this.source;
var threshold = this.config.threshold;

var sugObj = function(o) {
return {
var sug = {
offset: o.attrs.o,
confidence: o.attrs.s,
word: this.source.substr(o.attrs.o - 1, o.attrs.l),
words: o.chars.split('\t')
words: o.chars.split('\t'),
};
sug.context = lib.getContext(source, sug.word, sug.offset, 50, 30);
return sug;
}.bind(this);
var addSug = function(x) {
var sug = sugObj(x);
Expand All @@ -42,6 +46,7 @@ var Result = function(base, source, config) { this.__init(base, source, config);
this.suggestions = suggestions;
};


}(Result.prototype));

module.exports = Result;

0 comments on commit c2c4940

Please sign in to comment.