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)
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
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})
def getAllUsers(): """Returns all users""" return dumps(coll.find({}, {'userName': 1, "text": 1, '_id': 0}))
def getConversations(): ''' get all the messages of all conversations ''' return dumps(coll.find({}, {'idChat': 1, 'text': 1, '_id': 0}))
def getChat(): ''' get all the messages of all users ''' return dumps(coll.find({}, {"text": 1, '_id': 0}))
def getUsers(): ''' find all users. There is another better function below ''' all_users = dumps(coll.find().distinct("userName")) return all_users
def index(): ''' returns everything in server ''' return dumps(coll.find())
def index(): return dumps(coll.find())
def get_chat(chat_id): return dumps(coll.find({"idChat":int(chat_id)},{'_id':0, 'idChat':1, 'userName':1, 'text':1}))
def demo2(username): return dumps(coll.find({'userName':username},{'userName':1, 'idUser':1, 'text':1, '_id':0}))
def getRecomm(userName): req = list(coll.find({'userName': userName},{"text":1,'_id':0})) return req