-
Notifications
You must be signed in to change notification settings - Fork 0
/
stemmer.js
75 lines (60 loc) · 2.23 KB
/
stemmer.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
var fs = require('fs'),
db = require('diskdb'),
_ = require('lodash'),
async = require('async'),
path = require('path'),
storage = db.connect(__dirname + '/contents', ['records']),
mkdirp = require('mkdirp'),
tokenize = require('talisman/tokenizers/words/treebank').default,
stem = require('talisman/stemmers/uea-lite').default,
stopwords = require('./stopwords/en');
// console.log()
var total = db.records.count();
var q = async.queue(function(filepath, nextFile) {
console.log('remaining: ', q.length(),'/', total);
async.waterfall([
function getContents(next) {
console.log('-- reading file: ', filepath)
fs.readFile(filepath, {encoding: 'utf8'}, next);
},
function(contents, next) {
console.log('-- stemming file: ', filepath);
var stems = _(tokenize(contents))
.map(d => d.toLowerCase().replace(/\d/g,'')) // clean from numbers and lowercase text
//.map(d => d.replace(/\d/g,'')) // clean from numbers without lowercasing text
.map(d => d.replace(/[\d\.:\/,;\?\!<>@`'"]/g, '').trim()) // clean from digits
.compact() // remove empty strings
.map(d => stem(d)) // stem remaining
.filter(d => stopwords.indexOf(d.toLowerCase()) == -1) // remove stopwords
.filter(d => d.length > 1)// filter on word length
.value();
next(null, stems)
// next(null, contents);
},
function(contents, next) {
var outdir = path.join(path.dirname(filepath), 'stems'),
outpath = path.join(outdir, path.basename(filepath) + '.stemmed.txt');
// create if it does not exists
console.log('-- storing stanfordNER responses in: ', outpath + '.NER.json');
mkdirp(outdir, function (err) {
if (err)
next(err);
else
fs.writeFile(outpath, contents, next);
});
}
], function(err) {
if(err) {
q.kill();
console.error(err)
}
nextFile();
});
}, 3);
q.push(_(db.records.find())
.map('src') // get just the src part
// .take(5) // uncomment for testing
.value())
q.drain = function() {
console.log('oh, yeh');
}