File size: 3,098 Bytes
0c20ea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
interface SocioDemographicData {
  age: string;
  gender: string;
  percentage: number;
  social_group: string;
}

type SynonymsMap = Record<string, string[]>;

export function characterizeSocioDemographicData(
  data: SocioDemographicData[]
): string {
  const result: string[] = [];

  const ageSynonyms: SynonymsMap = {
    "15-24": ["young", "youthful", "adolescent", "teenage", "juvenile"],
    "25-34": ["young adult", "early adult", "youthful", "prime of life"],
    "35-49": ["middle-aged", "adult", "mature", "grown-up"],
    "50-64": ["senior", "elderly", "aged", "mature", "golden-aged"],
    "65-PLUS": ["elderly", "senior", "aged", "retired", "elder"],
  };

  const socialGroupSynonyms: SynonymsMap = {
    "1": ["poorest", "impoverished", "deprived", "underprivileged", "needy"],
    "2": [
      "poor",
      "low-income",
      "economically challenged",
      "struggling",
      "disadvantaged",
    ],
    "3": ["poor", "struggling", "disadvantaged", "less fortunate", "in need"],
    "4": ["middle-income", "average", "moderate", "ordinary", "typical"],
    "5": ["middle-income", "average", "moderate", "ordinary", "typical"],
    "6": ["middle-income", "average", "moderate", "ordinary", "typical"],
    "7": ["rich", "affluent", "well-off", "prosperous", "wealthy"],
    "8": ["rich", "wealthy", "opulent", "affluent", "privileged"],
    "9": ["richest", "privileged", "wealthiest", "affluent", "opulent"],
    "10": ["richest", "wealthiest", "opulent", "affluent", "privileged"],
  };

  const crowdedSynonyms = [
    "crowded",
    "dense",
    "populated",
    "teeming",
    "packed",
  ];
  const notCrowdedSynonyms = [
    "not that crowded",
    "sparsely populated",
    "unpopulated",
    "deserted",
    "vacant",
  ];

  const groupedData = data.reduce((groups, item) => {
    const key = `${item.age}-${item.gender}-${item.social_group}`;
    if (!groups[key]) {
      groups[key] = [];
    }
    groups[key].push(item.percentage);
    return groups;
  }, {} as Record<string, number[]>);

  for (const key in groupedData) {
    if (groupedData.hasOwnProperty(key)) {
      const percentages = groupedData[key];
      const averagePercentage =
        percentages.reduce((sum, percentage) => sum + percentage, 0) /
        percentages.length;

      if (averagePercentage > 0.1) {
        result.push(...crowdedSynonyms);
      } else {
        result.push(...notCrowdedSynonyms);
      }

      const age = key.split("-")[0];
      const randomAgeSynonym = ageSynonyms[age]
        ? ageSynonyms[age][Math.floor(Math.random() * ageSynonyms[age].length)]
        : age;
      result.push(randomAgeSynonym);

      const socialGroup = key.split("-")[2];
      const randomSocialGroupSynonym = socialGroupSynonyms[socialGroup]
        ? socialGroupSynonyms[socialGroup][
            Math.floor(Math.random() * socialGroupSynonyms[socialGroup].length)
          ]
        : "social group " + socialGroup;
      result.push(randomSocialGroupSynonym);
    }
  }

  const finalString = result.filter((word) => isNaN(Number(word))).join(" ");

  return finalString;
}