prepareData.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const config = require('../build.json');
  5. var languages = ['de', 'en'];
  6. var lang = process.argv[2];
  7. if(languages.indexOf(lang) === -1) {
  8. lang = languages[0];
  9. }
  10. // Change current directory to script location so that
  11. // the relative paths of the resources can be resolved
  12. process.chdir(path.dirname(process.argv[1]));
  13. function createDirectory(dir) {
  14. fs.mkdirSync(
  15. dir,
  16. { recursive: true },
  17. (error) => {
  18. if (error !== null) {
  19. console.error('Error while creating directory', error);
  20. }
  21. }
  22. );
  23. }
  24. function transformData() {
  25. var matrix = fs.readFileSync(path.join('../', config.path.data, 'jobloss_' + lang + '.tsv'), 'utf8');
  26. // TSV wird geparst
  27. matrix = matrix.trim();
  28. var lines = matrix.split('\n');
  29. matrix = matrix.split('\n').map(l => l.split('\t'));
  30. var rowCount = matrix.length;
  31. var result = [];
  32. // Welche Spalten enthalten die Punkte für Fähig- und Fertigkeiten
  33. var pointCols = [6,7,8,9,10,11,12,13];
  34. var pointColHeaders = matrix[1].map(function(h, i){
  35. if (pointCols.indexOf(i) !== -1) {
  36. return h;
  37. }
  38. });
  39. for (var r = 2; r < rowCount; r++) {
  40. var row = matrix[r];
  41. // Lese die einzelnen Jobs ein
  42. var entry = {
  43. level0: row[0],
  44. level1: row[1],
  45. label: row[2],
  46. prob: parseFloat(row[4].replace(/,/g, '.')),
  47. points: pointCols.map(c => parseFloat(row[c].replace(/,/g, '.'))),
  48. // index: result.length
  49. };
  50. // Jobs mit leerer Wahrscheinlichkeit werden ignoriert
  51. if (entry.prob) {
  52. entry.index = result.length;
  53. result.push(entry);
  54. }
  55. }
  56. // Vergleiche jeden Job mit jedem Job, um die ähnlichsten zu finden
  57. result.forEach(entry1 => {
  58. var vec1 = entry1.points;
  59. var similar = [];
  60. result.forEach(entry2 => {
  61. // Vergleiche nicht Jobs mit sich selbst
  62. if (entry1 === entry2) return;
  63. // Jobs die eine höhere Verlust-Wahrscheinlichkeit haben, werden ignoriert
  64. if (entry2.prob >= entry1.prob) return;
  65. var vec2 = entry2.points;
  66. var sum = 0;
  67. vec1.forEach((v1,i) => sum += sqr(v1-vec2[i]));
  68. similar.push({
  69. index: entry2.index,
  70. prob: entry2.prob,
  71. distance: sum
  72. });
  73. function sqr(v) { return v*v; }
  74. });
  75. // Finde die 5 besten der ähnlichen Jobs
  76. similar.sort((a,b) => a.distance-b.distance);
  77. similar = similar.slice(0,5);
  78. entry1.similar = similar;
  79. });
  80. // Bereite JSON fürs Speichern vor.
  81. result.forEach(entry => {
  82. delete entry.index;
  83. entry.skills = entry.points.map(function(value,i) {
  84. return value;
  85. // return {
  86. // 'name': pointColHeaders[i + pointCols[0]],
  87. // 'value': value
  88. // };
  89. });
  90. delete entry.points;
  91. entry.similar.forEach(s => {delete s.distance;});
  92. });
  93. // Resultat als JSON speichern
  94. fs.writeFileSync(
  95. path.join(
  96. '../',
  97. config.path.output,
  98. config.path.data,
  99. 'data_' + lang + '.json'),
  100. JSON.stringify(result, null, '\t'),
  101. 'utf8'
  102. );
  103. }
  104. createDirectory(path.join('../', config.path.output, config.path.data));
  105. transformData();