Skip to content

Commit

Permalink
relation model to handle data
Browse files Browse the repository at this point in the history
  • Loading branch information
kjj6198 committed Nov 12, 2020
1 parent 8a68465 commit 4904c94
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/models/relation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
export default class Relation {
constructor(data) {
const keywords = ["年份", "年齡層"];
const res = {};

data.forEach((d) => {
const result = {};
Object.keys(d).forEach((key) => {
if (!keywords.includes(key)) {
result[key] = parseInt(d[key].replace(",", ""), 10);
}
});

Object.assign(res, {
[d["年齡層"]]: result,
});
});

this.data = res;
}

getTotalFor(relationship) {
let sum = 0;

Object.keys(this.data).forEach((k) => (sum += this.data[k][relationship]));

return sum;
}

getProbabilityFor(name) {
let totalSum = 0;
let targetSum = 0;

Object.keys(this.data).forEach((k) => {
totalSum += this.data[k]["總計"];
targetSum += this.data[k][name];
});

return targetSum / totalSum;
}

// return
// [relation, number]
getRanking() {
const relations = [
"配偶",
"前配偶",
"直系親屬",
"旁系親屬",
"家人的朋友",
"未婚夫/妻",
"男/女朋友",
"前男/女朋友",
"普通朋友",
"同事",
"同學",
"網友",
"客戶關係",
"上司/下屬",
"師生關係",
"鄰居",
"不認識",
];

const ranking = {};
relations.forEach((relation) => (ranking[relation] = 0));

Object.keys(this.data).forEach((k) => {
const data = this.data[k];

relations.forEach((relation) => {
ranking[relation] += data[relation];
});
});

return Object.keys(ranking)
.map((relation) => {
return [relation, ranking[relation]];
})
.sort((a, b) => b[1] - a[1]);
}
}

0 comments on commit 4904c94

Please sign in to comment.