forked from aikar/node-markov
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
207 lines (174 loc) · 5.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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;
var db = {};
var self = {};
self.setDb = function (userDb) {
db = userDb;
}
self.getDb = function () {
return db;
}
self.seed = function (seed, cb) {
if (seed instanceof EventEmitter) {
Lazy(seed).lines.forEach(self.seed);
if (cb) {
seed.on('error', cb);
seed.on('end', cb);
}
}
else {
var text = (Buffer.isBuffer(seed) ? seed.toString() : seed)
var words = text.split(/\s+/);
var links = [];
for (var i = 0; i < words.length; i += order) {
var link = words.slice(i, i + order).join(' ');
links.push(link);
}
if (links.length <= 1) {
if (cb) cb(null);
return;
}
for (var i = 1; i < links.length; i++) {
var word = links[i-1];
var cword = clean(word);
var next = links[i];
var cnext = clean(next);
var node = Hash.has(db, cword)
? db[cword]
: {
count : 0,
words : {},
next : {},
prev : {},
}
;
db[cword] = node;
node.count ++;
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] = (
Hash.has(node.prev, prev) ? node.prev[prev] : 0
) + 1;
}
else {
node.prev[''] = (node.prev[''] || 0) + 1;
}
}
if (!Hash.has(db, cnext)) db[cnext] = {
count : 1,
words : {},
next : { '' : 0 },
prev : {},
};
var n = db[cnext];
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);
}
};
self.search = function (text) {
var words = text.split(/\s+/);
// find a starting point...
var start = null;
var groups = {};
for (var i = 0; i < words.length; i += order) {
var word = clean(words.slice(i, i + order).join(' '));
if (Hash.has(db, word)) groups[word] = db[word].count;
}
return deck.pick(groups);
};
self.pick = function () {
return deck.pick(Object.keys(db))
};
self.next = function (cur) {
if (!cur || !db[cur]) return undefined;
var next = deck.pick(db[cur].next);
return next && {
key : next,
word : deck.pick(db[next].words),
} || undefined;
};
self.prev = function (cur) {
if (!cur || !db[cur]) return undefined;
var prev = deck.pick(db[cur].prev);
return prev && {
key : prev,
word : deck.pick(db[prev].words),
} || undefined;
};
self.forward = function (cur, limit) {
var res = [];
while (cur && !limit || res.length < limit) {
var next = self.next(cur);
if (!next) break;
cur = next.key;
res.push(next.word);
}
return res;
};
self.backward = function (cur, limit) {
var res = [];
while (cur && !limit || res.length < limit) {
var prev = self.prev(cur);
if (!prev) break;
cur = prev.key;
res.unshift(prev.word);
}
return res;
};
self.fill = function (cur, limit) {
var res = [ deck.pick(db[cur].words) ];
if (!res[0]) return [];
if (limit && res.length >= limit) return res;;
var pcur = cur;
var ncur = cur;
while (pcur || ncur) {
if (pcur) {
var prev = self.prev(pcur);
pcur = null;
if (prev) {
pcur = prev.key;
res.unshift(prev.word);
if (limit && res.length >= limit) break;
}
}
if (ncur) {
var next = self.next(ncur);
ncur = null;
if (next) {
ncur = next.key;
res.unshift(next.word);
if (limit && res.length >= limit) break;
}
}
}
return res;
};
self.respond = function (text, limit) {
var cur = self.search(text) || self.pick();
return self.fill(cur, limit);
};
self.word = function (cur) {
return db[cur] && deck.pick(db[cur].words);
};
return self;
};
function clean (s) {
return s
.toLowerCase()
.replace(/[^a-z\d]+/g, '_')
.replace(/^_/, '')
.replace(/_$/, '')
;
}