Пример #1
0
def getSmallTalkResponse(textmessage):

    response = requests.get(url=(Parameters.smalltalk_url + textmessage + '/'))
    logger.debug(response.text)
    data = response.text

    return data
Пример #2
0
def getInvoiceStatus(ner, responseJson):
    logger.debug('get Invoice Status')
    responses = set()

    try:
        for ne in ner:
            if ne[Parameters.label] == Parameters.date or ne[
                    Parameters.label] == Parameters.org:

                response = requests.get(url=(Parameters.invoice_status_url +
                                             ne[Parameters.text] + '/'))
                status = response.json()
                res = responseJson[Parameters.response]
                res = res.replace(Parameters.status,
                                  status[Parameters.inv_status])
                res = res.replace(Parameters.amount,
                                  status[Parameters.inv_amount])
                responses.add(res)

    except (RuntimeError, TypeError, NameError, KeyError) as ex:
        logger.error('Error')
        logger.error(ex)

        res = responseJson[Parameters.default]
        responses.add(res)

    return responses
Пример #3
0
def getEntities(textmessage):

    logger.debug('get Entities')
    response = requests.get(url=(Parameters.ner_url + textmessage + '/'))
    logger.debug(response)
    data = response.json()

    return data
Пример #4
0
def getChatbotResponse(request, inputString):

    logger.debug('Request reached in chatbot views')
    logger.info('User input -->' + inputString)
    response = chatbot.get_chatbot_response(inputString)
    logger.debug('response -->' + response)

    return HttpResponse(response)
Пример #5
0
def getRushpayment(ner, responseJson):

    responses = set()
    logger.debug('get Rush payment')
    res = Parameters.rush_pay_response

    responses.add(res)

    return responses
Пример #6
0
def get_chatbot_response(textmessage):

    logger.info('Chatbot input : ' + textmessage)
    textmessage = re.sub(r'[^\w]', ' ', textmessage)
    textmessage = re.sub(' +', ' ', textmessage)
    chat_response = set()

    with open(Parameters.responsefile) as json_data:
        responseJson = json.load(json_data)
        logger.debug(responseJson)
        json_data.close()

        logger.debug('responseJson file loaded ')

    response = requests.get(url=(Parameters.intent_url + textmessage + '/'))

    # extracting data in json format
    data = response.json()
    logger.info('Chatbot intent : ' + str(data))
    intent = data[Parameters.Label]
    probability = data[Parameters.probability]

    if float(probability) > Parameters.confidence:

        if intent == Parameters.SmallTalk:
            talk = getSmallTalkResponse(textmessage)
            chat_response.add(talk)
            logger.info('SmallTalk response : ' + str(chat_response))

        else:
            ner = getEntities(textmessage)
            logger.info('ner response : ' + str(ner))

            switcher = {
                Parameters.ApprovalPolicy: getApprovalResponse,
                Parameters.InvoiceStatus: getInvoiceStatus,
                Parameters.InvoiceSubProcess: getInvoiceSubProcess,
                Parameters.Rushpayment: getRushpayment
            }

            func = switcher.get(intent, lambda: "Sorry, I don't know")
            # Execute the function
            chat_response = func(ner, responseJson[intent])

            logger.info('Chatbot chat_response : ' + str(len(chat_response)))
            if len(chat_response) == 0:
                default = responseJson[intent][Parameters.default]
                logger.info(default)
                chat_response.add(default)
    logger.info(chat_response)
    if chat_response == '' or len(chat_response) == 0:
        chat_response.add(responseJson[Parameters.default])
        logger.info(chat_response)

    return (chat_response.pop())
Пример #7
0
def getInvoiceSubProcess(ner, responseJson):

    responses = set()
    logger.debug('get Invoice SubProcess')
    for ne in ner:
        #query email
        if ne[Parameters.label] == Parameters.gpe:
            res = getEmailAddress(ne[Parameters.text],
                                  responseJson[Parameters.response])

            responses.add(res)

    return responses
Пример #8
0
def getlimit(approver):
    logger.debug('getting limit')
    response = requests.get(url=(Parameters.approval_url + approver))

    logger.debug(response)
    res = ''

    try:
        response = response.json()
        res = response[Parameters.limit]

    except (KeyError) as ex:
        logger.error('Error')
        logger.error(ex)

        res = response[Parameters.error]
        logger.info(res)

    return res
Пример #9
0
def getApprovalResponse(ner, responseJson):

    logger.debug('get Approval Response')
    responses = set()

    try:
        for ne in ner:
            if ne[Parameters.label] == Parameters.approver_ne:

                limit = getlimit(ne[Parameters.text])

                responses.add(limit)

    except (RuntimeError, TypeError, NameError, KeyError) as ex:
        logger.error('Error')
        logger.error(ex)

        res = responseJson[Parameters.default]
        responses.add(res)

    return responses
Пример #10
0
def input(request):

    if request.method == 'POST':
        logger.debug('Request reached in chatbot views')
        inputString = request.POST['text']
        response = ''
        if inputString != '' or len(inputString) > 0:
            logger.info('User input -->' + inputString)
            response = chatbot.get_chatbot_response(inputString)
            logger.debug('response -->' + response)
        template = loader.get_template('response/input.html')
        context = {
            'response': response,
        }

        return HttpResponse(template.render(context, request))

    else:
        template = loader.get_template('response/input.html')
        context = {}
        return HttpResponse(template.render(context, request))
Пример #11
0
def getEmailAddress(region, responseJson):
    logger.debug('get EmailAddress')
    response = requests.get(url=(Parameters.invoice_email_url + region))
    logger.debug(response)
    res = ''

    try:
        response = response.json()
        logger.info(response)
        email = response[Parameters.email_ad]

        res = responseJson.replace(Parameters.email, email)

    except (KeyError) as ex:
        logger.error('Error')
        logger.error(ex)

        res = response[Parameters.error]

    logger.info(res)

    return res