Last active
September 2, 2015 17:18
-
-
Save henriquebastos/2ec5a2f618e6841c6bf4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python -tt | |
# coding: utf-8 | |
import argparse | |
from collections import Counter | |
import tkinter as tk | |
from tkinter.filedialog import askopenfile | |
from tkinter.scrolledtext import ScrolledText | |
def load(file): | |
"""Lê o arquivo""" | |
return file.read() | |
def count(content): | |
"""Conta as palavras""" | |
return Counter(content.lower().split()) | |
def asc(words): | |
"""Ordena por ordem alfabética""" | |
return sorted(words.items()) | |
def top(words, qty=20): | |
"""Ordena os qty mais frequentes""" | |
return words.most_common(qty) | |
def lines(words): | |
"""Retorna uma string com as palavras e suas frequencias""" | |
entry = '%2d) %s %d' | |
lines_ = [entry % (i, w, c) for i, (w, c) in enumerate(words, start=1)] | |
return '\n'.join(lines_) | |
def count_words(file_): | |
return asc(count(load(file_))) | |
def top_words(file_, qty=20): | |
return top(count(load(file_)), qty) | |
# This basic command line argument parsing code is provided and | |
# calls the print_words() and print_top() functions which you must define. | |
def main(): | |
parser = argparse.ArgumentParser(description='Conta palavras.') | |
parser.add_argument('--count', action='store_true') | |
parser.add_argument('--topcount', type=int) | |
parser.add_argument('textfile', type=argparse.FileType()) | |
options = parser.parse_args() | |
if options.count: | |
words = count_words(options.textfile) | |
print(lines(words)) | |
elif options.topcount: | |
words = top_words(options.textfile, options.topcount) | |
print(lines(words)) | |
class SmartScrolledText(ScrolledText): | |
@property | |
def text(self): | |
return self.get(1.0, tk.END) | |
@text.setter | |
def text(self, value): | |
self.delete(1.0, tk.END) | |
self.insert(tk.END, value) | |
class Application(tk.Frame): | |
COUNT = 0 | |
TOPCOUNT = 1 | |
MODES = { | |
COUNT: asc, | |
TOPCOUNT: top, | |
} | |
def __init__(self, master): | |
super().__init__(master=master) | |
self._words = '' | |
self.text_area = None | |
self.option = tk.IntVar() | |
self.create_widget() | |
def on_click(self): | |
with askopenfile() as f: | |
self._words = count(f.read()) | |
self.output_words() | |
def output_words(self): | |
value = self.option.get() | |
cmd = self.MODES[value] | |
self.text_area.text = lines(cmd(self._words)) | |
def create_widget(self): | |
lb1 = tk.Label(text='Choose your file.') | |
lb1.grid(row=0, column=0, sticky=tk.W) | |
btn1 = tk.Button(text='Open...', command=self.on_click) | |
btn1.grid(row=0, column=1, sticky=tk.E) | |
rb1 = tk.Radiobutton(text='Count all', variable=self.option, value=self.COUNT, command=self.output_words) | |
rb1.grid(row=1, column=0, sticky=tk.W) | |
rb2 = tk.Radiobutton(text='Top count', variable=self.option, value=self.TOPCOUNT, command=self.output_words) | |
rb2.grid(row=2, column=0, sticky=tk.W) | |
st1 = SmartScrolledText() | |
st1.grid(row=3, column=0, columnspan=2) | |
self.text_area = st1 | |
def main(): | |
root = tk.Tk() | |
app = Application(root) | |
app.mainloop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment