files | Cell 0 | project imports d3 tree | Search

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.

Run example

npm run import -- "project word-cloud"

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));
}

What the code could have been:

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));
}

Code Breakdown

Requires and Function Definition

var importer = require('../Core');
var path = require('path');
    
function projectWordCloud(project) {
   ...
}

Module Imports

var d3CloudToSVG = importer.import('d3.ipynb[create word-cloud]');
var relativePaths = importer.import('relative paths and includes', {project});

Word Count Function

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]}));
};

Project Word Cloud Function

return relativePaths(project)
       .then(words => d3CloudToSVG(wordCount(words)));

Module Exports and Execution

module.exports = projectWordCloud;

if(typeof $!== 'undefined') {
   ...
}