<commit_before>/* A mix-in that adds DOM manipulations using only the w3c DOM API.
   Note: This module expects this.document to be a w3c legal Document object.
         The intent is that this module can be mixed in with any object with
         a valid this.document
 */
module.exports = {
    // Perform a depth first search, calling visit(currentNode, tree_depth)
    // for each node in the tree.
    depthFirstSearch : function (visit) {
        var stack = [[this.document, 0]]; // store [node, depth]
        // Do a depth first search.
        while (stack.length > 0) {
            var entry = stack.pop();
            var current = entry[0];
            var depth = entry[1];
            visit(current, depth);
            if (current.hasChildNodes()) {
                //Note: had to read backwards to match JSDOM node order
                for (var i = current.childNodes.length - 1; i >= 0; i--) {
                    stack.push([current.childNodes.item(i), depth + 1]);
                }
            }
        }
    },

    getNodes : function (name) {
        switch (name) {
            case 'dfs':
            default:
                traversal = this.depthFirstSearch;
        }
        if (typeof traversal != 'function') {
            throw new Error('Traversal is of wrong type: ' + typeof traversal);
        }
        var nodes = [];
        traversal.call(this, function (node) {
            nodes.push(node)
        });
        return nodes;
    },

    nodeTypeToString : function (nodeType) {
        // This way all instances share the same table.
        return nodeTypeToStringTable[nodeType];
    },

    // Debug function
    nodeStats : function () {
        var stats = [];
        var tags = {};
        for (var i = 0; i < nodeTypeToStringTable.length; i++) {
            stats[i] = 0;
        }<commit_msg>Added printNodes() debug function to domutils.js.<commit_after>/* A mix-in that adds DOM manipulations using only the w3c DOM API.
   Note: This module expects this.document to be a w3c legal Document object.
         The intent is that this module can be mixed in with any object with
         a valid this.document
 */
module.exports = {
    // Perform a depth first search, calling visit(currentNode, tree_depth)
    // for each node in the tree.
    depthFirstSearch : function (visit) {
        if (this.document == undefined) {
            throw('tried to search an undefined document');
        }
        var stack = [[this.document, 0]]; // store [node, depth]
        // Do a depth first search.
        while (stack.length > 0) {
            var entry = stack.pop();
            var current = entry[0];
            var depth = entry[1];
            visit(current, depth);
            if (current.hasChildNodes()) {
                //Note: had to read backwards to match JSDOM node order
                for (var i = current.childNodes.length - 1; i >= 0; i--) {
                    stack.push([current.childNodes.item(i), depth + 1]);
                }
            }
        }
    },

    getNodes : function (name) {
        switch (name) {
            case 'dfs':
            default:
                traversal = this.depthFirstSearch;
        }
        if (typeof traversal != 'function') {
            throw new Error('Traversal is of wrong type: ' + typeof traversal);
        }
        var nodes = [];
        traversal.call(this, function (node) {
            nodes.push(node)
        });
        return nodes;
    },

    printNodes : function () {
        var that = this;
        this.depthFirstSearch(function (node) {
            var line = that.nodeTypeToString(node.nodeType);
            if (node.nodeType == 1) { // ELEMENT NODE
                line += ' - ' + node.tagName;
            } else if (node.nodeType == 3) { // TEXT NODE
                line += ' - ' + node.data;
            }
            console.log(line);
        });
    },

    nodeTypeToString : function (nodeType) {
        // This way all instances share the same table.
        return nodeTypeToStringTable[nodeType];
    },

    // Debug function
    nodeStats : function () {
        var stats = [];
        var tags = {};
        for (var i = 0; i < nodeTypeToStringTable.length; i++) {
            stats[i] = 0;
        }
{'fraction_non_alphanumeric': 0.10967893133348958, 'fraction_numerical': 0.005624560581204594, 'mean_word_length': 1.6280788177339902, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '179104', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1127, 'n_words': 380}