@JesseMalnik/

StudentBot

Python

A simple AI that generates text based on training text that you provide. Able to generate some silly stuff If you feed it text!

fork
loading
Files
  • main.py
  • input.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
## To teach student bot, go to input.txt 
## To get there, press the file icon to the left
## and Copy/Paste some text there

abs_val = lambda x: ((x**2)**0.5)
from random import randint
global user_input
global sep_dir
global raw_text
global all_unique
user_input = ""
sep_dir ={}
raw_text = ""
all_unique=[]

def init_get():
	global user_input
	global raw_text
	
	cache_instance=open("input.txt","r")
	user_input=cache_instance.read()
	raw_text = user_input
	cache_instance.close()
	user_input = user_input.lower()
	user_input = user_input.replace("\n"," ")
	user_input = user_input.replace("."," ")
	user_input = user_input.replace(","," ")
	user_input = user_input.replace("/"," ")
	user_input = user_input.replace('"'," ")
	user_input = user_input.replace('!'," ")
	user_input = user_input.replace(':'," ")
	user_input = user_input.replace(';'," ")
	user_input = user_input.replace('?'," ")
	user_input = user_input.replace("-"," ")
	user_input = user_input.replace("'","")
	user_input = user_input.replace('"',"")
	user_input = user_input.split()

def process_text():
	global all_unique
	global user_input
	global sep_dir
	while(user_input != user_input[len(user_input)-1:len(user_input)]):
		if not(user_input[0] in sep_dir):
			all_unique.append(user_input[0])
			sep_dir[user_input[0]] = {user_input[1]:1}
			user_input.pop(0)
		else:
			if(user_input[1] in sep_dir[user_input[0]]):
				sep_dir[user_input[0]][user_input[1]] += 1
				user_input.pop(0)
			else:
				sep_dir[user_input[0]][user_input[1]] = 1
				user_input.pop(0)
	if not(user_input[0] in sep_dir):
		sep_dir[user_input[0]]={all_unique[randint(0,len(all_unique)-1)]:1}

def generate_text(text_length=100):
	random_text = all_unique[randint(0,len(all_unique)-1)]
	string_final=[]
	string_final.append(random_text)
	for i in range(0, text_length):
		target_word = ''.join(string_final[len(string_final)-1:len(string_final)])
		probability_cache = sep_dir[target_word]
		probability_percents = []
		probability_occurances = []
		probability_results = []
		probability_accuracy = []
		total_occurences = 0
		random_percent = (randint(0,100)/100)
		previous_cache = 0
		for entry in probability_cache:
			probability_occurances.append(probability_cache[entry])
			probability_results.append(entry)
			total_occurences += probability_cache[entry]
		for occurance_number in probability_occurances:
			probability_percents.append(((occurance_number + previous_cache)/total_occurences))
			previous_cache += occurance_number
		for percent in probability_percents:
			probability_accuracy.append(abs_val(percent - random_percent))
		selected_word = probability_results[probability_accuracy.index(min(probability_accuracy))]
		string_final.append(selected_word)
	return ' '.join(string_final)


init_get()
process_text()
robot_text = generate_text(150)
print(robot_text)