Advertisement
here2share

# tk_pattern_function_indexify.py

Dec 24th, 2022
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. # tk_pattern_function_indexify.py
  2.  
  3. import math
  4. from tkinter import *
  5.  
  6. fn_code = """
  7. def create_fn(index):
  8.     # split the pattern into a list of strings
  9.     pattern_list = pattern.split(" ")
  10.  
  11.     # determine the length of the pattern
  12.     pattern_length = len(pattern_list)
  13.  
  14.     # create a function that will determine the value at a given index
  15.     def pattern_index_fn(index): # rename this function
  16. """
  17.  
  18. # create a flag variable to indicate whether the neural network should continue running or not
  19. go = True
  20.  
  21. # create a function to stop the neural network
  22. def stop_neural_network():
  23.     global go
  24.     go = False
  25.    
  26. def neural_network(pattern, index):
  27.     # split the pattern into a list of strings
  28.     pattern_list = pattern.split(" ")
  29.  
  30.     # determine the length of the pattern
  31.     pattern_length = len(pattern_list)
  32.  
  33.     # create a function that will determine the value at a given index
  34.     def fn(index):
  35.         # determine the remainder when the index is divided by the pattern length
  36.         remainder = index % pattern_length
  37.  
  38.         # return the value at the remainder index in the pattern list
  39.         return pattern_list[remainder]
  40.    
  41.     return fn
  42.  
  43. # create a function to train the neural network
  44. def train(pattern, index, expected_result):
  45.     # run the neural network with the given pattern and index
  46.     result = neural_network(pattern, index)
  47.  
  48.     # compare the result to the expected result
  49.     if result == expected_result:
  50.         # if the result is correct, return True
  51.         return True
  52.     else:
  53.         # if the result is incorrect, update the pattern and return False
  54.         pattern_list = pattern.split(" ")
  55.         pattern_list[index % len(pattern_list)] = expected_result
  56.         pattern = " ".join(pattern_list)
  57.         return False
  58.  
  59. # create a tkinter button that will run the neural network and train it if necessary when clicked
  60. def button_clicked():
  61.     global go
  62.     go = True
  63.    
  64.     # get the user input for the pattern
  65.     pattern = pattern_input.get()
  66.  
  67.     # get the user input for the index
  68.     index = index_input.get()
  69.  
  70.     # get the user input for the expected result
  71.     expected_result = expected_result_input.get()
  72.  
  73.     # run the neural network
  74.     while go:
  75.  
  76.         # run the neural network and get the result
  77.         result = neural_network(pattern, index)
  78.  
  79.         # check if the result is correct
  80.         if result == expected_result:
  81.             # if the result is correct, display it to the user
  82.             result_label.config(text=result)
  83.             go = False
  84.  
  85.             # print the fn_code string followed by the string representation of the fn function
  86.             print(fn_code + str(result))
  87.         else:
  88.             # if the result is incorrect, train the neural network and try again
  89.             training_success = train(pattern, index, expected_result)
  90.             if training_success:
  91.                 # if training was successful, display the correct result to the user
  92.                 result_label.config(text=expected_result)
  93.  
  94. # create the tkinter window and widgets
  95. window = Tk()
  96. window.title("tk_pattern_function_indexify")
  97.  
  98. pattern_label = Label(window, text="Enter a pattern:")
  99. pattern_input = Entry(window)
  100. index_label = Label(window, text="Enter an index:")
  101. index_input = Entry(window)
  102. expected_result_label = Label(window, text="Enter expected result:")
  103. expected_result_input = Entry(window)
  104. button = Button(window, text="Run Neural Network", command=button_clicked)
  105. result_label = Label(window, text="")
  106. cancel_button = Button(window, text="Cancel", command=stop_neural_network)
  107.  
  108. # add the widgets to the window
  109. pattern_label.pack()
  110. pattern_input.pack()
  111. index_label.pack()
  112. index_input.pack()
  113. expected_result_label.pack()
  114. expected_result_input.pack()
  115. button.pack()
  116. result_label.pack()
  117. cancel_button.pack()
  118.  
  119. window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement