File size: 3,523 Bytes
185d33a
 
 
 
 
 
 
12058fc
185d33a
 
 
12058fc
185d33a
 
335cdd4
 
185d33a
 
 
 
 
 
335cdd4
 
185d33a
 
 
12058fc
ff85563
 
12058fc
 
185d33a
 
335cdd4
 
185d33a
12058fc
d6f5c10
185d33a
 
d6f5c10
185d33a
12058fc
d6f5c10
185d33a
 
 
 
 
 
 
 
 
 
 
 
 
 
05bcb80
 
 
 
 
1638b1d
 
 
 
 
 
 
 
05bcb80
 
185d33a
54fe116
185d33a
54fe116
185d33a
12058fc
ff85563
 
12058fc
 
d6f5c10
54fe116
 
 
 
 
d6f5c10
185d33a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from mathtext_fastapi.logging import prepare_message_data_for_logging
from mathtext.sentiment import sentiment
from mathtext.text2int import text2int
import re


def build_nlu_response_object(type, data, confidence):
    """ Turns nlu results into an object to send back to Turn.io
    Inputs
    - type: str - the type of nlu run (integer or sentiment-analysis)
    - data: str - the student message
    - confidence: - the nlu confidence score (sentiment) or '' (integer)
    """
    return {'type': type, 'data': data, 'confidence': confidence}


def test_for_float_or_int(message_data, message_text):
    nlu_response = {}
    if type(message_text) == int or type(message_text) == float:
        nlu_response = build_nlu_response_object('integer', message_text, '')
        prepare_message_data_for_logging(message_data, nlu_response)
    return nlu_response


def test_for_number_sequence(message_text_arr, message_data, message_text):
    nlu_response = {}
    if all(ele.isdigit() for ele in message_text_arr):
        nlu_response = build_nlu_response_object(
            'integer',
            ','.join(message_text_arr),
            ''
        )
        prepare_message_data_for_logging(message_data, nlu_response)
    return nlu_response


def run_text2int_on_each_list_item(message_text_arr):
    """ Attempts to convert each list item to an integer

    Input
    - message_text_arr: list - a set of text extracted from the student message

    Output
    - student_response_arr: list - a set of integers (32202 for error code)
    """
    student_response_arr = []
    for student_response in message_text_arr:
        int_api_resp = text2int(student_response.lower())
        student_response_arr.append(int_api_resp)
    return student_response_arr


def run_sentiment_analysis(message_text):
    # TODO: Add intent labelling here
    # TODO: Add logic to determine whether intent labeling or sentiment analysis is more appropriate (probably default to intent labeling)
    return sentiment(message_text)


def evaluate_message_with_nlu(message_data):
    # Keeps system working with two different inputs - full and filtered @event object
    try:
        message_text = message_data['message_body']
    except KeyError:
        message_data = {
            'author_id': message_data['message']['_vnd']['v1']['chat']['owner'],
            'author_type': message_data['message']['_vnd']['v1']['author']['type'],
            'contact_uuid': message_data['message']['_vnd']['v1']['chat']['contact_uuid'],
            'message_body': message_data['message']['text']['body'],
            'message_direction': message_data['message']['_vnd']['v1']['direction'],
            'message_id': message_data['message']['id'],
            'message_inserted_at': message_data['message']['_vnd']['v1']['chat']['inserted_at'],
            'message_updated_at': message_data['message']['_vnd']['v1']['chat']['updated_at'],
        }
        message_text = message_data['message_body']

    number_api_resp = text2int(message_text.lower())

    if number_api_resp == 32202:
        sentiment_api_resp = sentiment(message_text)
        nlu_response = build_nlu_response_object(
            'sentiment',
            sentiment_api_resp[0]['label'],
            sentiment_api_resp[0]['score']
        )
    else:
        nlu_response = build_nlu_response_object(
            'integer',
            number_api_resp,
            ''
        )

    prepare_message_data_for_logging(message_data, nlu_response)
    return nlu_response