Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_pattern_function_indexify.py
- import math
- from tkinter import *
- fn_code = """
- def create_fn(index):
- # split the pattern into a list of strings
- pattern_list = pattern.split(" ")
- # determine the length of the pattern
- pattern_length = len(pattern_list)
- # create a function that will determine the value at a given index
- def pattern_index_fn(index): # rename this function
- """
- # create a flag variable to indicate whether the neural network should continue running or not
- go = True
- # create a function to stop the neural network
- def stop_neural_network():
- global go
- go = False
- def neural_network(pattern, index):
- # split the pattern into a list of strings
- pattern_list = pattern.split(" ")
- # determine the length of the pattern
- pattern_length = len(pattern_list)
- # create a function that will determine the value at a given index
- def fn(index):
- # determine the remainder when the index is divided by the pattern length
- remainder = index % pattern_length
- # return the value at the remainder index in the pattern list
- return pattern_list[remainder]
- return fn
- # create a function to train the neural network
- def train(pattern, index, expected_result):
- # run the neural network with the given pattern and index
- result = neural_network(pattern, index)
- # compare the result to the expected result
- if result == expected_result:
- # if the result is correct, return True
- return True
- else:
- # if the result is incorrect, update the pattern and return False
- pattern_list = pattern.split(" ")
- pattern_list[index % len(pattern_list)] = expected_result
- pattern = " ".join(pattern_list)
- return False
- # create a tkinter button that will run the neural network and train it if necessary when clicked
- def button_clicked():
- global go
- go = True
- # get the user input for the pattern
- pattern = pattern_input.get()
- # get the user input for the index
- index = index_input.get()
- # get the user input for the expected result
- expected_result = expected_result_input.get()
- # run the neural network
- while go:
- # run the neural network and get the result
- result = neural_network(pattern, index)
- # check if the result is correct
- if result == expected_result:
- # if the result is correct, display it to the user
- result_label.config(text=result)
- go = False
- # print the fn_code string followed by the string representation of the fn function
- print(fn_code + str(result))
- else:
- # if the result is incorrect, train the neural network and try again
- training_success = train(pattern, index, expected_result)
- if training_success:
- # if training was successful, display the correct result to the user
- result_label.config(text=expected_result)
- # create the tkinter window and widgets
- window = Tk()
- window.title("tk_pattern_function_indexify")
- pattern_label = Label(window, text="Enter a pattern:")
- pattern_input = Entry(window)
- index_label = Label(window, text="Enter an index:")
- index_input = Entry(window)
- expected_result_label = Label(window, text="Enter expected result:")
- expected_result_input = Entry(window)
- button = Button(window, text="Run Neural Network", command=button_clicked)
- result_label = Label(window, text="")
- cancel_button = Button(window, text="Cancel", command=stop_neural_network)
- # add the widgets to the window
- pattern_label.pack()
- pattern_input.pack()
- index_label.pack()
- index_input.pack()
- expected_result_label.pack()
- expected_result_input.pack()
- button.pack()
- result_label.pack()
- cancel_button.pack()
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement