-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyse.js
executable file
·69 lines (56 loc) · 1.76 KB
/
analyse.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
#!/usr/bin/env node
const fs = require('fs')
const readline = require('readline')
const inputStream = readline.createInterface(fs.createReadStream('./parsed.txt'), {})
const outputStream = fs.createWriteStream('./analysed.txt')
const analyse = (accumulated, parseTree) => {
if (Array.isArray(parseTree)) {
const ruleName = parseTree[0];
switch (ruleName) {
case 'UI_clause':
accumulated = analyseOi(accumulated, parseTree)
break
default:
for (const subtree of parseTree) {
accumulated = analyse(accumulated, subtree)
}
}
} else {
switch (parseTree) {
case 'le':
case 'lo':
accumulated.set(parseTree, (accumulated.get(parseTree) || 0) + 1)
break
default:
break
}
}
return accumulated
}
const analyseOi = (accumulated, parseTree) => {
const [_, pre, post] = parseTree
accumulated.set('UI', (accumulated.get('UI') || 0) + 1)
if (pre[0][1] === 'oi') {
try {
if (post[1][0][1][0][1][1][0][1] !== 'nai') {
accumulated.set('oi', (accumulated.get('oi') || 0) + 1)
}
} catch (e) {
accumulated.set('oi', (accumulated.get('oi') || 0) + 1)
}
}
return analyse(accumulated, post)
}
let accumulatedPerYear = new Map()
inputStream.on('line', line => {
const [year, parseTreeJson] = line.split('\t')
const parseTree = JSON.parse(parseTreeJson)
accumulatedPerYear.set(year, analyse(accumulatedPerYear.get(year) || new Map(), parseTree))
})
inputStream.on('close', () => {
for (const [year, accumulated] of accumulatedPerYear) {
const loPerLe = accumulated.get('lo') / accumulated.get('le')
const oiPerUI = accumulated.get('oi') / accumulated.get('UI')
outputStream.write(`${year}\t${loPerLe}\t${oiPerUI}\n`)
}
})