Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Hash.has() everywhere to work around how {} has a "constructor" attri…

…bute and all of that noise
  • Loading branch information...
commit 0b19aacf89608f5d9b02525a772e8318639657c1 1 parent 5db0d90
@substack authored
Showing with 26 additions and 15 deletions.
  1. +23 −13 index.js
  2. +3 −2 package.json
View
36 index.js
@@ -1,6 +1,7 @@
var EventEmitter = require('events').EventEmitter;
var deck = require('deck');
var Lazy = require('lazy');
+var Hash = require('hashish');
module.exports = function (order) {
if (!order) order = 2;
@@ -31,35 +32,44 @@ module.exports = function (order) {
var next = links[i];
var cnext = clean(next);
- var node = db[cword] || {
- count : 0,
- words : {},
- next : {},
- prev : {},
- };
+ var node = Hash.has(db, cword)
+ ? db[cword]
+ : {
+ count : 0,
+ words : {},
+ next : {},
+ prev : {},
+ }
+ ;
db[cword] = node;
node.count ++;
- node.words[word] = (node.words[word] || 0) + 1;
- node.next[cnext] = (node.next[cnext] || 0) + 1;
+ node.words[word] = (
+ Hash.has(node.words, word) ? node.words[word] : 0
+ ) + 1;
+ node.next[cnext] = (
+ Hash.has(node.next, cnext) ? node.next[cnext] : 0
+ ) + 1
if (i > 1) {
var prev = clean(links[i-2]);
- node.prev[prev] = (node.prev[prev] || 0) + 1;
+ node.prev[prev] = (
+ Hash.has(node.prev, prev) ? node.prev[prev] : 0
+ ) + 1;
}
else {
node.prev[''] = (node.prev[''] || 0) + 1;
}
}
- if (!db[cnext]) db[cnext] = {
+ if (!Hash.has(db, cnext)) db[cnext] = {
count : 1,
words : {},
next : { '' : 0 },
prev : {},
};
var n = db[cnext];
- n.words[next] = (n.words[next] || 0) + 1;
- n.prev[cword] = (n.prev[cword] || 0) + 1;
+ n.words[next] = (Hash.has(n.words, next) ? n.words[next] : 0) + 1;
+ n.prev[cword] = (Hash.has(n.prev, cword) ? n.prev[cword] : 0) + 1;
n.next[''] = (n.next[''] || 0) + 1;
if (cb) cb(null);
@@ -74,7 +84,7 @@ module.exports = function (order) {
var groups = {};
for (var i = 0; i < words.length; i += order) {
var word = clean(words.slice(i, i + order).join(' '));
- if (db[word]) groups[word] = db[word].count;
+ if (Hash.has(db, word)) groups[word] = db[word].count;
}
return deck.pick(groups);
View
5 package.json
@@ -1,7 +1,7 @@
{
"name" : "markov",
"description" : "Silly markov chatbot module",
- "version" : "0.0.5",
+ "version" : "0.0.6",
"repository" : {
"type" : "git",
"url" : "git://github.com/substack/node-markov.git"
@@ -19,7 +19,8 @@
},
"dependencies" : {
"deck" : ">=0.0.3",
- "lazy" : ">=1.0.3"
+ "lazy" : ">=1.0.3",
+ "hashish" : ">=0.0.2"
},
"devDependencies" : {
"expresso" : ">=0.7.0"
Please sign in to comment.
Something went wrong with that request. Please try again.