-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimproved.py
More file actions
67 lines (58 loc) · 1.92 KB
/
improved.py
File metadata and controls
67 lines (58 loc) · 1.92 KB
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
import random
import string
import sys
import subprocess
import imghdr
import os
import json
import re
# my beautiful library
import img2ansi
# return a number of image filenames that we can asciify
def get_image_filenames(word, directory, quantity=3):
prefix = os.path.join(directory, word)
basenames = list(map(lambda x: x.name, os.scandir(prefix)))
ret = []
for bname in random.sample(basenames, quantity):
ret.append(os.path.join(prefix, bname))
return ret
# each folder represents an image type
def pick_subject(directory):
obj_names = [x.name for x in os.scandir(directory)]
return random.choice(obj_names)
# the version that uses my library
def colour_ascii(filename):
s = img2ansi.convert(filename, is_unicode=True, is_256=False, width=80)
return s
def levenshtein_distance(s1, s2):
if len(s1) > len(s2):
s1, s2 = s2, s1
distances = range(len(s1) + 1)
for i2, c2 in enumerate(s2):
distances_ = [i2+1]
for i1, c1 in enumerate(s1):
if c1 == c2:
distances_.append(distances[i1])
else:
distances_.append(1 + min((distances[i1], distances[i1 + 1], distances_[-1])))
distances = distances_
return distances[-1]
def correct_word(attempt, correct):
return levenshtein_distance(attempt, correct) <= 2
if __name__ == "__main__":
# try:
directory = "./example_objects"
word = pick_subject(directory)
filenames = get_image_filenames(word, directory)
for f in filenames:
print(colour_ascii(f))
guess = input("What is this object?\n> ")
if correct_word(guess, word):
print("correct!")
sys.exit(0)
print('incorrect...loading new image (of same object)')
print("bad robot! it was actually a:", word)
sys.exit(-1)
#except Exception as e:
# print(e)
# print(word)