From 12b806d7eca5ace7aee48899f2e80301fe7f4bf3 Mon Sep 17 00:00:00 2001 From: Caussz Date: Tue, 2 Jul 2024 16:34:08 -0300 Subject: [PATCH] FEAT: Adding new function for graph generator #29 --- src/utils/graph/graphGenerator.ts | 79 ++++++++++++++++++++++++------- src/utils/index.ts | 4 +- 2 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/utils/graph/graphGenerator.ts b/src/utils/graph/graphGenerator.ts index 6a501fd..4fe040c 100644 --- a/src/utils/graph/graphGenerator.ts +++ b/src/utils/graph/graphGenerator.ts @@ -2,28 +2,75 @@ import type { Nodes, Edges } from 'v-network-graph'; import type { IArticle } from '@/interfaces'; -function generateGraphFromArticles(articles: IArticle[]) { +function generateGraphFromArticles(articles: IArticle[]): { nodes: Nodes, edges: Edges } { const nodes = articles.reduce((acc, article, index) => { - acc[`node${index}`] = { name: article.label, ...article }; - return acc; + acc[`node${index}`] = { name: article.label, ...article }; + return acc; }, {} as Nodes); const edges = articles.reduce((acc, article, index) => { - article.keywords.forEach((keyword) => { - articles.forEach((otherArticle, otherIndex) => { - if (index !== otherIndex && otherArticle.keywords.includes(keyword)) { - const edgeKey1 = `edge${index}-${otherIndex}`; - const edgeKey2 = `edge${otherIndex}-${index}`; - if (!acc[edgeKey1] && !acc[edgeKey2]) { - acc[edgeKey1] = { source: `node${index}`, target: `node${otherIndex}`, label: keyword }; - } - } - }); + article.keywords.forEach((keyword) => { + articles.forEach((otherArticle, otherIndex) => { + if (index !== otherIndex && otherArticle.keywords.includes(keyword)) { + const edgeKey1 = `edge${index}-${otherIndex}`; + const edgeKey2 = `edge${otherIndex}-${index}`; + if (!acc[edgeKey1] && !acc[edgeKey2]) { + acc[edgeKey1] = { source: `node${index}`, target: `node${otherIndex}`, label: keyword, width: 1 }; + } else { + acc[edgeKey1] ? acc[edgeKey1].width += 1 : acc[edgeKey2].width += 1; + } + } }); - return acc; + }); + return acc; }, {} as Edges); return { nodes, edges }; -} + } -export { generateGraphFromArticles }; +function generateGraphFromKeywords(articles: IArticle[]): { nodes: Nodes, edges: Edges } { + const keywordSet = new Set(); + articles.forEach(article => { + article.keywords.forEach(keyword => { + keywordSet.add(keyword); + }); + }); + + const keywords = Array.from(keywordSet); + const nodes = keywords.reduce((acc, keyword, index) => { + acc[`node${index}`] = { name: keyword, radius: 16 }; + return acc; + }, {} as Nodes); + + const keywordConnections = articles.reduce((acc, article) => { + article.keywords.forEach((keyword) => { + if (!acc[keyword]) { + acc[keyword] = 0; + } + acc[keyword] += 1; + }); + return acc; + }, {} as Record); + + const edges = articles.reduce((acc, article, index) => { + article.keywords.forEach((keyword) => { + const sourceIndex = keywords.indexOf(keyword); + const edgeKey = `edge${index}-${sourceIndex}`; + if (!acc[edgeKey]) { + acc[edgeKey] = { source: `node${index}`, target: `node${sourceIndex}`, label: article.label }; + } + }); + return acc; + }, {} as Edges); + + for (const [keyword, count] of Object.entries(keywordConnections)) { + const nodeIndex = keywords.indexOf(keyword); + if (nodes[`node${nodeIndex}`]) { + nodes[`node${nodeIndex}`].width = 16 + count * 2; + } + } + + return { nodes, edges }; + } + +export { generateGraphFromArticles, generateGraphFromKeywords }; diff --git a/src/utils/index.ts b/src/utils/index.ts index eecdbf3..981aa07 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,3 @@ -import { generateGraphFromArticles } from "./graph/graphGenerator"; +import { generateGraphFromArticles, generateGraphFromKeywords } from "./graph/graphGenerator"; -export { generateGraphFromArticles } \ No newline at end of file +export { generateGraphFromArticles, generateGraphFromKeywords } \ No newline at end of file