Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions textworld/envs/wrappers/tests/test_tw_inform7.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ def test_score(self):
game_state, _, _ = env.step("close chest")
assert game_state.score == 3

def test_multiple_commands(self):
# Multiple commands on one line (e.g. "go east. go west.") must
# not crash: the Inform7 interpreter prints one set of extra-info
# tags per command, so the reported state must come from the last
# command.
for env in [self.env_z8]:
env.reset()
game_state, _, _ = env.step("go east. go west.")
assert game_state.moves == 2

def test_moves(self):
for env in [self.env_z8]:
initial_state = env.reset()
Expand Down
12 changes: 6 additions & 6 deletions textworld/envs/wrappers/tw_inform7.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ def _detect_extra_infos(text: str, tracked_infos: Optional[List[str]] = None) ->
if tag not in AVAILABLE_INFORM7_EXTRA_INFOS:
raise ValueError("TW game doesn't support tag: {}".format(tag))

regex = re.compile(r"<{tag}>\n(.*)</{tag}>".format(tag=tag), re.DOTALL)
match = re.search(regex, text)
if match:
_, cleaned_text = _detect_i7_events_debug_tags(match.group(1))
regex = re.compile(r"<{tag}>\n(.*?)</{tag}>".format(tag=tag), re.DOTALL)
all_matches = regex.findall(text)
if all_matches:
_, cleaned_text = _detect_i7_events_debug_tags(all_matches[-1])
matches[tag] = cleaned_text.strip()
text = re.sub(regex, "", text)
text = regex.sub("", text)
else:
matches[tag] = None

Expand Down Expand Up @@ -137,7 +137,7 @@ def _gather_infos(self):
if self.state[info] is not None and type(self.state[info]) is not int:
try:
self.state[info] = int(self.state[info].strip())
except:
except ValueError:
self.state[info] = int(self.state[info].strip().split("\n")[0])

self.state["won"] = '*** The End ***' in self.state["feedback"]
Expand Down
Loading