Package nltk :: Package draw :: Module dispersion
[hide private]
[frames] | no frames]

Source Code for Module nltk.draw.dispersion

 1  # Natural Language Toolkit: Dispersion Plots 
 2  # 
 3  # Copyright (C) 2001-2008 NLTK Project 
 4  # Author: Steven Bird <[email protected]> 
 5  # URL: <http://nltk.org> 
 6  # For license information, see LICENSE.TXT 
 7   
 8  """ 
 9  A utility for displaying lexical dispersion. 
10  """ 
11   
12 -def dispersion_plot(text, words):
13 """ 14 Generate a lexical dispersion plot. 15 16 @param text: The source text 17 @type text: C{list} or C{enum} of C{str} 18 @param words: The target words 19 @type words: C{list} of C{str} 20 """ 21 22 try: 23 import pylab 24 except ImportError: 25 raise ValueError('The plot function requires the matplotlib package.' 26 'See http://matplotlib.sourceforge.net/') 27 28 text = list(text) 29 words.reverse() 30 points = [(x,y) for x in range(len(text)) 31 for y in range(len(words)) 32 if text[x] == words[y]] 33 x, y = zip(*points) 34 pylab.plot(x, y, "b|", scalex=.1) 35 pylab.yticks(range(len(words)), words, color="b") 36 pylab.ylim(-1, len(words)) 37 pylab.title("Lexical Dispersion Plot") 38 pylab.xlabel("Word Offset") 39 pylab.show()
40 41 if __name__ == '__main__': 42 from nltk.corpus import gutenberg 43 words = ['Elinor', 'Marianne', 'Edward', 'Willoughby'] 44 dispersion_plot(gutenberg.words('austen-sense.txt'), words) 45