<s> from random import shuffle <EOL> import sys <EOL> from wit import Wit <EOL> if len ( sys . argv ) != <NUM_LIT:2> : <EOL> print ( "<STR_LIT>" ) <EOL> exit ( <NUM_LIT:1> ) <EOL> access_token = sys . argv [ <NUM_LIT:1> ] <EOL> def first_entity_value ( entities , entity ) : <EOL> if entity not in entities : <EOL> return None <EOL> val = entities [ entity ] [ <NUM_LIT:0> ] [ '<STR_LIT:value>' ] <EOL> if not val : <EOL> return None <EOL> return val [ '<STR_LIT:value>' ] if isinstance ( val , dict ) else val <EOL> all_jokes = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] , <EOL> '<STR_LIT:default>' : [ <EOL> '<STR_LIT>' , <EOL> ] , <EOL> } <EOL> def say ( session_id , context , msg ) : <EOL> print ( msg ) <EOL> def merge ( session_id , context , entities , msg ) : <EOL> if '<STR_LIT>' in context : <EOL> del context [ '<STR_LIT>' ] <EOL> category = first_entity_value ( entities , '<STR_LIT>' ) <EOL> if category : <EOL> context [ '<STR_LIT>' ] = category <EOL> sentiment = first_entity_value ( entities , '<STR_LIT>' ) <EOL> if sentiment : <EOL> context [ '<STR_LIT>' ] = '<STR_LIT>' if sentiment == '<STR_LIT>' else '<STR_LIT>' <EOL> elif '<STR_LIT>' in context : <EOL> del context [ '<STR_LIT>' ] <EOL> return context <EOL> def error ( session_id , context , e ) : <EOL> print ( str ( e ) ) <EOL> def select_joke ( session_id , context ) : <EOL> jokes = all_jokes [ context [ '<STR_LIT>' ] or '<STR_LIT:default>' ] <EOL> shuffle ( jokes ) <EOL> context [ '<STR_LIT>' ] = jokes [ <NUM_LIT:0> ] <EOL> return context <EOL> actions = { <EOL> '<STR_LIT>' : say , <EOL> '<STR_LIT>' : merge , <EOL> '<STR_LIT:error>' : error , <EOL> '<STR_LIT>' : select_joke , <EOL> } <EOL> client = Wit ( access_token , actions ) <EOL> session_id = '<STR_LIT>' <EOL> client . run_actions ( session_id , '<STR_LIT>' , { } ) </s>
