reductcsv.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // librairie de gestion des répertoires et fichiers
  2. const fs = require("fs");
  3. const path = require("path");
  4. // librairie de gestion des fichiers csv
  5. var csv = require("comma-separated-values");
  6. // librairie de traitement du pinyin
  7. const pinyinizer = require('pinyinizer');
  8. // on crée le fichier csv de sortie
  9. var moncsvSortie = fs.createWriteStream("../MaineEdu-vocabulary-1.csv");
  10. // on crée le fichier csv d'entrée
  11. var moncsvEntree = "../MaineEdu-vocabulary.csv";
  12. // utilitaires
  13. var tab = "\t";
  14. var endLine = "\n";
  15. var ligneCSV = "";
  16. var fichierATraiter = fs.readFileSync(moncsvEntree, "UTF-8");
  17. var monjson = new csv(fichierATraiter, {
  18. header: [
  19. 'hanzi',
  20. 'traditional',
  21. 'pinyin',
  22. 'translation',
  23. 'classifier',
  24. "taiwan-pinyin",
  25. "sameword",
  26. 'example'
  27. ],
  28. cast: [
  29. 'String',
  30. 'String',
  31. 'String',
  32. 'String',
  33. 'String',
  34. 'String',
  35. 'String',
  36. 'String'
  37. ]
  38. }).parse();
  39. monjson.forEach(function(enregistrement){
  40. // console.log(enregistrement.hanzi);
  41. ligneCSV = "";
  42. // traitement de la ligne
  43. console.log(enregistrement.translation);
  44. enregistrement.translation = pinyinizer.pinyinize(enregistrement.translation);
  45. console.log(enregistrement.translation);
  46. ligneCSV = enregistrement.hanzi + tab +
  47. enregistrement.traditional + tab +
  48. enregistrement.pinyin + tab +
  49. enregistrement.translation + tab +
  50. enregistrement.classifier + tab +
  51. enregistrement.example +
  52. endLine;
  53. moncsvSortie.write(ligneCSV);
  54. });