1
2
3
4
5
6
7
8 """
9 A utility for displaying lexical dispersion.
10 """
11
12 from Tkinter import Canvas
13
14 -def plot(text, words, rowheight=15, rowwidth=800):
15 """
16 Generate a lexical dispersion plot.
17
18 @param text: The source text
19 @type text: C{list} or C{enum} of C{str}
20 @param words: The target words
21 @type words: C{list} of C{str}
22 @param rowheight: Pixel height of a row
23 @type rowheight: C{int}
24 @param rowwidth: Pixel width of a row
25 @type rowwidth: C{int}
26
27 """
28 canvas = Canvas(width=rowwidth, height=rowheight*len(words))
29 text = list(text)
30 scale = float(rowwidth)/len(text)
31 position = 0
32 for word in text:
33 for i in range(len(words)):
34 x = position * scale
35 if word == words[i]:
36 y = i * rowheight
37 canvas.create_line(x, y, x, y+rowheight-1)
38 position += 1
39 canvas.pack()
40 canvas.mainloop()
41
42 if __name__ == '__main__':
43 from nltk_lite.corpora import gutenberg
44 from nltk_lite.draw import dispersion
45 words = ['Elinor', 'Marianne', 'Edward', 'Willoughby']
46 dispersion.plot(gutenberg.raw('austen-sense'), words)
47