-
Notifications
You must be signed in to change notification settings - Fork 1
/
import-conllu.js
87 lines (78 loc) · 2.32 KB
/
import-conllu.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
const process = require('process')
const fs = require('fs')
var conllu = require('conllujs')
var _ = require('lodash')
var redis = require('redis'),
client = redis.createClient(6399)
if (process.argv[4]) {
movieName = process.argv[2]
annotatorName = process.argv[3]
filepath = process.argv[4]
} else {
console.error('Pass two arguments: import-conllu.js movieName annotatorName filepath.conllu')
process.exit(0)
}
c = new conllu.Conllu()
c.serial = fs.readFileSync(filepath, 'utf8')
function parseWord(sentence, token) {
const t = _.split(token.misc, /\(|\)|:/)
if (t.length > 2)
return {
startTime: parseFloat(t[1]),
endTime: parseFloat(t[2]),
word: token.form,
sentId: parseInt(sentence.metadata[0].value),
sentIndex: parseInt(token.id),
tag: token.upostag,
}
return {
word: token.form,
sentId: parseInt(sentence.metadata[0].value),
sentIndex: parseInt(token.id),
tag: token.upostag,
}
}
const wordsWithTimes = []
_.forEach(c.sentences, s => {
_.forEach(s.tokens, token => {
const w = parseWord(s, token)
if (w.startTime) wordsWithTimes.push(w)
})
})
const contractions = ["'m", "n't", "'ve", "'re", "'s", "'ll", "'d"]
async function main() {
let lastStartTime = 0
await Promise.all(
_.map(c.sentences, async s => {
return await Promise.all(
_.map(s.tokens, async (token, idx) => {
const w = parseWord(s, token)
if (token.upostag !== 'PUNCT' && !_.includes(contractions, token.form)) {
if (s.tokens[idx + 1] && _.includes(contractions, s.tokens[idx + 1].form)) {
w.word = w.word + s.tokens[idx + 1].form
}
if (!w.startTime) {
w.word = w.word + '@'
w.startTime = lastStartTime + 0.1
w.endTime = lastStartTime + 0.3
} else lastStartTime = w.startTime
await client.zadd(
'movie:annotations:v3:' + movieName + ':' + annotatorName,
w.startTime,
JSON.stringify({
startTime: w.startTime,
endTime: w.endTime,
word: w.word,
sentId: w.sentId,
sentIndex: w.sentIndex,
tag: w.tag,
})
)
}
})
)
})
)
await client.quit()
}
main()