Advertisement
here2share

# tk_learnique.py

Jun 21st, 2024 (edited)
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 KB | None | 0 0
  1. # tk_learnique.py
  2.  
  3. import tkinter as tk
  4. import random
  5. import string
  6. import math
  7. import collections
  8.  
  9. ww = 1200
  10. hh = 600
  11.  
  12. root = tk.Tk()
  13. root.title("# tk_learnique.py")
  14. root.geometry(f"{ww}x{hh}+10+10")
  15.  
  16. scrollbar = tk.Scrollbar(root)
  17. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  18.  
  19. canvas = tk.Canvas(root, width=ww, height=hh, bg="black", yscrollcommand=scrollbar.set)
  20. canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  21.  
  22. scrollbar.config(command=canvas.yview)
  23.  
  24. len_nodes = 300
  25.  
  26. def get_nodes(data):
  27.     node = []
  28.     for idx1, s1 in enumerate(data):
  29.         for idx2, s2 in enumerate(data[idx1+1:]):
  30.             s = f"{s1}:{idx1}:{s2}:{idx1+idx2}"
  31.             node += [s]
  32.     # print(len(node)) # gets len_nodes
  33.     return node
  34.  
  35. def sum_weights(nodes):
  36.     w = {}
  37.     node_sums = set()
  38.     for category in range(1, 6):
  39.         w[category] = 0
  40.         for node in nodes:
  41.             s1, idx1, s2, idx2 = node.split(':')
  42.             try:
  43.                 t = weights[category][node]
  44.                 w[category] += t
  45.             except:
  46.                 weights[category][node] = 0
  47.                 t = 0
  48.             node_sums.add((s1, idx1))
  49.     w = [(w[cat], cat) for cat in w]
  50.     w.sort(reverse=True)
  51.     return w, node_sums
  52.  
  53. def gym():
  54.     if prediction == -1:
  55.         for node in nodes:
  56.             weights[correct_category][node] = weights[correct_category][node] + 100
  57.     else:
  58.         for node in nodes:
  59.             weights[correct_category][node] = min(99, weights[prediction][node] + 1)
  60.  
  61. def predict():
  62.     nodes = get_nodes(data)
  63.     sorted_sums, node_sums = sum_weights(nodes)
  64.     prediction = sorted_sums[0][1]
  65.     return prediction, sorted_sums, node_sums, nodes
  66.    
  67. def update_canvas(i):
  68.     cell_width = 20
  69.     cell_height = 20
  70.     num_cols = 25
  71.     for char, j in node_sums:
  72.         k = str_idx[char, j, correct_category]
  73.         str_idx[char, j, correct_category] += 50
  74.         color_value = get_color[min(999, k)] # ZZZ
  75.         x0 = int(j) * cell_width
  76.         y0 = i * cell_height
  77.         x1 = x0 + cell_width
  78.         y1 = y0 + cell_height
  79.         canvas.create_rectangle(x0, y0, x1, y1, fill=color_value, outline="gray")
  80.         canvas.create_text(x0 + 10, y0 + 10, text=char, fill="white")
  81.  
  82.     result_text = f"Y [{correct_category}] " if is_correct else f"N [{correct_category}] "
  83.     progress.append(1 if is_correct else 0)
  84.     progress[:] = progress[-100:]
  85.     gain = sum(progress)
  86.     gain_text = f"{gain}% gain"
  87.     canvas.create_text(num_cols * cell_width + 30, i * cell_height + 10, text=result_text, fill="white", anchor=tk.W)
  88.     canvas.create_text(num_cols * cell_width + 80, i * cell_height + 10, text=gain_text, fill="white", anchor=tk.W)
  89.     k = 0
  90.     for wt, idx in sorted_sums:
  91.         canvas.create_text(650 + (60 * k), i * cell_height + 10, text=f"{idx}: {int(wt)}", fill="white", anchor=tk.W)
  92.         k += 1
  93.     canvas.create_text(650 + (60 * k), i * cell_height + 10, text=line+1, fill="white", anchor=tk.W)
  94.  
  95.     canvas.config(scrollregion=(0, 0, ww, (i + 1) * cell_height + cell_height))
  96.  
  97.     root.update()
  98.  
  99. progress = []
  100. weights = {i+1: collections.defaultdict(int) for i in range(5)}
  101.  
  102. j = 256 / 400
  103. a = [int(i * j) for i in range(400)]
  104. b = [0 for _ in range(300)]
  105. red = a[::-1] + b + b
  106. green = b + b + a
  107. blue = b + a + b
  108. get_color = [f'#{r:02X}{g:02X}{b:02X}' for r, g, b in zip(red, green, blue)]
  109.  
  110. def shuffle_mixed_case(s):
  111.     mixed = [c.lower() if random.random() < 0.5 else c.upper() for c in s]
  112.     random.shuffle(mixed)
  113.     return ''.join(mixed)
  114.  
  115. def category_1():
  116.     digits = ''.join(random.choices(string.digits, k=7))
  117.     combined = digits + 'yellow'
  118.     combined += ''.join(random.choices(string.ascii_letters, k=(25 - len(combined))))
  119.     return shuffle_mixed_case(combined)
  120.  
  121. def category_2():
  122.     digits = ''.join(random.choices(string.digits, k=9))
  123.     combined = digits + 'helloworld'
  124.     combined += ''.join(random.choices(string.ascii_letters, k=(25 - len(combined))))
  125.     return shuffle_mixed_case(combined)
  126.  
  127. def category_3():
  128.     combined = 'python3407'
  129.     combined += ''.join(random.choices(string.ascii_letters + string.digits, k=(25 - len(combined))))
  130.     return shuffle_mixed_case(combined)
  131.  
  132. def category_4():
  133.     combined = 'thankyou12345'
  134.     combined += ''.join(random.choices(string.ascii_letters + string.digits, k=(25 - len(combined))))
  135.     return shuffle_mixed_case(combined)
  136.  
  137. def category_5():
  138.     s = []
  139.     for i in range(12):
  140.         s.append(random.choice(string.ascii_letters))
  141.         s.append(random.choice(string.digits))
  142.     s.append(random.choice(string.ascii_letters))
  143.     return ''.join(s)
  144.  
  145. category_functions = {
  146.     1: category_1,
  147.     2: category_2,
  148.     3: category_3,
  149.     4: category_4,
  150.     5: category_5
  151. }
  152.  
  153. def generate_training_data(category):
  154.     return category_functions[category]()
  155.  
  156. def toggle_auto_scroll(event):
  157.     global auto_scroll
  158.     auto_scroll = not auto_scroll
  159. root.bind("<space>", toggle_auto_scroll)
  160.  
  161. number_of_data = 5000
  162. categories = [1, 2, 3, 4, 5] * (number_of_data // 5)
  163. random.shuffle(categories)
  164.  
  165. training_data = [(generate_training_data(category), category) for category in categories]
  166. str_idx = {}
  167. for cat in range(1,6):
  168.     for s in string.ascii_letters + string.digits:
  169.         for idx in range(25):
  170.             str_idx[s, str(idx), cat] = 0
  171. line = 0
  172. auto_scroll = True
  173.  
  174. while 1:
  175.     data, correct_category = training_data[line % number_of_data]
  176.     prediction, sorted_sums, node_sums, nodes = predict()
  177.     is_correct = prediction == correct_category
  178.     if not sorted_sums[0][0]:
  179.         prediction = -1
  180.     if not is_correct:
  181.         gym()
  182.     update_canvas(line)
  183.     if auto_scroll:
  184.         canvas.yview_moveto(1.0)
  185.     line += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement