CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
tree.js93 linesDownload Raw Back to test
1import { emojiComponents } from "../data.js";2/**3* Merge types for unique key4*/5function mergeComponentTypes(value) {6	return "[" + value.join(",") + "]";7}8/**9* Merge count for unique key10*/11function mergeComponentsCount(value) {12	const keys = [];13	for (const key in emojiComponents) {14		const type = key;15		for (let i = 0; i < value[type]; i++) keys.push(type);16	}17	return keys.length ? mergeComponentTypes(keys) : "";18}19/**20* Get item from group21*/22function getGroupItem(items, components) {23	const item = items[mergeComponentsCount(components)];24	if (item) {25		item.parsed = true;26		return item.item;27	}28}29/**30* Convert test data to dependencies tree, based on components31*/32function getEmojiTestDataTree(data) {33	const groups = Object.create(null);34	for (const key in data) {35		const item = data[key];36		const text = item.name.key;37		const parent = groups[text] || (groups[text] = {});38		const components = {39			"hair-style": 0,40			"skin-tone": 041		};42		item.sequence.forEach((value) => {43			if (typeof value !== "number") components[value]++;44		});45		const componentsKey = mergeComponentsCount(components);46		if (parent[componentsKey]) throw new Error(`Duplicate components tree item for "${text}"`);47		parent[componentsKey] = { item: {48			...item,49			components,50			componentsKey51		} };52	}53	const results = Object.create(null);54	for (const key in groups) {55		const items = groups[key];56		const check = (parent, parentComponents, type) => {57			const item = parse(parentComponents, [type]);58			if (item) {59				const children = parent.children || (parent.children = {});60				children[type] = item;61				return true;62			}63		};64		const parse = (parentComponents, newComponents) => {65			const components = {66				"hair-style": 0,67				"skin-tone": 068			};69			const componentsList = parentComponents.concat(newComponents);70			componentsList.forEach((type) => {71				components[type]++;72			});73			let item = getGroupItem(items, components);74			if (!item && newComponents.length === 1 && newComponents[0] === "skin-tone") {75				const doubleComponents = { ...components };76				doubleComponents["skin-tone"]++;77				item = getGroupItem(items, doubleComponents);78			}79			if (item) {80				const result = { item };81				for (const key in emojiComponents) check(result, componentsList, key);82				return result;83			}84		};85		const root = parse([], []);86		if (!root) throw new Error(`Cannot find parent item for "${key}"`);87		for (const itemsKey in items) if (!items[itemsKey].parsed) throw new Error(`Error generating tree for "${key}"`);88		if (root.children) results[key] = root;89	}90	return results;91}92export { getEmojiTestDataTree };93 
basant307/AI_Governance_Project · CoolFace