-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathwordpos-bench.js
130 lines (106 loc) · 2.49 KB
/
wordpos-bench.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
/**
* wordpos-bench.js
*
* Copyright (c) 2012-2016 [email protected]
* https://github.com/moos/wordpos
*
* Released under MIT license
*/
var Bench = require('mini-bench'),
fs = require('fs'),
_ = require('underscore')._,
WordPOS = require('../src/wordpos'),
wordpos = new WordPOS();
suite = new Bench.Suite({
type: 'fixed',
iterations: 1,
async: false, // important!
start: function(tests){
console.log('starting %d tests', tests.length);
},
result: function(name, stats){
var persec = 1000 / stats.elapsed
, ops = .5 + stats.iterations * persec;
console.log(' \033[90m%s : \033[36m%d \033[90mops/s\033[0m', name, ops | 0, stats);
pos && console.log(out(pos));
},
done: function(time){
console.log('looked up %d words, %d found', nwords, found);
console.log('done in %d msecs', time );
},
section: function(name, stats) {
console.log('\033[35m%s\033[0m',name);
}
});
function out(res){
return _(res).keys().map(function(k){
return k + ':' + res[k].length
});
}
var
text = fs.readFileSync('text-512.txt', 'utf8'),
parsedText = wordpos.parse(text),
nwords = parsedText.length,
pos;
function getPOS(next){
wordpos.getPOS(text, function(res){
pos = res;
next();
});
}
function getNouns(next){
wordpos.getNouns(text, function(res){
pos = {nouns: res};
next();
});
}
function getVerbs(next){
wordpos.getVerbs(text, function(res){
pos = {verbs: res};
next();
});
}
function getAdjectives(next){
wordpos.getAdjectives(text, function(res){
pos = {adjectives: res};
next();
});
}
function getAdverbs(next){
wordpos.getAdverbs(text, function(res){
pos = {adverbs: res};
next();
});
}
function lookup(next){
var count = nwords;
found = 0;
parsedText.forEach(function(word) {
wordpos.lookup(word, function (res) {
res.length && ++found;
if (--count === 0) next();
});
});
}
function lookupNoun(next){
var count = nwords;
found = 0;
parsedText.forEach(function(word) {
wordpos.lookupNoun(word, function (res) {
res.length && ++found;
if (--count === 0) next();
});
});
}
suite.section('--512 words--', function(next){
suite.options.iterations = 1;
next();
});
suite.bench('getPOS', getPOS);
suite.bench('getNouns', getNouns);
suite.bench('getVerbs', getVerbs);
suite.bench('getAdjectives', getAdjectives);
suite.bench('getAdverbs', getAdverbs);
suite.bench('lookup', lookup);
suite.bench('lookupNoun', lookupNoun);
suite.run();