microsoft / jericho

A learning environment for man-made Interactive Fiction games.
GNU General Public License v2.0
253 stars 42 forks source link

'load_bindings()' and action generation grammar parsing error #46

Closed lazyrenee closed 3 years ago

lazyrenee commented 3 years ago

Hi there,

I was trying to generating the action by following the doc's example.

It looks like the jericho.load_bindings() doesn't work anymore, and has been changed to _load_bindings()code.

However, when I try with jericho._load_bindings(), I got another error with the dictionary parsing KeyError: 'grammar'

I'm assuming the text-game dictionary follows the pattern as suggested here, not quite sure what might be wrong with my action generation. Or there might be a new version of the text-games?

Below are my light-testing codes and errors for reference:

from jericho import *
from jericho.template_action_generator import TemplateActionGenerator

bindings = jericho._load_bindings('z-machine-games-master/jericho-game-suite/zork1.z5')
act_gen = TemplateActionGenerator(bindings)
interactive_objs = ['phone', 'keys', 'wallet']
act_gen.generate_actions(interactive_objs)

File "/Users/reneejia/opt/anaconda3/lib/python3.7/site-packages/jericho/template_action_generator.py", line 32, in __init__ grammar = rom_bindings['grammar'].split(';') KeyError: 'grammar'

Thanks in advance!

MarcCote commented 3 years ago

Good catch. We need to update the documentation. Can you try to load the game first, then access the bindings?

from jericho import *
from jericho.template_action_generator import TemplateActionGenerator

# Instantiate the `FrotzEnv` environment with a game.
env = jericho.FrotzEnv('z-machine-games-master/jericho-game-suite/zork1.z5')
bindings = env.bindings

act_gen = TemplateActionGenerator(bindings)
interactive_objs = ['phone', 'keys', 'wallet']
act_gen.generate_actions(interactive_objs)
MarcCote commented 3 years ago

Also, for convenience we added the attribute act_gen to the FrotzEnv object.

env.act_gen  # Which corresponds to `act_gen = TemplateActionGenerator(bindings)`
lazyrenee commented 3 years ago

Thanks for the quick reply and the fixes, very much appreciated! It works for me.