Utilities¶
-
jericho.util.
chunk
(items: List[T], n: int) → List[List[T]]¶ Split a list of items into n chunks.
The first len(items) % n chunks will contain one more item.
- Args:
- items (List): list of items to be split. n (int): number of chunks to return.
- Returns:
- List[List]: Chunks containing the items.
- References:
- https://stackoverflow.com/a/2135920
-
jericho.util.
clean
(s)¶ Cleans a string for compact output.
Example: >>> clean('*This string\nneeds a good clean.--\n\n*') 'This string needs a good clean.'
-
jericho.util.
extract_objs
(text)¶ Uses Spacy to extract a set of tokens corresponding to possible objects in the provided text.
Example: >>> extract_objs('The quick brown fox jumped over the lazy dog.') {'brown', 'dog', 'fox', 'lazy', 'quick'}
Note
Returns adjectives as well as nouns since many games allow players to refer to objects using adjectives.
-
jericho.util.
get_subtree
(obj_nb, full_object_tree)¶ Traverses the object tree, returning a list containing full subtree at obj_nb.
Parameters: - obj_nb (int) –
jericho.ZObject.num
corresponding to the root of the subtree to collect. - full_object_tree (list) – Full list of objects as given by Jericho’s get_world_objects().
Returns: A list
jericho.ZObject
of all descendents and siblings of obj_nb.Example: >>> env = FrotzEnv('zork1.z5') >>> world_objs = env.get_world_objects() >>> env.get_player_object() Obj4: cretin Parent180 Sibling181 Child0 Attributes [7, 9, 14, 30] Properties [18, 17, 7] >>> get_subtree(4, world_objs) [ Obj4: cretin Parent180 Sibling181 Child0 Attributes [7, 9, 14, 30] Properties [18, 17, 7], Obj181: door Parent180 Sibling160 Child0 Attributes [14, 23] Properties [18, 17, 16], Obj160: mailbox Parent180 Sibling0 Child161 Attributes [13, 19] Properties [18, 17, 16, 10], Obj161: leaflet Parent160 Sibling0 Child0 Attributes [16, 17, 26] Properties [18, 16, 15, 11, 8] ]
- obj_nb (int) –
-
jericho.util.
recognized
(response)¶ Returns True if the game’s response indicates the last action was successfully parsed.
Parameters: response (string) – The game’s textual response to the last action. Example: >>> recognized('I dont know the word "Azerbijan".') False >>> recognized('The vines block your way.') True
Note
Invalid actions may often be recognzied. Only ungrammatical actions are detected by this method.
-
jericho.util.
tokenize
(str)¶ Returns a list of tokens in a string (using Spacy).
Example: >>> tokenize('This is an example sentence.') ['this', 'is', 'an', 'example', 'sentence', '.']
-
jericho.util.
unabbreviate
(action)¶ Returns an unabbreviated version of a text action.
Example: >>> unabbreviate('nw') 'northwest' >>> unabbreviate('x sewer') 'examine sewer'
-
jericho.util.
verb_usage_count
(verb, max_word_length=None)¶ Returns the number of times that a particular verb appears across all clubfloyd transcripts.
Params verb: Retrieve a usage count for this verb. Example: >>> verb_usage_count('take') 8909 >>> verb_usage_count('jump') 786