Exemplo n.º 1
0
def addMessage(chat_id):
    new_idMessage = coll.distinct("idMessage")[-1] + 1
    idUser = int(request.forms.get("idUser"))
    message = str(request.forms.get("text"))
    fields = list(coll.find({"idUser":idUser},{"userName":1, "idUser":1, "_id":0,"idChat":1}))
    print(fields)
    name = fields[0]["userName"]
    for f in fields:
        if f["userName"] == name:
            new_idUser = f["idUser"]
        else:
            name = name
    new_message = {
        "idUser" : idUser,
        "userName" : name,
        "idChat" : int(chat_id),
        "idMessage" : new_idMessage,
        "text" : message
    }
    print(new_message)
    new_user = {
        "idUser" : new_idUser,
        "userName" : name
    }
    print(new_user)
    coll.insert_one(new_message)
Exemplo n.º 2
0
def sentimentReport(idChat):
    """
    ---- Celia's function --- :D
    Returns a report with the sentiment metric from a chat_id
    """
    chat = dumps(
        coll.find({'idChat': int(idChat)}, {
            'userName': 1,
            'text': 1,
            '_id': 0
        }))
    report = getSentimentReport(chat)
    return report
def getChat(chat_id):

    db, coll = connectCollection('chats', 'messages')
    db, collchat = connectCollection('chats', 'chats')
    chatid_data = list(collchat.find({'Chat_id': int(chat_id)}))
    chat_obj_id = chatid_data[0].get('_id')
    chat_data = list(coll.find({'idChat': chat_obj_id}))
    ret = {}
    for i in range(len(chat_data)):
        name = chat_data[i].get('userName')
        message = chat_data[i].get('text')
        date = chat_data[i].get('datetime')
        ret[f'{name}_{date}'] = message
    return ret
Exemplo n.º 4
0
def getSentiment(chat_id):
    query = list(coll.find({'idChat':int(chat_id)}, {"userName":1,"idUser":1,"text": 1,"_id":0}))
    sid = SentimentIntensityAnalyzer()
    total_info=[]
    for text in query:
        info={}
        info["userName"]=text["userName"]
        info["idChat"]=int(chat_id)
        info["text"]=text["text"]
        info["sentiment"]=sid.polarity_scores(text["text"])
        total_info.append(info)
    #return dumps(total_info)
    comp_sent = [s['sentiment']['compound'] for s in total_info]
    avg = reduce((lambda x, y: x+y), comp_sent)/len(comp_sent)
    return dumps({"Sentiments": total_info,
                  "compound sentiments": comp_sent,
                  "avg sentiment [-1,1]": avg})
Exemplo n.º 5
0
def getAllUsers():
    """Returns all users"""
    return dumps(coll.find({}, {'userName': 1, "text": 1, '_id': 0}))
Exemplo n.º 6
0
def getConversations():
    '''
    get all the messages of all conversations
    '''
    return dumps(coll.find({}, {'idChat': 1, 'text': 1, '_id': 0}))
Exemplo n.º 7
0
def getChat():
    '''
    get all the messages of all users
    '''
    return dumps(coll.find({}, {"text": 1, '_id': 0}))
Exemplo n.º 8
0
def getUsers():
    '''
    find all users. There is another better function below
    '''
    all_users = dumps(coll.find().distinct("userName"))
    return all_users
Exemplo n.º 9
0
def index():
    '''
    returns everything in server
    '''
    return dumps(coll.find())
Exemplo n.º 10
0
def index():
    return dumps(coll.find())
Exemplo n.º 11
0
def get_chat(chat_id):
    return dumps(coll.find({"idChat":int(chat_id)},{'_id':0, 'idChat':1, 'userName':1, 'text':1}))
Exemplo n.º 12
0
def demo2(username):
    return dumps(coll.find({'userName':username},{'userName':1, 'idUser':1, 'text':1, '_id':0}))
Exemplo n.º 13
0
def getRecomm(userName):
    req = list(coll.find({'userName': userName},{"text":1,'_id':0}))
    return req