def get_salary_for_interval(request, responder):
    """
	If a user asks for the salary of a specific person for a given time interval (recur_ent: hourly,
	daily, weekly, monthly, yearly; default = hourly), this dialogue state fetches the hourly
	salary from the knowledge base and returns it after converting it to the requested
	time interval.
	"""

    recur_ent = [
        e['value'][0]['cname'] for e in request.entities
        if e['type'] == 'time_recur'
    ][0]

    responder = _get_person_info(request, responder, 'money')

    money = responder.slots['money']
    total_money = _get_interval_amount(recur_ent, money)

    responder.slots['money'] = total_money
    responder.slots['interval'] = recur_ent

    try:
        replies = [
            "{name}'s {interval} salary is ${money}",
            "{name}'s {interval} wage is ${money}"
        ]
        responder.reply(replies)
    except:
        responder.reply(_not_an_employee())
def get_hierarchy_up(request, responder):
    """
	If a user asks about any employees manager or whether they are some other employee's 
	manager, this function captures all the names in the query and returns the employee-manager
	mapping for each one of them.
	"""

    try:
        name_ent = [
            e['value'][0]['cname'] for e in request.entities
            if e['type'] == 'name'
        ]

        # if no name, shift to exception flow
        assert name_ent[0]

        for name in name_ent:
            if name == '':
                responder.reply(_not_an_employee())
            responder = _fetch_from_kb(responder, name, 'manager')
            reply = ["{manager} is {name}'s manager"]
            responder.reply(reply)

    except:
        responder.reply(
            "Who's manager would you like to know? (You can try saying 'Mia's manager')"
        )
def get_hierarchy_down(request, responder):
    """
	If a user asks about any employees subordinates or who reports to them, 
	this function fetches that info from the KB.
	"""

    try:
        name_ent = [
            e['value'][0]['cname'] for e in request.entities
            if e['type'] == 'name'
        ]

        # if no name, shift to exception flow
        assert name_ent[0]

        for name in name_ent:
            if name == '':
                responder.reply(_not_an_employee())
            responder = _fetch_from_kb(responder, name, 'subordinates')
            if len(responder.slots['subordinates']) == 0:
                responder.reply("{name} has no subordinates")
                return
            reply = ["The following people work under {name}: {subordinates}"]
            responder.reply(reply)

    except:
        responder.reply(
            "Who's subordinates would you like to know? (You can try saying 'which employees report to Mia?')"
        )
def get_salary(request, responder):
    """
	If a user asks for the salary of a specific person, this function returns their
	hourly salary by querying into the knowledge base according to the employee name.
	"""

    responder = _get_person_info(request, responder, 'money')
    try:
        responder.reply("{name}'s hourly salary is {money}")
    except:
        responder.reply(_not_an_employee())
Exemplo n.º 5
0
def get_date(request, responder):
    """
	If a user asks for a date related information of any person, this function returns 
	the required date. In case of a termination date related query, it also informs the user 
	of the reason for termination. For non-terminated employees, it informs the user about 
	the active current state of the employee in question.
	"""

    # Look for name from the context of previous turn
    name = request.frame.get('name')

    # If user presents a new name, update the name in context
    try:
        name_ent = [e for e in request.entities if e['type'] == 'name']
        name = name_ent[0]['value'][0]['cname']
    except:
        pass

    # If neither context nor the current query has an employee name, return not an employee
    if not name:
        responder.reply(_not_an_employee())
        responder.listen()
        return

    # If name is found but not in the database, return not an employee
    if name == '':
        responder.reply(_not_an_employee())

    responder.slots['name'] = name
    responder.frame['name'] = name

    employee = app.question_answerer.get(index='employee_data',
                                         emp_name=name)[0]

    # 'action' entities represent employment action such as hiring or termination
    action_entity = [
        e['value'][0]['cname'] for e in request.entities
        if e['type'] == 'employment_action'
    ]

    # 'dob' entities represent the date of birth entity
    dob_entity = [e for e in request.entities if e['type'] == 'dob']

    # Search for the determined action entity or date of birth in the KB and return.
    # 'doh' - 'date of hire', 'dot' - 'date of termination', 'dob' - 'date of birth'
    if action_entity:
        action_entity = action_entity[0]

        if action_entity == 'hired':
            date = employee['doh']
            responder.slots['date'] = date
            responder.reply("{name}'s date of hire was {date}")

        # If action is that of termination:
        # If employee was not terminated, express the active employment status of the employee
        # If the employee was terminated, return both the reason for termination and the date of temrination
        elif action_entity == 'fired':
            date = employee['dot']
            responder.slots['date'] = date
            responder.slots['reason'] = employee['rft']

            if responder.slots['reason'] == 'N/A - still employed':
                responder.reply("{name} is still employed.")
            else:
                responder.reply(
                    "{name}'s date of termination was {date}. The reason for termination was: {reason}."
                )

    elif dob_entity:
        date = employee['dob']
        responder.slots['date'] = date
        responder.reply("{name}'s date of birth is {date}")

    # If no action of dob entities specified by the user, prompt them for specifics in the next turn
    # If the user wants to exit this state they can choose to do that in the return prompt by invoking the 'exit' intent.
    else:
        if request.frame.get('date_visited'):
            responder.reply("If you want to know something else, say 'exit'")
            responder.frame['date_visited'] = False
        else:
            responder.reply(
                'What would you like to know about {name}? You can ask about date of hire, date of termination or date of birth.'
            )
            responder.frame['date_visited'] = True
            responder.params.allowed_intents = ('date.get_date',
                                                'general.get_info',
                                                'salary.get_salary',
                                                'hierarchy.*', 'greeting.*')
            responder.listen()