-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopulate.js
59 lines (55 loc) · 1.74 KB
/
populate.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
const process = require('process')
const csv = require('csv-parser')
const fs = require('fs')
var _ = require('lodash')
const { exec, execSync } = require('child_process')
var redis = require('redis'),
client = redis.createClient(6399)
if (process.argv[3]) {
movieName = process.argv[2]
annotatorName = process.argv[3]
} else {
console.log('Pass two arguments: populate.js movieName annotatorName')
process.exit(0)
}
allWords = []
fs.createReadStream('movies/' + movieName + '/word-times-' + annotatorName + '.csv')
.pipe(csv())
.on('data', row => {
row.start = row.start / 1000
row.end = row.end / 1000
allWords.push(row)
})
.on('end', () => {
console.log('CSV read')
console.log(allWords)
segments = []
_.forEach(fs.readdirSync('public/spectrograms/'), item => {
if (_.startsWith(item, movieName) && _.endsWith(item, '.png')) {
segments.push(_.tail(_.split(_.split(item, '.')[0], ':')))
}
})
_.forEach(segments, segment => {
start = segment[0] // seconds
end = segment[1]
segmentName = movieName + ':' + segment[0] + ':' + segment[1]
words = _.uniq(
_.map(
_.filter(allWords, word => word.start >= start && word.end <= end),
word => word.sentence
)
)
fs.writeFileSync('public/words/' + segmentName + '.words', _.join(words, ' '))
fs.appendFileSync('segments', segmentName + '\n')
})
console.log('Redis')
_.forEach(allWords, word =>
client.zadd(
'movie:annotations:v3:' + movieName + ':' + annotatorName,
word.start,
JSON.stringify({ startTime: word.start, endTime: word.end, word: word.text })
)
)
console.log('Finished uploading to redis')
process.exit(0)
})