Permalink
Browse files

Improved context a tad

  • Loading branch information...
1 parent 02b8339 commit c2c4940fd845694282effe5c945bdf5cc895ee26 @tstone committed Jun 24, 2012
Showing with 28 additions and 30 deletions.
  1. +20 −27 src/lib.js
  2. +8 −3 src/result.js
View
@@ -1,5 +1,5 @@
-var _ = require('underscore');
+
exports.cleanText = function(source, removeCode, removeHtml) {
@@ -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) {
View
@@ -1,6 +1,7 @@
-var _ = require('underscore');
+var _ = require('underscore'),
+ lib = require('./lib');
var Result = function(base, source, config) { this.__init(base, source, config); };
@@ -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);
@@ -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.