-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrect-lists.js
More file actions
52 lines (49 loc) · 1.65 KB
/
Copy pathcorrect-lists.js
File metadata and controls
52 lines (49 loc) · 1.65 KB
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
var db = require('./lib/db');
var _ = require('lodash');
var async = require('async');
var corrections = _.map(['phrases_list', 'phrases_party_list', 'phrases_speaker_ids_list'], function(table){
return function(cb){ correctPhrasesList(table, cb); };
});
async.series(corrections, function(err){
console.error('done');
db.end();
console.log(err);
});
function correctPhrasesList(table, cb){
var query = 'select * from ' + table;
console.error('doing ' + table);
db.query(query, [], function(err, result) {
if (err) return cb(err);
async.forEachSeries(result.rows, function(row, next){
var updatedNeeded = false;
var data = JSON.parse(row.data);
data.forEach(function(datum){
if (datum.text.split(' ').length > 3 && datum.score && datum.frequency !== datum.score){
console.error('updating ' + datum.text + ' from ' + datum.frequency + ' to ' + datum.score);
datum.frequency = datum.score;
updatedNeeded = true;
}
});
if (updatedNeeded){
console.error('updated need for ' + row.year);
data = _.sortBy(data, 'frequency').reverse();
var update = 'update ' + table + ' set data = $1 where year = $2 ';
var params = [data, row.year];
if (row.party){
update += ' and party = $3 ';
params.push(row.party);
}
if (row.speaker_id){
update += ' and speaker_id = $3 ';
params.push(row.speaker_id);
}
console.error('updating with ' + update);
console.error(params);
//db.query(update, params, next);
next();
}else{
next();
}
}, cb);
});
}