def SendToHub(provider, frame):
    prog_name = "main"
    global provider_env
    env = provider_env[provider]
    if env:
        f = Galaxy.Frame(prog_name, Galaxy.GAL_CLAUSE, frame)

        try:
            env.WriteFrame(f)
        except GalaxyIO.DispatchError:
            Log('ERROR: cannot send frame')
def Welcome(env, dict):
    Log(dict)
    user_id = dict[":user_id"]
    # Log(user_id)
    # if env and user_id not in provider_env:
    #     provider_env[user_id] = env
    #     Log('Stored the env for user_id %s' %(user_id))
    Log("Welcome to the new Backend Server")
    prog_name = "reinitialize"
    #print Galaxy.Frame(prog_name, Galaxy.GAL_CLAUSE,dict)
    print dict
    return Galaxy.Frame(prog_name, Galaxy.GAL_CLAUSE, dict)
Пример #3
0
def Welcome(env, dict):
	#global connection
	Log(dict)
	user_id = dict[":user_id"]
	# Log(user_id)
	# if env and user_id not in provider_env:
	#     provider_env[user_id] = env
	#     Log('Stored the env for user_id %s' %(user_id))
	Log("Welcome to the new Backend Server")
	prog_name = "reinitialize"
	#serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	#serversocket.bind(('localhost', 13000))
	#connection, address = serversocket.accept()
	#print Galaxy.Frame(prog_name, Galaxy.GAL_CLAUSE,dict)
	print dict
	return Galaxy.Frame(prog_name, Galaxy.GAL_CLAUSE,dict)
def LaunchQuery(env, dict):
    global requestCounter
    Log("Launching a query")

    Log(dict.keys())

    propertiesframe = env.GetSessionProperties(dict.keys())
    hub_opaque_data = propertiesframe[':hub_opaque_data']
    provider_id = hub_opaque_data[':provider_id'].strip('[').strip(']')

    try:
        prog_name = dict[":program"]
    except:
        prog_name = "main"

    inframe = dict[":inframe"]
    inframe = inframe.replace("\n{c inframe \n}", "")

    Log("Converting inframe to galaxy frame")
    #Log(inframe)

    raw_inframe_str = dict[":inframe"]
    inframe_raw_dict = ReadRawInFrame(raw_inframe_str)

    Log('RAW INFRAME is \n%s' % (str(inframe_raw_dict)))
    user_input = ''
    system_response = 'pardon me'
    try:
        user_input = inframe_raw_dict['user_input'].strip('"')
        user_input = user_input.replace('_', ' ')
    except KeyError:
        system_response = 'I am Tick Tock, how are you doing'
        pass

    if user_input:
        #system_response = user_input
        #system_response = get_response(user_input)
        filehistory = open('input_response_history.txt', 'r')
        system_tail = tail(filehistory, 4)
        filehistory.close()
        Log('USER INPUT is [ %s ]' % (user_input))
        if user_input == '':
            system_response = 'pardon me'
        elif (user_input == 'repeat') or (user_input == 'say that again') or (
                user_input == 'what did you say'):
            filein = open('history.txt', 'r')
            system_response = filein.readline()
            filein.close()
        elif (system_tail[0] == system_tail[2]) and (system_tail[0]
                                                     == user_input):
            system_response = 'I am having a good time talking to you.{ {BREAK TIME="2s"/}} Do you want to keep going,' \
                              ' if not, you can say goodbye'
        else:
            system_response = get_response(user_input)
        fileout = open('history.txt', 'w')
        fileout.write(str(system_response) + '\n')
        fileout.close()
        prefix = ['', 'well ... ', 'uh ... ', '', 'let me see ... ', 'oh ... ']
        cur_index = -1
        while True:
            random_index = randrange(0, len(prefix))
            if random_index != cur_index:
                break
        cur_index = random_index
        system_response = prefix[cur_index] + system_response
    resultsFrame = '{\n res %s \n}\n}' % (system_response)
    #Log("outframe")
    f = Galaxy.Frame(prog_name, Galaxy.GAL_CLAUSE, {":outframe": resultsFrame})
    #Log(f)

    return f
def SetDialogState(env, dict):
    global current_dialog_state
    global home_dialog_state
    global current_dialog_state_counter
    global current_dialog_state_begin
    global global_dialog_state_counter

    inframe = dict[":dialog_state"]
    # extracting the dialog state and turn number
    # main logic of updating the dialog state, such as sleeping, awake, etc
    lines = inframe.split('\n')
    new_dialog_state = None
    turn_counter = 0

    for l in lines:
        components = l.split(' = ')
        if (len(components) != 2):
            continue
        prefix = components[0]
        suffix = components[1]
        if (prefix == "dialog_state"):
            new_dialog_state = suffix

            if (global_dialog_state_counter == 0):
                home_dialog_state = new_dialog_state

            print "current_dialog_state", current_dialog_state
            print "new_dialog_state", new_dialog_state
            if (current_dialog_state == new_dialog_state):
                current_dialog_state_counter = turn_counter - current_dialog_state_begin
                current_dialog_state = new_dialog_state
                print "cur == new, cur_counter =", current_dialog_state_counter
            else:
                current_dialog_state = new_dialog_state
                current_dialog_state_counter = 0
                current_dialog_state_begin = turn_counter
                print "cur != new, cur_begin =", current_dialog_state_begin
                print "cur_counter =", current_dialog_state_counter

        elif (prefix == "turn_number"):
            turn_counter = int(suffix)
            print "get turn counter", turn_counter
            if (global_dialog_state_counter == -1 or turn_counter == 0):
                global_dialog_state_counter = 0
                #print "set g_d_s_c to 0"
            else:
                global_dialog_state_counter = turn_counter
                #print "g_d_s_c =", turn_counter

            #print "end of turn counter"
    print "==============================="
    print "DIALOG STATE is", current_dialog_state
    print "CURRENT TURN NUMBER is", current_dialog_state_counter

    state_out = -1

    if (current_dialog_state.endswith(aware_state)):
        print "system is aware of the person but can't see"
        state_out = 4
    elif (current_dialog_state == home_dialog_state):
        print "system is sleeping now ... zzz"
        state_out = 1
    elif (current_dialog_state_counter >= 1):
        print "system is puzzled ... "
        state_out = 2
    else:
        print "system can understand you."
        state_out = 3

    count = 1
    onDialogState(state_out)
    print "==============================="
    # end of the main logic

    prog_name = "main"
    outframe = "got dialog state"
    f = Galaxy.Frame(prog_name, Galaxy.GAL_CLAUSE, {":outframe": outframe})
    return f