Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #coding=latin-1
- from __future__ import print_function # use print() in Python 2
- try:
- import Tkinter as tk # Python 2
- except:
- import tkinter as tk # Python 3
- import codecs # to open() with encoding="latin-1" in Python 2
- class Principal():
- def __init__(self):
- self.you_shall_not_pass = ['de', 'o','a', 'do', 'da', 'em', 'e', 'que', 'ser', 'um', 'uns', 'uma', 'umas', 'por','para', 'nao', 'com', 'se', 'ter', 'seu', 'sua', 'seus', 'suas','como',
- 'estar','sim', '-', 'os', 'as', 'no', 'ao', 'na', '.', 'mas', 'era', 'lhe', 'ele', 'mais','dos', 'ela', 'ele', 'muito','ja','eu','sobre','das','tinha','quando','sem','la','num',
- 'depois','entao','nos','esta','ainda','numa','onde','foi','tao','estava','entre','dum','so','grande','disse','todo','pela','agora','pelo','me','toda', 'havia', 'tudo', 'ou','ate',
- 'ha','nem','logo','ia','sempre','duas','tambem','mesmo','ali','aos','tem','fora','aquele','tu','meu','duma','aquela','e,','outra','essa','outro','isto','esse','assim','quem','ele,',
- 'apenas','todos','pouco','nas','diante','ai','sob','tinham','coisas','aqui','pois','porque','ir','te','nunca','depois','lado','tres','mao','vez','sim,','desde','isso','fazer','este']
- self.livro1 = 'erro'
- self.livro2 = 'batata.txt'
- self.dicionario1 = {}
- self.dicionario2 = {}
- def read_dictionario(self, filename):
- result = {}
- lista = codecs.open(filename, 'r', encoding='latin-1').read().split()
- for word in lista:
- if word in self.you_shall_not_pass:
- continue
- if word in result:
- result[word] += 1
- else:
- result[word] = 1
- return result
- def palavras_repetidas(self):
- self.dicionario1 = self.read_dictionario(self.livro1)
- sorted_dict = [(k,v) for v,k in sorted([(v,k) for k,v in self.dicionario1.items()], reverse=True)]
- with open('teste.txt', 'w') as f:
- for item in sorted_dict:
- f.write(str(item))
- print(self.dicionario1)
- return self.dicionario1
- def palavras_totais(self, filename):
- f = open(filename, 'r').read().split()
- total = 0
- for word in f:
- total += 1
- #total = len(f)
- print(total)
- return total
- def comparar(self):
- self.dicionario2 = self.read_dictionario(self.livro2)
- print(self.dicionario2)
- result = []
- limit = 7
- for key in self.dicionario1:
- if key in self.dicionario2:
- if (self.dicionario1[key] > limit and self.dicionario2[key] > limit):
- txt = u'{}: {}, {}'.format(key, self.dicionario1[key], self.dicionario2[key])
- result.append(txt)
- return result
- class MainWindow():
- def __init__(self):
- self.master = tk.Tk()
- self.master.title('App')
- self.e1 = tk.Entry(self.master)
- self.e1.pack()
- self.e2 = tk.Entry(self.master)
- self.e2.pack()
- self.b1 = tk.Button(self.master, text='Submeter', command=self.submeter)
- self.b1.pack()
- self.b2 = tk.Button(self.master, text='Comparar', command=self.comparar)
- self.b2.pack()
- # ------------------------------------------------------------
- self.frame1 = tk.Frame(self.master)
- self.frame1.pack()
- self.scrollbar1 = tk.Scrollbar(self.frame1)
- self.scrollbar1.pack(side='right', expand=True, fill='y')
- self.txt1 = tk.Text(self.frame1, yscrollcommand=self.scrollbar1.set)
- self.txt1.pack(side='left')
- self.scrollbar1.config(command=self.txt1.yview)
- self.clear1 = tk.Button(self.master, text='Clear', command=self.clear1)
- self.clear1.pack()
- # ------------------------------------------------------------
- self.frame2 = tk.Frame(self.master)
- self.frame2.pack()
- self.scrollbar2 = tk.Scrollbar(self.frame2)
- self.scrollbar2.pack(side='right', expand=True, fill='y')
- self.txt2 = tk.Text(self.frame2, yscrollcommand=self.scrollbar2.set)
- self.txt2.pack(side='left')
- self.scrollbar2.config(command=self.txt2.yview)
- self.clear2 = tk.Button(self.master, text='Clear', command=self.clear2)
- self.clear2.pack()
- # ------------------------------------------------------------
- #self.master.minsize(width=666, height=666)
- #self.master.maxsize(width=666, height=666)
- self.principal = Principal()
- def run(self):
- self.master.mainloop()
- def clear1(self):
- self.txt1.delete(1.0, 'end')
- def clear2(self):
- self.txt2.delete(1.0, 'end')
- def submeter(self):
- self.principal.livro1 = self.e1.get() +'.txt'
- result = self.principal.palavras_repetidas()
- for line in result.items():
- self.txt1.insert('end', line)
- self.txt1.insert('end', "\n")
- def comparar(self):
- self.principal.livro1 = self.e1.get() +'.txt'
- self.principal.livro2 = self.e2.get() +'.txt'
- result = self.principal.comparar()
- for line in result:
- self.txt2.insert('end', line)
- self.txt2.insert('end', "\n")
- # ---------------------------------------------------------------------
- MainWindow().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement