Download src/algorithm/WordCloud.ts from nolual/majic: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/nolual/majic/resolve/main/src/algorithm/WordCloud.ts
- Command line
-
hf download hf://nolual/majic/src/algorithm/WordCloud.ts
-
curl -L -o WordCloud.ts https://huggingface.co/nolual/majic/resolve/main/src/algorithm/WordCloud.ts
3.1 kB
| 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; | |
| } | |