-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnGram.js
151 lines (123 loc) · 3.31 KB
/
nGram.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
/**
* Created by syzer on 4/7/2015.
*/
// run on two threads
var jsSpark = require('js-spark')({workers: 2});
var task = jsSpark.jsSpark;
var q = jsSpark.q;
var _ = require('lodash');
var lib = require('./lib')(_);
_.mixin(lib);
var fs = require('fs');
var DRACULA = './data/text/dracula/';
// cache
var dataBase;
// simples would be return dataBase[word];
// string => array
function predict(word) {
var needle = dataBase[word];
if (!needle) {
return [];
}
var total = _.reduce(needle, function (acc, el) {
return acc + el;
});
needle = _.objToSortedArr(needle);
return _(needle)
.map(function (el) {
el[1] = (el[1] / total).toFixed(3);
return el;
})
.map(function (el) {
return el.join(',');
})
.value();
}
function train(fileName, splitter) {
var parts = fs.readFileSync(fileName).toString().split(splitter);
return mergeBig(parts);
}
function mergeBig(texts) {
return q.all(texts.map(function (el) {
return task(el)
.thru(bigramText)
.run()
})).then(function reducer(data) {
return _.mergeObjectsInArr(data); // uncomment if u want to reduce on this worker
//return task(data)
// .thru(merger)
// .run();
}).then(function cacheInDb(data) {
dataBase = data;
return data;
});
}
function merger(arr) {
var _ = this;
function mergeObjectsInArr(arr) {
return arr.reduce(function (acc, curr) {
return _.merge(acc, curr, objectMerger);
});
}
function objectMerger(a, b) {
if (a && b && _.isNumber(a) && _.isNumber(b)) {
return a + b;
}
if (_.isArray(a)) {
return a.concat(b);
}
}
return mergeObjectsInArr(arr);
}
function bigramText(str) {
var arr = str
.replace(/\n/gi, ' ')
.replace(/\r/gi, ' ')
.replace(/[^\w\s]+/gi, '')
.toLowerCase()
.trim()
.split(' ');
var out = {};
arr.reduce(function bigramReduce(last, word, i, arr) {
out[last] = out[last] || {};
out[last][word] = out[last][word] + 1 || 1;
return word;
});
return out;
}
function displayPrediction(result, i, word) {
console.log('\n\nPredictions for word: ' + (words[i] || word));
console.log(result);
}
module.exports = function () {
return {
bigramText: bigramText,
train: train,
predict: predict
}
};
function promptUserPredictions(predictions) {
var prompt = require('prompt');
prompt.message = 'Guess what user typed after that';
prompt.start();
prompt.get(['word'], function (err, res) {
displayPrediction(predict(res.word).slice(0, 10), null, res.word);
return predictions;
});
}
var words = ['i', 'van', 'helsing'];
console.time('train');
module
.exports()
.train(DRACULA + 'full.txt', '\r\n\r\n\r\nCHAPTER')
.then(function checkPredictionsForHardcodedWords() {
console.timeEnd('train');
return words.map(function (word) {
return predict(word).slice(0, 10);
});
})
.then(function displayPredictions(predictions) {
predictions.map(displayPrediction);
return predictions;
})
.then(promptUserPredictions);