-
Notifications
You must be signed in to change notification settings - Fork 0
/
strokeCount.js
28 lines (23 loc) · 870 Bytes
/
strokeCount.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
const fs = require('fs');
const { exec } = require('child_process');
function strokeCount(words) {
return new Promise((resolve) => {
// load words into strokecounter/datatocount.txt
fs.writeFile('./strokecounter/datatocount.txt', words.join('\n') + '\n', (err) => {
// done. run perl script
exec('cd strokecounter && perl strokecounter_batchmode.pl', (err, stdout, stderr) => {
// load words from strokecounter/strokecounts.txt
fs.readFile( './strokecounter/strokecounts.txt', 'utf-8', function ( err, data ) {
let response = {};
const lines = data.split('\n');
words.forEach((word, i) => {
response[word] = parseInt( lines[i].trim(), 10 );
});
// return result
resolve( response );
} );
});
} );
});
}
module.exports = strokeCount;