Skip to content

Commit 225bb18

Browse files
authored
Merge pull request #29 from MarcCote/bf_names_ambiguity
BF: Object names disambiguation
2 parents d3f7b00 + 8689337 commit 225bb18

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

textworld/generator/inform7/tests/test_world2inform7.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_names_disambiguation():
165165
assert "tasty apple" in game_state.inventory
166166
assert "tasty apple" not in game_state.description
167167

168-
# With two-argument actions.
168+
# Actions with two arguments.
169169
M = textworld.GameMaker()
170170
roomA = M.new_room("roomA")
171171
roomB = M.new_room("roomB")
@@ -213,6 +213,53 @@ def test_names_disambiguation():
213213
game_state, _, done = env.step("go west")
214214
assert "-= Roomc =-" in game_state.description
215215

216+
# Test invariance of the order in which ambiguous object names are defined.
217+
# First define "type G safe" then a "safe".
218+
M = textworld.GameMaker()
219+
garage = M.new_room("garage")
220+
M.set_player(garage)
221+
222+
key = M.new(type="k", name="key")
223+
typeG_safe = M.new(type="c", name="type G safe")
224+
safe = M.new(type="c", name="safe")
225+
226+
safe.add(key)
227+
garage.add(safe, typeG_safe)
228+
229+
M.add_fact("open", safe)
230+
231+
game = M.build()
232+
game_name = "test_names_disambiguation"
233+
with make_temp_directory(prefix=game_name) as tmpdir:
234+
game_file = compile_game(game, game_name, games_folder=tmpdir)
235+
env = textworld.start(game_file)
236+
game_state = env.reset()
237+
game_state, _, done = env.step("take key from safe")
238+
assert "key" in game_state.inventory
239+
240+
# First define "safe" then "type G safe".
241+
M = textworld.GameMaker()
242+
garage = M.new_room("garage")
243+
M.set_player(garage)
244+
245+
key = M.new(type="k", name="key")
246+
safe = M.new(type="c", name="safe")
247+
typeG_safe = M.new(type="c", name="type G safe")
248+
249+
safe.add(key)
250+
garage.add(safe, typeG_safe)
251+
252+
M.add_fact("open", safe)
253+
254+
game = M.build()
255+
game_name = "test_names_disambiguation"
256+
with make_temp_directory(prefix=game_name) as tmpdir:
257+
game_file = compile_game(game, game_name, games_folder=tmpdir)
258+
env = textworld.start(game_file)
259+
game_state = env.reset()
260+
game_state, _, done = env.step("take key from safe")
261+
assert "key" in game_state.inventory
262+
216263

217264
def test_take_all_and_variants():
218265
M = textworld.GameMaker()

textworld/generator/inform7/world2inform7.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,15 @@ def generate_inform7_source(game, seed=1234, use_i7_description=False):
326326
327327
""")
328328

329-
# Refeering to an object by it whole name shouldn't be ambiguous.
329+
# Referring to an object by its whole name shouldn't be ambiguous.
330330
source += textwrap.dedent("""\
331-
Does the player mean doing something with something (called target):
332-
if the player's command matches the text printed name of the target and the second noun is nothing:
333-
it is very likely;
334-
if the player's command matches the text printed name of the target and the player's command matches the text printed name of the second noun:
335-
it is very likely. [Handle action with two arguments.]
336-
331+
Does the player mean doing something:
332+
if the noun is not nothing and the second noun is nothing and the player's command matches the text printed name of the noun:
333+
it is likely;
334+
if the noun is nothing and the second noun is not nothing and the player's command matches the text printed name of the second noun:
335+
it is likely;
336+
if the noun is not nothing and the second noun is not nothing and the player's command matches the text printed name of the noun and the player's command matches the text printed name of the second noun:
337+
it is very likely. [Handle action with two arguments.]
337338
""")
338339

339340
# Useful for listing room contents with their properties.
@@ -393,7 +394,8 @@ def generate_inform7_source(game, seed=1234, use_i7_description=False):
393394
remove the list of containers from L;
394395
remove the list of supporters from L;
395396
remove the list of doors from L;
396-
say "There is [L with indefinite articles] on the floor.";
397+
if the number of entries in L is greater than 0:
398+
say "There is [L with indefinite articles] on the floor.";
397399
398400
""")
399401

0 commit comments

Comments
 (0)