<commit_before>from flask import render_template
from flask.ext.socketio import emit
from messenger import Messenger
from app import app, db, models, socketio


@app.route('/')
def index():
    # TODO: use bootstrap, and remove js/css to separate files.
    return render_template('index.html')


@socketio.on('user', namespace='/chat')
def user_received_message(user_message):
    # TODO: currently, empty messages can be sent.
    # TODO: save message to database for improvements later...
    add_new_lines_user_message = user_message['data'].replace('\n', '</br>')
    emit('response', {'type': 'received', 'data': add_new_lines_user_message})
    open_ended_question = Messenger().open_ended_question(user_message['data'])
    emit('response', {'type': 'service', 'data': open_ended_question})


@socketio.on('connect', namespace='/chat')
def on_connection():
    # Once a connection is established, we want to send the initial message.
    emit('response', {'type': 'service', 'data': Messenger().initial_message()})


@socketio.on('vote', namespace='/chat')
def on_vote(data):
    # TODO: what about initial OEQ and clarification question?
    __cast_vote(data['question'], data['rating'])
    # Only 'update' current question if user is dissatisfied.
    if 'down' in data['rating']:
        emit('vote response',
             {'data': Messenger().open_ended_question(data['prev_user_msg'])})


def __cast_vote(_question, _rating):
    '''
    Updates the rating of a question voted for by a user.

    Args:
        _question (str): The question to vote against.
        _rating (str): can either be 'up' or 'down'.
    '''
    row = models.Question.query.filter_by(question=_question).first()
    # The limits exist to not exceed threshold (0, 1)<commit_msg>Added general comments to view methods<commit_after>from flask import render_template
from flask.ext.socketio import emit
from messenger import Messenger
from app import app, db, models, socketio


@app.route('/')
def index():
    '''
    Homepage of website that contains the chat application.
    '''
    return render_template('index.html')


@socketio.on('user', namespace='/chat')
def user_received_message(user_message):
    '''
    Receives a clients message and sends an appropriate contextual response.

    Args:
        user_message (dict): Contains the user message (data).
        TODO: simplify argument by sending only a string.
    '''
    # TODO: currently, empty messages can be sent.

    add_new_lines_user_message = user_message['data'].replace('\n', '</br>')
    emit('response', {'type': 'received', 'data': add_new_lines_user_message})

    open_ended_question = Messenger().open_ended_question(user_message['data'])
    emit('response', {'type': 'service', 'data': open_ended_question})


@socketio.on('connect', namespace='/chat')
def on_connection():
    '''
    Send initial opening message and instructions on connection.
    '''
    emit('response', {'type': 'service', 'data': Messenger().initial_message()})


@socketio.on('vote', namespace='/chat')
def on_vote(data):
    '''
    Improve rating of OEQ when the up/down buttons clicked.
    Note: a response is only sent IFF the user is dissatisfied (downvotes).

    Args:
        data (dict): contains users previous message and feedback (rating).
    '''
    # TODO: what about initial OEQ and clarification question?
    __cast_vote(data['question'], data['rating'])
    if 'down' in data['rating']:
        emit('vote response',
             {'data': Messenger().open_ended_question(data['prev_user_msg'])})


def __cast_vote(_question, _rating):
    '''
    Updates the rating of a question voted for by a user.

    Args:
        _question (str): The question to vote against.
        _rating (str): can either be 'up' or 'down'.
    '''
    row = models.Question.query.filter_by(question=_question).first()
    # The limits exist to not exceed threshold (0, 1)
{'fraction_non_alphanumeric': 0.13272077590607453, 'fraction_numerical': 0.0010209290454313426, 'mean_word_length': 4.024358974358974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '47478', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1095, 'n_words': 358}