-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
225 lines (192 loc) · 7.05 KB
/
Copy pathscript.js
File metadata and controls
225 lines (192 loc) · 7.05 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// ---------------------------------------------- Functions ----------------------------------------------
// Load JSON file
function loadJSON(jsonFile) {
return new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
xhr.open('GET', jsonFile, true);
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
var data = JSON.parse(xhr.responseText);
resolve(data);
} else {
reject('Request failed with status: ' + xhr.status);
}
};
xhr.onerror = function() {
reject('Request failed');
};
xhr.send();
});
}
// JSON to CSV
function jsonToCSV(jsonData) {
// Extracting unique symptoms
let symptomsSet = new Set();
jsonData.forEach(disease => {
disease.symptom.forEach(symptom => {
symptomsSet.add(symptom);
});
});
let symptomsList = Array.from(symptomsSet);
symptomsList = symptomsList.map(symptom => symptom.split(" ").join("_"));
// Creating CSV header
let csvContent = symptomsList.join(",") + ",male,female,infant,child,adult,senior,disease" + "\n";
jsonData.forEach(disease => {
let row = [];
symptomsList.forEach(symptom => {
if (disease.symptom.includes(symptom.split("_").join(" "))) {
row.push(1);
} else {
row.push(0);
}
});
csvContent += `${row.join(",")},${disease.gender === "male" ? 1 : 0},${disease.gender === "female" ? 1 : 0}`
+ `,${disease.ageGroup === "infant" ? 1 : 0},${disease.ageGroup === "child" ? 1 : 0},${disease.ageGroup === "adult" ? 1 : 0},${disease.ageGroup === "senior" ? 1 : 0},`
+ `${disease.name.split(" ").join("_")}\n`;
});
return csvContent;
}
// ---------------------------------------------- Utils ----------------------------------------------
const DisplayPart = document.getElementById('DisplayPart');
// ---------------------------------------------- Main ----------------------------------------------
const Main = async () => {
try {
let DisplayContent = 'Display Data Here...';
const data = await loadJSON('./NewData/NewData.json');
const herbs = await loadJSON('./NewData/HerbsData.json');
let contData= {};
data.forEach((item) => {
if (contData[item.name]) {
if(item.gender === "male")
++contData[item.name].male;
else
++contData[item.name].female;
if(item.ageGroup === "infant")
++contData[item.name].infant;
else if(item.ageGroup === "child")
++contData[item.name].child;
else if(item.ageGroup === "adult")
++contData[item.name].adult;
else
++contData[item.name].senior;
} else {
contData[item.name] = {
male: item.gender === "male" ? 1 : 0,
female: item.gender === "female"? 1 : 0,
infant: item.ageGroup === "infant" ? 1 : 0,
child: item.ageGroup === "child" ? 1 : 0,
adult: item.ageGroup === "adult" ? 1 : 0,
senior: item.ageGroup === "senior" ? 1 : 0
}
}
});
contData = Object.keys(contData).map((key) => {
return {
name: key,
...contData[key]
}
});
console.log(contData);
let symptomsSet = new Set();
let allSymptoms = 0;
data.forEach((item) => {
item.symptom.forEach((symptom) => {
symptomsSet.add(symptom.toLowerCase());
++allSymptoms;
});
});
console.log(Array.from(symptomsSet).sort());
// const csvContent = jsonToCSV(data);
// console.log(csvContent);
return;
let herbsSet = new Set();
herbs.forEach((item) => {
herbsSet.add(item.name.toLowerCase());
herbsSet.add(item.englishName.toLowerCase());
herbsSet.add(item.hindiName.toLowerCase());
});
console.log(herbsSet);
let dataSet = new Set();
let cnt1 = 0, cnt2 = 0;
data.forEach((item) => {
item.ayurvedicRemedies.forEach((remedy) => {
dataSet.add(remedy.toLowerCase());
++cnt1;
});
})
console.log(dataSet);
//find partial match of herbs in data
let commonSet = new Set();
herbsSet.forEach((item) => {
dataSet.forEach((dataItem) => {
if (dataItem.includes(item) || item.includes(dataItem)) {
commonSet.add(dataItem);
++cnt2;
}
});
});
console.log(commonSet);
console.log(cnt1, cnt2);
data.sort((a, b) => {
if (a.name > b.name) {
return 1;
} else if (a.name < b.name) {
return -1;
} else
a.gender > b.gender ? 1 : -1;
});
console.log(data);
// Display name of unique diseases
let uniqueDiseases = new Set();
let c = 0;
data.forEach((item) => {
if(uniqueDiseases.has({name: item.name, gender: item.gender, ageGroup: item.ageGroup}))
++c;
else
uniqueDiseases.add({name: item.name, gender: item.gender, ageGroup: item.ageGroup});
});
//sort the uniqueDiseases array
// uniqueDiseases.sort((a, b) => {
// if (a.name > b.name) {
// return 1;
// } else {
// return -1;
// }
// });
console.log(uniqueDiseases);
// console all the count of the all fields in data
let objKeys = Object.keys(data[0]);
let countObj = {};
objKeys.forEach((key) => {
countObj[key] = 0;
});
data.forEach((item) => {
objKeys.forEach((key) => {
countObj[key] += 1;
});
});
console.log(countObj);
// convert all the field [name, ageGroup, gender] and symptom array to lowercase
console.log("lowercase data")
let newData = [];
data.forEach((item) => {
console.log(item.name)
let newItem = item;
newItem.name = item.name.toLowerCase();
newItem.ageGroup = item.ageGroup.toLowerCase();
newItem.gender = item.gender.toLowerCase();
newItem.symptom = item.symptom.map((symp) => symp.toLowerCase());
newData.push(newItem);
});
console.log(newData);
let temp = '';
newData.forEach((item, index) => {
temp += `<p> => ${item.name}, ${item.ageGroup}, ${item.gender}<p>`;
});
DisplayContent = temp;
DisplayPart.innerHTML = DisplayContent;
} catch (error) {
console.log(error);
}
}
Main();