Permalink
Browse files

responses now working swimmingly

  • Loading branch information...
1 parent 2818e9c commit edf8e9a3a400971126ab57511ad260492aaaf468 @substack committed May 1, 2011
Showing with 17 additions and 18 deletions.
  1. +2 −2 example/qwantz.js
  2. +15 −16 index.js
View
@@ -2,15 +2,15 @@ var util = require('util');
var fs = require('fs');
var markov = require('markov');
-var m = markov(2);
+var m = markov(1);
var s = fs.createReadStream(__dirname + '/qwantz.txt');
m.seed(s, function () {
var stdin = process.openStdin();
util.print('> ');
stdin.on('data', function (line) {
- var res = m.respond(line.toString());
+ var res = m.respond(line.toString()).join(' ');
console.log(res);
util.print('> ');
});
View
@@ -54,8 +54,10 @@ module.exports = function (order) {
next : {},
prev : {},
};
- db[cnext].words[next] = (db[cnext].words[next] || 0) + 1;
- db[cnext].prev[cword] = (db[cnext].prev[cword] || 0) + 1;
+ var n = db[cnext];
+ n.count = (n.count || 0) + 1;
+ n.words[next] = (n.words[next] || 0) + 1;
+ n.prev[cword] = (n.prev[cword] || 0) + 1;
if (cb) cb(null);
}
@@ -73,7 +75,6 @@ module.exports = function (order) {
}
return deck.pick(groups);
- ;
};
self.pick = function () {
@@ -98,34 +99,32 @@ module.exports = function (order) {
self.forward = function (cur, limit) {
var res = [];
- do {
+ while (!limit || res.length < limit) {
var next = self.next(cur);
- if (next) {
- cur = next.key;
- res.push(next.word);
- }
- } while (next && limit === 0 || res.length < limit)
+ if (!next) break;
+ cur = next.key;
+ res.push(next.word);
+ }
return res;
};
self.backward = function (cur, limit) {
var res = [];
- do {
+ while (!limit || res.length < limit) {
var prev = self.prev(cur);
- if (prev) {
- cur = prev.key;
- res.unshift(prev.word);
- }
- } while (prev && limit === 0 || res.length < limit)
+ if (!prev) break;
+ cur = prev.key;
+ res.unshift(prev.word);
+ }
return res;
};
self.fill = function (cur, limit) {
var f = self.forward(cur, limit);
var b = self.backward(cur, limit - f.length);
- return b.concat(f);
+ return b.concat(deck.pick(db[cur].words), f);
};
self.respond = function (text, limit) {

0 comments on commit edf8e9a

Please sign in to comment.