Advertisement
furas

Python - Kivy - ScrollableLabel

May 17th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.55 KB | None | 0 0
  1. import kivy
  2. from kivy.app import App
  3. from kivy.uix.gridlayout import GridLayout
  4. from kivy.uix.label import Label
  5. from kivy.uix.textinput import TextInput
  6. from kivy.uix.button import Button
  7. from kivy.uix.boxlayout import BoxLayout
  8. from kivy.uix.screenmanager import ScreenManager, Screen
  9.  
  10. # --- data ---
  11.  
  12. 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',
  13.  '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',
  14.  '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',
  15.  '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,',
  16.  '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']
  17.  
  18. # --- classes ---
  19.  
  20.  
  21. from kivy.uix.scrollview import ScrollView
  22. from kivy.properties import StringProperty
  23. from kivy.lang import Builder
  24.  
  25. Builder.load_string('''
  26. <ScrollableLabel>:
  27.    Label:
  28.        size_hint_y: None
  29.        height: self.texture_size[1]
  30.        text_size: self.width, None
  31.        text: root.text
  32. ''')
  33.  
  34. class ScrollableLabel(ScrollView):
  35.     text = StringProperty('')
  36.  
  37. # --- classes ---
  38.  
  39. class LoginScreen(GridLayout):
  40.    
  41.     def __init__(self, **kwargs):
  42.         super(LoginScreen, self).__init__(**kwargs)
  43.        
  44.         self.cols = 2
  45.        
  46.         self.add_widget(Label(text='Livro1:'))
  47.         self.bookone = TextInput(multiline=False)
  48.         self.add_widget(self.bookone)
  49.  
  50.         self.add_widget(Label(text='Livro2:'))
  51.         self.booktwo = TextInput(multiline=False)
  52.         self.add_widget(self.booktwo)
  53.  
  54.  
  55.         self.add_widget(Button(text='Submit1', on_press=self.callback))
  56.         self.add_widget(Button(text='Submit2', on_press=self.callback))
  57.  
  58.        
  59.         self.debug_label = ScrollableLabel()
  60.         self.add_widget(self.debug_label)
  61.  
  62.         # --- class object ---
  63.        
  64.         self.total = 0
  65.  
  66.         self.livro1 = 'erro'
  67.         self.livro2 = 'batata.txt'
  68.        
  69.         self.dicionario = {}
  70.         self.dicionario2 = {}
  71.  
  72.     def callback(self, args):
  73.        
  74.         print('[DEBUG] args:', args)
  75.         print('[DEBUG] args.text:', args.text)
  76.        
  77.         if args.text == 'Submit1':
  78.             self.livro1 = self.bookone.text + '.txt'
  79.            
  80.             self.debug_label.text += 'livro1: {}\n'.format(self.livro1)
  81.  
  82.             self.palavras_repetidas()
  83.         elif args.text == 'Submit2':
  84.             self.livro1 = self.bookone.text + '.txt'
  85.             self.livro2 = self.booktwo.text + '.txt'
  86.  
  87.             self.debug_label.text += 'livro1: {}\n'.format(self.livro1)
  88.             self.debug_label.text += 'livro2: {}\n'.format(self.livro2)
  89.  
  90.             self.palavras_repetidas()
  91.             self.comparar()
  92.         else:
  93.             txt = '???'
  94.        
  95.        
  96.     def comparar(self):
  97.        
  98.         lista2 = open(self.livro2, 'r').read().split()
  99.        
  100.         for word in lista2:
  101.             if word in you_shall_not_pass:
  102.                 continue
  103.             if word in self.dicionario2:
  104.                 self.dicionario2[word] += 1
  105.             else:
  106.                 self.dicionario2[word] = 1
  107.      
  108.         #for key in dicionario.keys():
  109.         #   if key in dicionario2.keys():
  110.         #       if (dicionario[key]>10 and dicionario2[key]>10):
  111.         #           print(key, dicionario[key], dicionario2[key])######################################################
  112.  
  113.         common_keys = set(self.dicionario.keys()) & set(self.dicionario2.keys())
  114.         print(common_keys)
  115.        
  116.         for key in common_keys:
  117.             a = self.dicionario[key]
  118.             b = self.dicionario2[key]
  119.             if a > 10 and b > 10:
  120.                 #print(key, a, b)
  121.                 self.debug_label.text += '{}: {}, {}\n'.format(key, a, b)
  122.             self.debug_label.text += '{}: {}, {}\n'.format(key, a, b)
  123.            
  124.     def palavras_repetidas(self):
  125.        
  126.         lista = open(self.livro1, 'r').read().split()
  127.        
  128.         for word in lista:
  129.             if word in you_shall_not_pass:
  130.                 continue
  131.             if word in self.dicionario:
  132.                 self.dicionario[word]+=1
  133.             else:
  134.                 self.dicionario[word] = 1
  135.        
  136.         #sorted_dict = [(k,v) for v,k in sorted([(v,k) for k,v in dicionario.items()],reverse=True)]
  137.        
  138.         sorted_dict = sorted(self.dicionario.items(), reverse=True, key=lambda d:d[1])
  139.        
  140.         print(sorted_dict)
  141.        
  142.         #with open('teste.txt', 'w') as f:##############################################################################
  143.         #    for item in sorted_dict:
  144.         #        f.write(str(item))
  145.  
  146.         for item in sorted_dict:
  147.             self.debug_label.text += '{}: {}\n'.format(*item)
  148.            
  149.     def palavras_totais(self):
  150.  
  151.         f = open(self.livro1, 'r').read().split()
  152.  
  153.         for word in f:
  154.             self.total += 1
  155.    
  156.         self.debug_label.text += 'total: {}\n'.format(self.total)
  157.  
  158.  
  159. class MyApp(App):
  160.  
  161.     def build(self):
  162.         return LoginScreen()
  163.  
  164. MyApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement