Skip to content

Commit b1ba61a

Browse files
authored
Merge pull request #62 from allenai/enh_autocompletion
Add autocompletion support to examples/human.py
2 parents 3c90216 + 71e53c4 commit b1ba61a

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

examples/human.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
1+
import sys
12
import time
23
import argparse
34

45
from scienceworld import ScienceWorldEnv
56

67

8+
prompt_toolkit_available = False
9+
try:
10+
# For command line history and autocompletion.
11+
from prompt_toolkit import prompt
12+
from prompt_toolkit.completion import WordCompleter
13+
from prompt_toolkit.history import InMemoryHistory
14+
prompt_toolkit_available = sys.stdout.isatty()
15+
except ImportError:
16+
pass
17+
18+
try:
19+
# For command line history when prompt_toolkit is not available.
20+
import readline # noqa: F401
21+
except ImportError:
22+
pass
23+
24+
725
def userConsole(args):
826
""" Example user input console, to play through a game. """
27+
history = None
28+
if prompt_toolkit_available:
29+
history = InMemoryHistory()
30+
931
exitCommands = ["quit", "exit"]
1032

1133
taskIdx = args['task_num']
@@ -98,13 +120,22 @@ def userConsole(args):
98120
print("isCompleted: " + str(isCompleted))
99121
# print("info: " + str(info))
100122

101-
print("'help' lists valid action templates, 'objects' lists valid" +
102-
" objects, 'valid' lists valid action-object combinations (long!). ")
123+
print("'help' lists valid action templates, 'objects' lists valid objects, use <tab> to list valid actions. ")
103124
print("'goals' lists progress on subgoals.")
104125
print("type 'exit' to quit.")
105126

127+
# Select a random action
128+
valid_actions = env.get_valid_action_object_combinations()
129+
106130
# Get user input
107-
userInputStr = input('> ')
131+
if prompt_toolkit_available:
132+
actions_completer = WordCompleter(valid_actions, ignore_case=True, sentence=True)
133+
userInputStr = prompt('> ', completer=actions_completer,
134+
history=history, enable_history_search=True)
135+
else:
136+
print("Valid Actions: " + str(valid_actions))
137+
userInputStr = input('> ')
138+
108139
# Sanitize input
109140
userInputStr = userInputStr.lower().strip()
110141

0 commit comments

Comments
 (0)