Created
July 16, 2023 05:58
-
-
Save Ednaordinary/42c964f7e85aeda493fec6a93c98fad7 to your computer and use it in GitHub Desktop.
A simple learning algorithm for 20 questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
data = {} | |
questions = [ | |
"Is it alive?", | |
"Is it real?", | |
"Can it bend without breaking?", | |
"Does it move?", | |
"Does it roll?", | |
"Is it mechanical?", | |
"Does it have a memory?", | |
"Can you play games with it?", | |
"Is it a concept?", | |
"Does it weigh more than a duck?", | |
"Do you need batteries to use it?", | |
"Is it a mineral?", | |
"Is it bright?", | |
"Does it come in many varieties?", | |
"Can it float?", | |
"Does it have seeds?", | |
"Can you order it at a restaurant?", | |
"Would you give it as a gift?", | |
"Is it comforting?", | |
"Does it have writing on it?", | |
] | |
while data == {}: | |
thing = input("I don't have any data! Give me a thing>").lower() | |
if thing[:2] == "a ": thing = thing[2:] | |
if thing[:3] == "an ": thing = thing[3:] | |
currentdata = {} | |
questionnum = 0 | |
for i in questions: | |
while True: | |
ask = input(questions[questionnum] + " (y/n/?)>").lower() | |
if ask == "y": currentdata[questionnum] = 1; break | |
elif ask == "n": currentdata[questionnum] = 0; break | |
elif ask == "?": print("Skipping"); break | |
else: print("Please input 'y', 'n', or '?' to skip") | |
questionnum += 1 | |
if currentdata == {}: pass | |
else: data[thing] = [currentdata] | |
while True: | |
print("Lets play! Think of an object") | |
asked = 0 | |
currentdata = {} | |
questionsasked = [] | |
while asked < len(questions) and asked < 20: | |
while True: | |
questionnum = random.randint(0, len(questions) - 1) | |
if questionnum not in questionsasked: | |
questionsasked.append(questionnum) | |
break | |
while True: | |
ask = input(questions[questionnum] + " (y/n/?)>").lower() | |
if ask == "y": currentdata[questionnum] = 1; asked += 1; break | |
elif ask == "n": currentdata[questionnum] = 0; asked += 1; break | |
elif ask == "?": print("Skipping"); asked += 1; break | |
else: print("Please input 'y', 'n', or '?' to skip") | |
if currentdata == {}: print("I was unable to make a guess!") | |
else: | |
index = {} | |
for thing, answers in data.items(): | |
answerlists = {} | |
answeraverages = {} | |
for i in answers: | |
for x, y in i.items(): | |
try: | |
answerlists[x].append(y) | |
except: | |
answerlists[x] = [y] | |
#for i in range(len(questions) - 1): | |
# try: answerlists[i] | |
# except: answerlists[i] = [1, 0] | |
for x, y in answerlists.items(): | |
answeraverages[x] = sum(y)/len(y) | |
index[thing] = answeraverages | |
probabilities = {} | |
for thing, averages in index.items(): | |
probability = [] | |
for z, average in averages.items(): | |
try: | |
if round(average) == currentdata[z]: probability.append(1) | |
else: probability.append(0) | |
except: pass | |
probabilities[thing] = sum(probability)/len(probability) | |
guess = ["None", 0] | |
for thing, probability in probabilities.items(): | |
if probability > guess[1]: guess = [thing, probability] | |
print("I am " + str(round(guess[1] * 100)) + "% sure that it is " + str(guess[0])) | |
while True: | |
correct = input("Was I correct? (y/n)>") | |
if correct == "y": | |
data[guess[0]].append(currentdata) | |
print("Thanks for playing!") | |
break | |
elif correct == "n": | |
thing = input("What was it? ").lower() | |
if thing[:2] == "a ": thing = thing[2:] | |
if thing[:3] == "an ": thing = thing[3:] | |
try: | |
data[thing].append(currentdata) | |
except: | |
data[thing] = [currentdata] | |
print("Thanks for playing!") | |
break | |
else: print("Please input 'y' or 'n'") | |
again = None | |
while again != "y" and again != "n": | |
again = input("Play again? (y/n)>") | |
if again == "y": break | |
elif again == "n": break | |
else: print("Please input 'y' or 'n'") | |
if again == "n": break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment