-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipfexplorer.py
329 lines (222 loc) · 10 KB
/
zipfexplorer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from collections import defaultdict
from collections import OrderedDict
import random
from nltk.tokenize import RegexpTokenizer
import powerlaw
from scipy import stats
from scipy.stats import kstest
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import argparse
import sys
import os
import glob
import csv
from Tkinter import *
import tkFileDialog as tkf
import tkSimpleDialog as tksd
import tkMessageBox
import ttk
class mainWindow(Tk):
def __init__(self):
Tk.__init__(self)
self.title("Zipf Explorer")
self.option_add('*tearOff', FALSE) #screw you tear off menus FUCK YOU
ico = PhotoImage(file='icon.gif')
self.tk.call('wm', 'iconphoto', self._w, ico)
self.tabMan = tabManager(self)
self.tokenLimit = 0
menu = Menu(self)
self.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="Open...", underline = 0, accelerator="Ctrl+O", command=lambda: self.openText(None))
filemenu.add_command(label="Limit Token Amount...", command = self.limitDialogueCallBack)
filemenu.add_command(label="Save Single Result As...", underline = 0, accelerator="Ctrl+S", command = lambda: self.saveSingleResult(None))
filemenu.add_separator()
filemenu.add_command(label="Close Tab", underline = 0, accelerator="Ctrl+C", command=lambda: self.closeTabCallBack(None))
filemenu.add_command(label="Quit", underline = 0, accelerator="Ctrl+Q", command= lambda: self.exitCallBack(None))
self.bind_all("<Control-q>", self.exitCallBack)
self.bind_all("<Control-o>", self.openText)
self.bind_all("<Control-s>", self.saveSingleResult)
self.bind_all("<Control-c>", self.closeTabCallBack)
processmenu = Menu(menu)
menu.add_cascade(label = "Process", menu=processmenu)
processmenu.add_command(label = "Frequency Table", underline = 9, accelerator="Ctrl+T", command = lambda: self.freqTableCallBack(None)) #so this should make tabs happen OK?
processmenu.add_command(label = "Plot", underline = 0, accelerator="Ctrl+P", command = lambda: self.plotCallBack(None))
processmenu.add_command(label = "Fit values", underline = 0, accelerator="Ctrl+F", command = lambda: self.fitValuesCallBack(None))
self.bind_all("<Control-t>", self.freqTableCallBack)
self.bind_all("<Control-p>", self.plotCallBack)
self.bind_all("<Control-f>", self.fitValuesCallBack)
batchmenu = Menu(menu)
menu.add_cascade(label = "Batch", menu=batchmenu)
batchmenu.add_command(label = "Generate Report", underline = 10, accelerator="Ctrl+R", command = lambda: self.reportCallBack(None))
self.bind_all("<Control-r>", self.reportCallBack)
helpmenu = Menu(menu)
menu.add_cascade(label="Help", menu=helpmenu)
helpmenu.add_command(label="About...", command=self.aboutBox)
def aboutBox(self):
tkMessageBox.showinfo("About", "Zipf Explorer | cmessner.com")
def exitCallBack(self, event):
self.destroy()
sys.exit(0)
####save functions###
def limitDialogueCallBack(self):
self.tokenLimit = tksd.askinteger("Token Limit?", "Limit texts to the first (0 means no limit):", initialvalue=self.tokenLimit)
def reportCallBack(self, event):
self.tabMan.saveReport()
def saveSingleResult(self, event):
self.tabMan.saveSingle(self.tabMan.tab(self.tabMan.select(), "text"))
### these all pass the name of the currently focused on tab to the manager
def freqTableCallBack(self, event):
self.tabMan.dispTable(self.tabMan.tab(self.tabMan.select(), "text")) #the freqtable menu item passes the current tab to the tab manager
def fitValuesCallBack(self, event):
self.tabMan.dispFit(self.tabMan.tab(self.tabMan.select(), "text")) #the freqtable menu item passes the current tab to the tab manager
def plotCallBack(self, event):
self.tabMan.dispPlot(self.tabMan.tab(self.tabMan.select(), "text"))
def closeTabCallBack(self, event):
self.tabMan.closeTab(self.tabMan.tab(self.tabMan.select(), "text"))
#####
def openText(self, event):
#totalText = []
outRow = []
tokenizer = RegexpTokenizer("[\w']+") #improve -- taking 'a' as a diff token from a but needs to retain contraction support
#multiple files
try:
files = tkf.askopenfilenames()
except IOError:
print "Cancel/File not Found"
return
files = root.splitlist(files)
for infile in files:
frequencies = defaultdict(int)
with open(infile, 'r') as fileName:
for line in fileName:
for word in tokenizer.tokenize(line):
word = unicode(word, 'ascii', 'ignore')
if word and word[0] == '\'' and word[:1] == '\'': # a stupid crutch to remove the overinclusive end ''s left by tokenizer
word = word[1:-1]
if word: #no blanks!
if self.tokenLimit == 0 or sum(frequencies.values()) < self.tokenLimit: #if there is no token limit or we are below the specified token limit
frequencies[word.lower()] += 1
#totalText.append(word.lower()) #maintain words in order for monkey things
#outRow.append(fileName.name.split('/')[-1]) #for the actual display just save the .txt filename, not the whole path
fileName.close()
orderedKeys = sorted(frequencies, key = frequencies.get, reverse = True)
orderedFreq = OrderedDict(zip(orderedKeys, [frequencies[x] for x in orderedKeys])) #keys, frequencies of words by descending frequency
self.tabMan.addText(fileName.name.split('/')[-1], orderedFreq)
class tabManager(ttk.Notebook): #checking that we have only one tab open for each file here, etc.
def __init__(self, parent):
ttk.Notebook.__init__(self, parent, width=640, height = 480)
self.pack(fill='both', expand=1)
self.tabDict = {}
self.enable_traversal()
def addText(self, name, freq): #load a text from file into the dictionary
self.tabDict[name] = tabView(self, name, freq)
self.tabDict[name].freqTabView()
for openTab in self.tabs(): #these three lines just make it so that focus automatically switches to a newly-opened tab
if self.tab(openTab, "text") == name:
self.select(openTab)
def dispTable(self, name):
if name in self.tabDict:
self.tabDict[name].freqTabView()
def dispFit(self, name):
if name in self.tabDict:
self.tabDict[name].fitDataView()
def dispPlot(self, name):
if name in self.tabDict:
self.tabDict[name].plotView()
def closeTab(self, name): #remove from dictionary and forget tab
if name in self.tabDict:
self.tabDict[name].destroy()
self.tabDict.pop(name)
def saveReport(self):
try:
with tkf.asksaveasfile(mode='w', defaultextension=".csv") as of:
for key in self.tabDict:
of.write(','.join(self.tabDict[key].fitDataView()) + '\n') #process every open tab
of.close()
except AttributeError:
print "Cancel/File not Found"
return
def saveSingle(self, name):
if name in self.tabDict:
with tkf.asksaveasfile(mode='w', defaultextension=".csv") as of:
for item in self.tabDict[name].orderedFreq:
of.write(item + "," + str(self.tabDict[name].orderedFreq[item]) +'\n')
class tabView(Frame):
def __init__(self, parent, nameText, orderedFreq):
Frame.__init__(self, parent)
parent.add(self, text=nameText)
self.outRow = [nameText]
self.orderedFreq = orderedFreq
self.outRowHeadings = ["Text", "Total Tokens", "Alpha", "vs. Exponential Liklihood", "vs. Exponential Pvalue", "vs. Lognormal Liklihood", "vs. Lognormal Pvalue"]
def freqTabView(self):
self.clearView()
textDisplay = Canvas(self, width=200, height=480)
textDisplay.pack(fill=BOTH, expand=YES, side = LEFT)
vbar=Scrollbar(self, orient=VERTICAL) #TODO, mouse wheel
vbar.pack(side=RIGHT, fill = Y, expand = FALSE)
vbar.config(command=textDisplay.yview)
y = 12
for key, value in self.orderedFreq.items():
textDisplay.create_text(10,y, text = key, justify = "center", anchor = "nw") #lines bewtween rows?
textDisplay.create_text(140,y, text = value, justify = "center", anchor = "nw")
textDisplay.create_line(0, y+14, textDisplay.winfo_reqwidth(), y+14)
y+=22
textDisplay.config(yscrollcommand=vbar.set, scrollregion = textDisplay.bbox("all"))
return self.orderedFreq
def fitDataView(self):
self.clearView()
total = sum(self.orderedFreq.values())
self.outRow.append(str(total)) #total amount of words
results = powerlaw.Fit(self.orderedFreq.values(), discrete=True) #fit to powerlaw distribution
self.outRow.append(str(results.power_law.alpha)) #alpha
R, p = results.distribution_compare('power_law', 'exponential', normalized_ratio=True)
#print "Liklihood for power instead of exponential, P-value"
#print R, p
self.outRow.append(str(R))
self.outRow.append(str(p))
R, p = results.distribution_compare('power_law', 'lognormal', normalized_ratio=True)
#print "Liklihood for power instead of lognormal, P-value"
#print R, p
self.outRow.append(str(R))
self.outRow.append(str(p))
textDisplay = Canvas(self, width = 800, height = 100)
textDisplay.pack(fill=BOTH, expand=YES)
x = 0
for heading in self.outRowHeadings:
textDisplay.create_text(10+x, 10, text=heading, anchor="nw")
x += 200
textDisplay.create_line(0, 25, 200*len(self.outRowHeadings)+10, 25)
x = 0
for info in self.outRow:
textDisplay.create_text(10+x, 35, text = info, anchor = "nw")
x+=200
hbar=Scrollbar(self,orient=HORIZONTAL)
hbar.pack(side=BOTTOM, fill = Y, expand = FALSE)
hbar.config(command=textDisplay.xview)
textDisplay.config(xscrollcommand=hbar.set, scrollregion = textDisplay.bbox("all"))
return self.outRow
def plotView(self):
self.clearView()
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
test = powerlaw.plot_ccdf(self.orderedFreq.values(), ax = a, color = 'b')
a.plot()
canvas = FigureCanvasTkAgg(f, master=self)
canvas.show()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg( canvas, self )
toolbar.update()
canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=1)
def clearView(self):
for child in self.winfo_children():
child.destroy()
if __name__ == "__main__":
root = mainWindow()
root.mainloop()