<commit_before># Natural Language Toolkit: Dispersion Plots
#
# Copyright (C) 2001-2009 NLTK Project
# Author: <NAME> <<EMAIL>>
# URL: <http://www.nltk.org/>
# For license information, see LICENSE.TXT

"""
A utility for displaying lexical dispersion.
"""

def dispersion_plot(text, words):
    """
    Generate a lexical dispersion plot.

    @param text: The source text
    @type text: C{list} or C{enum} of C{str}
    @param words: The target words
    @type words: C{list} of C{str}
    """

    try:
        import pylab
    except ImportError:
        raise ValueError('The plot function requires the matplotlib package.'
                     'See http://matplotlib.sourceforge.net/')

    text = list(text)
    words.reverse()
    points = [(x,y) for x in range(len(text))
                    for y in range(len(words))
                    if text[x] == words[y]]
    x, y = zip(*points)
    pylab.plot(x, y, "b|", scalex=.1)
    pylab.yticks(range(len(words)), words, color="b")
    pylab.ylim(-1, len(words))
    pylab.title("Lexical Dispersion Plot")<commit_msg>Fixed issue 396, allowing empty dispersion plots<commit_after># Natural Language Toolkit: Dispersion Plots
#
# Copyright (C) 2001-2009 NLTK Project
# Author: <NAME> <<EMAIL>>
# URL: <http://www.nltk.org/>
# For license information, see LICENSE.TXT

"""
A utility for displaying lexical dispersion.
"""

def dispersion_plot(text, words):
    """
    Generate a lexical dispersion plot.

    @param text: The source text
    @type text: C{list} or C{enum} of C{str}
    @param words: The target words
    @type words: C{list} of C{str}
    """

    try:
        import pylab
    except ImportError:
        raise ValueError('The plot function requires the matplotlib package.'
                     'See http://matplotlib.sourceforge.net/')

    text = list(text)
    words.reverse()
    points = [(x,y) for x in range(len(text))
                    for y in range(len(words))
                    if text[x] == words[y]]
    if points:
        x, y = zip(*points)
    else:
        x = y = ()
    pylab.plot(x, y, "b|", scalex=.1)
    pylab.yticks(range(len(words)), words, color="b")
    pylab.ylim(-1, len(words))
    pylab.title("Lexical Dispersion Plot")
{'fraction_non_alphanumeric': 0.13605747642568478, 'fraction_numerical': 0.010327795240233499, 'mean_word_length': 2.658456486042693, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6165343', 'n_tokens_mistral': 771, 'n_tokens_neox': 719, 'n_words': 216}