<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: <NAME> <<EMAIL>>

"""
This module contains code to convert document graphs to graphviz graphs
("dot files") and manipulate them.
"""

import codecs
from tempfile import NamedTemporaryFile
from networkx import write_dot


def print_dot(docgraph):
    """
    converts a document graph into a dot file and returns it as a string.

    If this function call is prepended by %dotstr,
    it will display the given document graph as a dot/graphviz graph
    in the currently running IPython notebook session.

    To use this function, the gvmagic IPython notebook extension
    needs to be installed once::

        %install_ext https://raw.github.com/cjdrake/ipython-magic/master/gvmagic.py

    In order to visualize dot graphs in your currently running
    IPython notebook, run this command once::

        %load_ext gvmagic
    """
    tmp_file = NamedTemporaryFile()
    write_dot(docgraph, tmp_file.name)
    # write_dot does not seem to produce valid utf8 for all files, that's
    # why we're adding error handling here<commit_msg>dot: added ignore_node_labels as parameter to print_dot()<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: <NAME> <<EMAIL>>

"""
This module contains code to convert document graphs to graphviz graphs
("dot files") and manipulate them.
"""

import codecs
from tempfile import NamedTemporaryFile
from networkx import write_dot


def print_dot(docgraph, ignore_node_labels=False):
    """
    converts a document graph into a dot file and returns it as a string.

    If this function call is prepended by %dotstr,
    it will display the given document graph as a dot/graphviz graph
    in the currently running IPython notebook session.

    To use this function, the gvmagic IPython notebook extension
    needs to be installed once::

        %install_ext https://raw.github.com/cjdrake/ipython-magic/master/gvmagic.py

    In order to visualize dot graphs in your currently running
    IPython notebook, run this command once::

        %load_ext gvmagic

    Parameters
    ----------
    ignore_node_labels : bool
        If True, use the ID of a node as its node label
    """
    tmp_file = NamedTemporaryFile()
    if ignore_node_labels:
        tmpgraph = docgraph.copy()
        for node, ndict in tmpgraph.nodes_iter(data=True):
            ndict.pop('label', None)
    else:
        tmpgraph = docgraph

    write_dot(tmpgraph, tmp_file.name)
    # write_dot does not seem to produce valid utf8 for all files, that's
    # why we're adding error handling here
{'fraction_non_alphanumeric': 0.07621247113163972, 'fraction_numerical': 0.001539645881447267, 'mean_word_length': 3.260655737704918, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '1769861', 'n_tokens_mistral': 765, 'n_tokens_neox': 730, 'n_words': 297}