Created
June 29, 2023 15:22
-
-
Save aglove2189/a2f05d2bec71e08cdca69b0e93cf3009 to your computer and use it in GitHub Desktop.
Aaronson Oracle
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
from collections import defaultdict, deque | |
class Model: | |
def __init__(self): | |
self.counts = defaultdict(lambda: {"f": 0, "d": 0}) | |
self.last_six = deque(maxlen=6) | |
self.num_correct = 0 | |
self.num_predictions = 0 | |
def fit(self, x): | |
if x in ("f", "d"): | |
self.last_six.append(x) | |
def predict(self, x): | |
if len(self.last_six) >= 5: | |
fivegram = "".join(list(self.last_six)[:5]) | |
m = self.counts[fivegram] | |
prediction = "f" if not m or m["f"] > m["d"] else "d" | |
# update counts and accuracy | |
self.counts[fivegram][x[-1]] += 1 | |
self.num_predictions += 1 | |
self.num_correct += prediction == x | |
return prediction | |
def fit_predict(self, x): | |
self.fit(x) | |
return self.predict(x) | |
@property | |
def accuracy(self): | |
return round((self.num_correct / self.num_predictions) * 100) | |
def cli(): | |
from getch import getch | |
model = Model() | |
print("Start typing, only 'f' or 'd' are allowed") | |
while True: | |
char = getch() | |
prediction = model.fit_predict(char) | |
msg = f"observed {char}" | |
if prediction is not None: | |
msg += f" predicted {prediction}" | |
if model.num_predictions > 9: | |
msg += f" {model.accuracy}% accurate" | |
print(msg) | |
if __name__ == "__main__": | |
cli() |
Author
aglove2189
commented
Jun 29, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment