The code is a Node.js module that generates a word cloud for a project. It uses the d3
library to convert a word cloud to SVG and the path
module to work with file and directory paths.
npm run import -- "project word-cloud"
var importer = require('../Core');
var path = require('path');
function projectWordCloud(project) {
var d3CloudToSVG = importer.import("d3.ipynb[create word-cloud");
var relativePaths = importer.import("relative paths and includes",
"{project}");
var words = [];
function wordCount(r) {
var words = r['packages'].map(p => p.import)
.concat(r['packages'].map(p => path.basename(p.file)))
.concat(r['relatives'].map(r => path.basename(r.import)))
.concat(r['relatives'].map(r => path.basename(r.file)));
var wordCount = {};
words.forEach(w => {
if (typeof wordCount[w] === 'undefined') {
wordCount[w] = 15;
} else {
wordCount[w]++;
}
});
return Object.keys(wordCount).map((d) => ({text: d, size: wordCount[d]}));
};
return relativePaths(project)
.then(words => d3CloudToSVG(wordCount(words)));
}
module.exports = projectWordCloud;
if(typeof $ !== 'undefined') {
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var project = PROFILE_PATH + '/Documents/portal';
$.async()
projectWordCloud(project)
.then(svg => $.svg(svg))
.catch(e => $.sendError(e));
}
const importer = require('../Core');
const path = require('path');
/**
* Generates a word cloud for a given project.
* @param {Object} project - The project to generate the word cloud for.
* @returns {Promise} A promise that resolves to the SVG representation of the word cloud.
*/
function projectWordCloud(project) {
// Import the necessary functions from the Core module.
const { d3CloudToSVG, relativePaths } = importer.import([
'd3.ipynb[create word-cloud]',
'relative paths and includes',
], { project });
// Initialize an empty array to store the words.
const words = [];
/**
* Counts the occurrences of each word in a project.
* @param {Object} r - The project data.
* @returns {Object[]} An array of objects containing the word and its frequency.
*/
function wordCount(r) {
// Extract the package and relative import names.
const packageNames = r.packages.map(p => p.import);
const relativeImportNames = r.relatives.map(r => path.basename(r.import));
// Combine the names and count the occurrences of each word.
const wordCount = packageNames
.concat(r.packages.map(p => path.basename(p.file)))
.concat(relativeImportNames)
.reduce((acc, word) => {
if (acc[word]) {
acc[word]++;
} else {
acc[word] = 1;
}
return acc;
}, {});
// Convert the word count object to an array of objects.
return Object.keys(wordCount).map(word => ({ text: word, size: wordCount[word] }));
}
// Chain the relativePaths and d3CloudToSVG functions together.
return relativePaths(project)
.then(words => d3CloudToSVG(wordCount(words)));
}
module.exports = projectWordCloud;
// TODO: Remove this block when it's no longer needed.
if (typeof $!== 'undefined') {
const PROFILE_PATH = [
process.env.HOME,
process.env.HOMEPATH,
process.env.USERPROFILE,
].find(Boolean) || '';
const project = PROFILE_PATH + '/Documents/portal';
$.async()
.then(() => projectWordCloud(project))
.then(svg => $.svg(svg))
.catch(e => $.sendError(e));
}
var importer = require('../Core');
var path = require('path');
function projectWordCloud(project) {
...
}
../Core
and path
. The path
module is a Node.js built-in module for working with file and directory paths.projectWordCloud
function is defined, which takes a project
parameter.var d3CloudToSVG = importer.import('d3.ipynb[create word-cloud]');
var relativePaths = importer.import('relative paths and includes', {project});
importer
module is used to import two modules: d3CloudToSVG
and relativePaths
.d3CloudToSVG
is imported from d3.ipynb[create word-cloud]
. This suggests that it's a function to convert a word cloud to SVG.relativePaths
is imported from relative paths and includes
with the project
parameter. This suggests that it's a function to get relative paths and includes for a project.function wordCount(r) {
var words = r['packages'].map(p => p.import)
.concat(r['packages'].map(p => path.basename(p.file)))
.concat(r['relatives'].map(r => path.basename(r.import)))
.concat(r['relatives'].map(r => path.basename(r.file)));
var wordCount = {};
words.forEach(w => {
if (typeof wordCount[w] === 'undefined') {
wordCount[w] = 15;
} else {
wordCount[w]++;
}
});
return Object.keys(wordCount).map((d) => ({text: d, size: wordCount[d]}));
};
wordCount
function is defined, which takes an object r
as a parameter.packages
and relatives
properties of r
, extracting the import and file names, and concatenates them into an array words
.wordCount
and iterates over words
, incrementing the count for each word in wordCount
.text
property (the word) and a size
property (the count).return relativePaths(project)
.then(words => d3CloudToSVG(wordCount(words)));
projectWordCloud
function returns a promise that:relativePaths
with the project
parameter, which returns a promise that resolves to a value words
.wordCount
with the words
value, which returns a promise that resolves to an array of word cloud data.d3CloudToSVG
with the word cloud data, which returns a promise that resolves to an SVG string.module.exports = projectWordCloud;
if(typeof $!== 'undefined') {
...
}
projectWordCloud
function is exported as a module.$
is defined, and if so, executes a block of code that:PROFILE_PATH
variable based on environment variables.project
variable based on the PROFILE_PATH
.projectWordCloud
with the project
variable, which returns a promise that is handled by $.async()
and $.sendError()
.