123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- // librairie de gestion des répertoires et fichiers
- const fs = require('fs');
- const path = require('path');
- // librairie de gestion des arguments de la ligne de commande
- var argv = require('optimist').argv;
- // libvrairie de lecture de ligens de texte
- //var readline = require('readline');
- // librairie de traitement du pinyin
- const pinyinizer = require('pinyinizer');
- // librairie de gestion du hanzi
- //const opencc = require('node-opencc');
- // librairie de gestion des fichiers xml
- const builder = require('xmlbuilder');
- // librairie de gestion des fichiers csv
- //var csv = require('comma-separated-values');
- // données de stockage
- //var dataJson = [];
- //var dataCSV = [];
- // les noms de fichiers
- var fichierXml = '';
- var fichierJson = '';
- var fichierCSV = '';
- // en entrée, on donne le nom du fichier à traiter sans le répertoire
- var repertoireInitial = './';
- var argument = argv._[0];
- var fichierDonnees = path.join(repertoireInitial, argument);
- // DEBUG
- // console.log(fichierDonnees);
- var data = require(fichierDonnees);
- // utilitaires
- var tab = '\t';
- var endLine = '\n';
- // Variables fichier des fichiers csv, json et xml
- fichierXml = path.join(repertoireInitial, argument + '.xml');
- fichierJson = path.join(repertoireInitial, argument + '.json');
- fichierCSV = path.join(repertoireInitial, argument + '-1.csv');
- var ligne = {
- hanzi: '',
- traditional: '',
- pinyin: '',
- translations: [],
- origin: ''
- }
- // on crée le fichier csv
- var moncsv = fs.createWriteStream(fichierCSV);
- // on crée le fichier json
- var json = fs.createWriteStream(fichierJson);
- json.write('{"CFdict":[');
- // on crée le fichier xml
- var feed = builder.create('cfdicttable', {
- version: '1.0',
- encoding: 'UTF-8',
- standalone: true
- });
- // la ligne CSV qu'on va écrire dans le fichier CSV
- var ligneCSV = '';
- // L'élément xml qui constitue un enregistrement du fichier xml
- var ele = null;
- function resetLigne () {
- ligne.hanzi = '';
- ligne.traditional = '';
- ligne.pinyin = '';
- ligne.translations = [];
- ligne.origin = '';
- }
- var mesDonnees = data.dic.word;
- mesDonnees.forEach(function(item, index){
- resetLigne()
- ligneCSV = ""
- pinyin = pinyinizer.pinyinize(item.py[0])
- traduction = item.trans[0].fr
- //console.log(index, item.simp[0], item.trad[0], pinyin, item.trans[0].fr)
- ligne.hanzi = item.simp[0]
- ligne.traditional = item.trad[0]
- ligne.pinyin = pinyin
- ligne.translations = traduction
- ligne.origin = "CFdict"
- //console.log(ligne)
- trad = traduction.toString()
- trad = trad.replace(/\,/g, ' / ' )
- ligneCSV = item.simp[0] + tab +
- item.trad[0] + tab +
- pinyin + tab +
- trad + tab +
- "CFdict" +
- endLine
- //console.log(ligneCSV)
- // Ecriture de la ligne CSV
- moncsv.write(ligneCSV);
- // Ecriture de la ligne json
- json.write(JSON.stringify(ligne) + ",");
- // écriture de la ligne xml
- var ele = feed.ele({ligne});
- });
- // Fermeture des fichiers json, xml et csv
- // on ferme le fichier CSV
- moncsv.end();
- // on écrit le fichier xml
- fs.writeFileSync(fichierXml, feed.end({
- pretty: true
- }));
- // on écrit le fichier Json
- // fs.writeFileSync(fichierJson, JSON.stringify(maineeduObj), "UTF-8");
- json.write(']}');
- json.end();
|