-
Notifications
You must be signed in to change notification settings - Fork 8
/
exampleScript.js
executable file
·74 lines (69 loc) · 2.48 KB
/
exampleScript.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
#!/usr/bin/env node
/**
* @fileoverview Node.js script filling given JSON file with abbreviations.
*
* It expects a JSON object with a key 'abbrev', like:
* {
* 'abbrevs': {
* 'Journal of Foo': {},
* 'Journal of Bar': {},
* ...
* },
* ...
* }
* Under 'abbrevs', an object maps titles to abbreviation data.
* When empty (or when 'reset' is given), this data is replaced by:
* {
* 'all': 'abbrev using all language rules',
* 'eng': 'abbrev using eng, mul (multilanguage) and lat (Latin) rules',
* 'matchingPatterns': 'LTWA lines whose patterns matched, one per line'
* }
* The speed is roughly 1h for 10000 titles.
*/
'use strict';
const fs = require('fs');
const AbbrevIso = require('./nodeBundle.js');
// Parse command-line arguments.
if (process.argv.length < 3 || process.argv[2] == 'reset')
throw new Error('No filename given.');
const stateFileName = process.argv[2];
const recomputeAll = process.argv.includes('reset');
// Open JSON 'state' file.
let state = fs.readFileSync(stateFileName, 'utf8');
state = JSON.parse(state);
if (!(state instanceof Object) || !('abbrevs' in state))
throw new Error('Invalid file: expected object with "abbrevs" key.');
// Load abbrevISO.
const ltwa = fs.readFileSync(__dirname + '/LTWA_20210702-modified.csv', 'utf8');
const shortWords = fs.readFileSync(__dirname + '/shortwords.txt', 'utf8');
const abbrevIso = new AbbrevIso.AbbrevIso(ltwa, shortWords);
// Compute the data in state['abbrevs'][title] for all titles.
for (let [title, data] of Object.entries(state['abbrevs'])) {
let changed = false;
if (typeof(data) !== 'object')
data = {'all': null, 'eng': null, 'matchingPatterns': null};
for (const lang of Object.keys(data)) {
if (data[lang] !== null && !recomputeAll)
continue;
changed = true;
const t = title.normalize('NFC');
if (lang == 'matchingPatterns') {
const matchingPatterns = abbrevIso.getMatchingPatterns(t);
let s = '';
for (const pattern of matchingPatterns)
s += pattern.line + '\n';
data[lang] = s;
} else if (lang == 'all') {
data[lang] = abbrevIso.makeAbbreviation(t);
} else {
const langSet = lang.split(',').concat([, 'mul', 'lat']);
data[lang] = abbrevIso.makeAbbreviation(t, langSet);
}
if (lang != 'matchingPatterns')
console.log(`"${t}"\t[${lang}]\t->\t${data[lang]}`);
}
if (changed)
state['abbrevs'][title] = data;
}
// Save JSON 'state' file.
fs.writeFileSync(stateFileName, JSON.stringify(state), 'utf8');