-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsker.py
117 lines (89 loc) · 3.1 KB
/
Asker.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
__author__ = 'Khaleeq'
import Guesser
import random
from collections import Counter
tax = ["artefact", "natural"]
def natLangList(list, terminator):
retString = ""
for i in xrange(len(list)):
if len(list) == 1:
return list[0]
if i == len(list) -1:
retString += str(" " +terminator + " " + list[i])
elif i == 0:
retString += str((list[i]))
else:
retString += str(", " + list[i])
return retString
def superQuestion(list):
print("Is this object an"),
print(natLangList(list, "or")),
print "?"
answer = raw_input().lower()
# print list
if answer not in list:
superQuestion(list)
# print "not"
else:
# print "is in"
return answer
def subQuestion(list):
# print list
print("Is it a kind of "),
print(natLangList(list, "or")),
print "?"
answer = raw_input().lower()
print answer
# print list
if answer in list:
return answer
else:
return "skip"
def askPreQuestions(concepts, objects):
sup = superQuestion(tax)
for k in concepts.keys():
if concepts[k]['superclass'] != sup:
del objects[k]
def featuresInObjectset(dict, all_concepts):
cfeats = [] #all concept features
for c in dict.keys():
cfeats += all_concepts[c]['features'].keys()
return Counter(cfeats);
def getQuestion(objects, asked, all_concepts, all_features):
# Get questions over remaining objects
questions = featuresInObjectset(objects, all_concepts)
# Remove previously asked and unlisted questions
for q in questions.keys():
if q in asked:
del questions[q]
if not (q in all_features): ### TODO: Currently HACKY fix to skip unlisted features
del questions[q]
##DIVIDE AND CONQUER##
# Counts of related objects for each feat
counts = sorted(questions.values())
counts = list(set(counts)) ##conversion removes duplicates
# Find median/middle count value, and use this to select a question
med = int(Guesser.middle(counts))
questionChoices = [k for k, v in questions.iteritems() if v == med]
return random.choice(questionChoices)
# chosen = max(questions, key=questions.get) #TODO: separate out maxed/standard q version
# ret = questions.popitem()
def convertToQuestion(feature):
if feature.startswith('it_'):
question = feature.replace("it_", "This_object_", 1)
elif feature.startswith('has_'):
question = feature.replace("has_", "Does_it_have_", 1)
elif feature.startswith('is_'):
question = feature.replace("is_", "Is_it_", 1)
elif feature.startswith('was_'):
question = feature.replace("was_", "Was_it_", 1)
elif feature.startswith('it\'s_'):
question = feature.replace("it's_", "Is_it's_", 1)
elif feature.startswith('for_example_'):
question = feature.replace("for_example_", "Is_\"")
question += "\"_an_example_or_version_of_this_object"
else:
pass
question = question.replace("_", " ")
question += "?"
return question