- Put
convert.js
into a folder npm i xml2js
Dump into stdout:
node convert.js path/to/file.annot
Save to a file:
node convert.js path/to/file.annot > annotations.txt
const fs = require('fs'); | |
const xml2js = require('xml2js'); | |
const filePath = process.argv[2]; | |
if (!filePath) { | |
console.error('Please provide .annot file path as an argument'); | |
process.exit(1); | |
} | |
const fileContent = fs.readFileSync(filePath).toString(); | |
const parser = new xml2js.Parser(); | |
parser.parseString(fileContent, (err, result) => { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
const fragments = result.annotationSet.annotation.map(a => a.target[0].fragment); | |
const fragmentsSorted = fragments.sort((a, b) => { | |
return parseFloat(a[0]['$']['progress']) - parseFloat(b[0]['$']['progress']); | |
}); | |
const texts = fragmentsSorted.map(f => f[0]['text'][0]); | |
console.log(texts.join('\n\n---\n\n')); | |
}); |