Exemple #1
0
def add_user():
    chat = request.args['chat']
    user = request.args['user']

    cur = db.chats.find({}, {'name': 1})
    if chat not in [n['name'] for n in cur]:
        raise APIError(
            'That chat does not exist, you can create it with the endpoint /chat/create/<chat_name>'
        )
    cur = db.users.find({}, {'name': 1})
    if user not in [n['name'] for n in cur]:
        raise APIError(
            'That user does not exist, you can create it with the endpoint /user/create/<user_name>'
        )

    id_chat = db.chats.find_one({'name': chat}, {'_id': 1})['_id']
    id_user = db.users.find_one({'name': user}, {'_id': 1})['_id']

    cur = db.chats.find({'_id': id_chat}, {'participants': 1})
    if id_user in [e['participants'] for e in cur][0]:
        raise APIError('That user is already in the chat')

    db.chats.update({'_id': id_chat}, {'$addToSet': {'participants': id_user}})
    db.users.update({'_id': id_user}, {'$addToSet': {'chats': id_chat}})
    return {
        'Chat': dumps(db.chats.find_one({'name': chat})),
        'Message': 'We succeded in adding the user'
    }
Exemple #2
0
def AddUserChat(chatname, username):
    usernames = db.users.distinct("user_name")  # get all usernames that exist
    user_id = db.users.find_one(
        {'user_name': username},
        {'user_id': 1})['user_id']  # get the user_id for the username
    chatnames = db.chats.distinct("chat_name")  # get all chatnames that exist
    chat_id = db.chats.find_one(
        {'chat_name': chatname},
        {'chat_id': 1})['chat_id']  #get the chat_id for the chatname
    #Check if username and chatname exist
    if username not in usernames:
        print("ERROR")
        raise APIError("I´m sorry this user doesn´t exist.")
    if chatname not in chatnames:
        print("ERROR")
        raise APIError("I´m sorry this chat doesn´t exist.")
    #Check if user_id  exist in chat
    chat_users = db.chats.find({"chat_name": chatname}, {"users": 1})
    if user_id in [user["users"] for user in chat_users][0]:
        raise APIError("I´m sorry. This username already exists in this chat.")
    #Add new user in chat
    db.chats.update({'chat_id': chat_id}, {'$addToSet': {'users': user_id}})
    return dumps(
        f"Success! chat_id:{chat_id},chat_name:{chatname}, users:{user_id}, new_user:{username}"
    )
Exemple #3
0
def recommender(user):

    if not request.args:
        raise APIError(
            'This endpoint requires a paramether type= "similar" or type="sentiment"'
        )

    type_recom = request.args['type']

    if type_recom not in ['similar', 'sentiment']:
        raise APIError(
            'The type parameter must be either "similar" or "sentiment"')

    #Create a dictionary with all the messages of each user
    messages = {}
    users = db.users.find({}, {'_id': 1, 'name': 1})
    for e in users:
        texts = db.messages.find({'user': e['_id']}, {'text': 1, '_id': 0})
        messages[e['name']] = ' '.join([t['text'] for t in texts])

    #Remove stopwords from the messages:
    trimmed = {}
    stpwrd = set(stopwords.words('english'))
    string = ''
    for k, v in messages.items():
        trimmed[k] = ' '.join([w for w in v.split(' ') if w not in stpwrd])

    if type_recom == 'similar':
        #Create a sparse_matrix with the counts of each word for each of the users
        count_vectorizer = CountVectorizer()
        sparse_matrix = count_vectorizer.fit_transform(trimmed.values())
        matrix = sparse_matrix.todense()

        #Calculate the cosine distances between users:
        similarity_matrix = distance(matrix, matrix)
        sim_df = pd.DataFrame(similarity_matrix,
                              columns=messages.keys(),
                              index=messages.keys())

        similars = sim_df[user].sort_values(ascending=False)[1:].head(3)
        return {'Similar users': list(similars.index)}

    elif type_recom == 'sentiment':
        sia = SentimentIntensityAnalyzer()
        sentim = {}
        for k, v in trimmed.items():
            sentim[k] = sia.polarity_scores(v)
        simi = pd.DataFrame(sentim).T
        distances = pd.DataFrame(1 /
                                 (1 + squareform(pdist(simi, 'euclidean'))),
                                 index=simi.index,
                                 columns=simi.index)
        similars = distances[user].sort_values(ascending=False)[1:].head(3)
        return {'Similar users': list(similars.index)}
Exemple #4
0
def create_conversation():
    try:
        conversation_name = request.args.get("conversation")
    except:
        raise APIError(
            "Wrong parameters. Try /user/create?conversation=<conversation_name>"
        )
    try:
        db["conversations"].insert_one({
            "conversation_name": conversation_name,
            "users": [],
            "messages": []
        })
        return {conversation_name: "created!"}
    except:
        raise APIError("This conversation already exists in database")
Exemple #5
0
def create_chat(name):
    '''
    Insert a new chat and check 
    that it has been correctly inserted by returning 
    a find_one. The function checks for unique names and can insert one participant as 
    a parameter users
    '''
    cur = db.chats.find({}, {'name': 1})
    if name in [n['name'] for n in cur]:
        raise APIError('That name is already in use, please use another name')
    db.chats.insert_one({'name': name, 'participants': []})
    if request.args:
        for u in request.args:
            user = request.args[u]
            id = db.users.find_one({'name': user}, {'_id': 1})['_id']
            id_chat = db.chats.find_one({'name': name}, {'_id': 1})['_id']
            db.chats.update({'name': name},
                            {'$addToSet': {
                                'participants': id
                            }})
            db.users.update({'_id': id}, {'$addToSet': {'chats': id_chat}})

    check = db.chats.find_one({'name': name})
    return {
        'message': 'We succeded in creating the chat!',
        'result': dumps(check)
    }
def insertChat():
    arr = request.args.get("ids")
    print(arr)
    name = request.args.get("name", default='')

    #creation of a new chat with the users included in arr
    if arr:
        arr = ast.literal_eval(arr)
        dic = {
            'chat_name': name,
            'creation_date': getDate(),
            'users_list': [],
            'messages_list': []
        }
        chat_id = db.chats.insert_one(dic)
        #insert the users in the chat
        chatId = chat_id.inserted_id
        for user_id in arr:
            #r = requests.get(f'http://localhost:3500/chat/{chatId}/adduser?user_id={user_id}')
            r = addChatUser(chatId, user_id)
        #update of the users chats_list by adding the chat id
        for user_id in arr:
            post = db.users.find_one({'_id': ObjectId(user_id)})
            post['chats_list'].append(ObjectId(chat_id.inserted_id))
            db.users.update_one({'_id': ObjectId(user_id)}, {"$set": post},
                                upsert=False)

    else:
        print("ERROR")
        raise APIError(
            "Tienes que mandar un query parameter ?ids=<arr>&name=<chatname>")

    return json.dumps({'chat_id': str(chat_id.inserted_id)})
Exemple #7
0
def add_message(conversation_name):
    conversation = db['conversations'].find_one(
        {"conversation_name": conversation_name}, {
            "_id": 1,
            "conversation_name": 1,
            "users": 1
        })
    if not conversation:
        raise Error404("Conversation doesn't exist in database")
    username = request.args.get("username").lower()
    user = db['users'].find_one({"username": username}, {"username": 1})
    if not user:
        raise Error404("User not found in database")
    if user["username"] not in conversation["users"]:
        raise APIError("User is not in this conversation.Please, add it first")

    message = request.args.get("message").replace("%20", " ")
    message_id = db["messages"].insert_one({
        "message":
        message,
        "user": [username, user["_id"]],
        "conversation":
        [conversation["conversation_name"], conversation["_id"]]
    }).inserted_id
    db["users"].update({"_id": user["_id"]},
                       {"$addToSet": {
                           "messages": message_id
                       }})
    db["conversations"].update({"_id": conversation["_id"]},
                               {"$addToSet": {
                                   "messages": message_id
                               }})
    print("hola")
    return (f"{{'message_id':{message_id}}}")
def getSentiment(chat_id):
    #mess=requests.get(f'http://localhost:3500/chat/{chat_id}/list').json()
    mess = ast.literal_eval(getMessages(chat_id))
    sentiments = {}
    try:
        lang = request.args.get("lang")
    except:
        raise APIError(
            "You should specify the language of the chat in the query parameters [english='en',spanish='es'] ?lang=<lang>"
        )

    if lang == 'en':

        for id, text in mess.items():
            sentiments[id] = {'text': text, "score": sia.polarity_scores(text)}
        sums = 0
        for v in sentiments.values():
            sums += v['score']['compound']
        avg = sums / len(sentiments)
        sentiments['chat_sentiment'] = avg
    else:
        for id, text in mess.items():
            sentiments[id] = {'text': text, "score": clf.predict(text)}
        sums = 0
        for v in sentiments.values():
            sums += v[
                'score'] * 2 - 1  #normalize the score(in senti the score_value domain is [0,1])
        avg = sums / len(sentiments)
        sentiments['chat_sentiment'] = avg

    return json.dumps(sentiments)
Exemple #9
0
def CreateChat(new_chatname):
    chatnames = (db.chats.distinct("chat_name"))
    new_chatid = max(db.chats.distinct("chat_id")) + 1  # create new chat_id
    users_list_id = list(db.chats.distinct("user_id"))
    #Check if username exist in chat
    if new_chatname in chatnames:
        print("ERROR")
        raise APIError("I´m sorry this chat name already exists.")
    # Create new chat
    chat_details = {
        "chat_id": new_chatid,
        "chat_name": new_chatname,
        "users": []
    }
    db.chats.insert_one(chat_details)
    #Include users in chat
    if request.args:
        for param in request.args:
            user = request.args[param]  # create param
            user_id = db.users.find_one(
                {'user_name': user},
                {'user_id': 1
                 })['user_id']  # get user_id for the username (param)
            db.chats.update({'chat_name': new_chatname},
                            {'$addToSet': {
                                'users': user_id
                            }})  #include user_id in chat
    return dumps(
        f"Success! chat_id:{new_chatid},chat_name:{new_chatname}, users:{user_id}"
    )
Exemple #10
0
def extractList(chat_name):
    # Check if chat is in the data base
    # Do a query of the name of the chat.
    query = list(db.find({"chatname": chat_name}))
    #print(query)
    if len(query) == 0:
        raise APIError(
            "Chat doesn't exists in the dabatabase. Please, chose another name or create the chat."
        )
    else:
        # Extract the id of the chat from chat name
        id_chat = list(db.find({"chatname": chat_name}, {"_id": 1}))[0]
        #print(id_chat[0]["_id"])
        #id_chat["_id"]

        # Lok for all the messages in that chat
        messages = list(
            db.find(
                {"$and": [{
                    "type": "message"
                }, {
                    "chat_id": id_chat["_id"]
                }]}))
        total = len(messages)
        #print(messages)
        lista_msg = [e["text"] for e in messages]

        return {
            "status": f"{total} messages found in the chat",
            "list": lista_msg
        }
def addChatUser(chat_id, user_id=None):
    if user_id == None:
        user_id = request.args.get("user_id")
    if user_id != None and chat_id != None:
        #update of the chat document by adding the user id
        post = db.chats.find_one({'_id': ObjectId(chat_id)})
        if ObjectId(user_id) not in post['users_list']:
            post['users_list'].append(ObjectId(user_id))
        db.chats.update_one({'_id': ObjectId(chat_id)}, {"$set": post},
                            upsert=False)

        #update of the user permissions by adding the chat id
        post = db.users.find_one({'_id': ObjectId(user_id)})
        if ObjectId(chat_id) not in post['chats_list']:
            post['chats_list'].append(ObjectId(chat_id))
        db.users.update_one({'_id': ObjectId(user_id)}, {"$set": post},
                            upsert=False)
    elif not chat_id:
        print("ERROR")
        raise Error404("chat_id not found")
    elif not user_id:
        print("ERROR")
        raise APIError(
            "You should send these query parameters ?user_id=<user_id>")

    return json.dumps({'chat_id': str(chat_id)})
def newuser(username):
    """
    This funct creates new users, incase exists return APIerror.
    """
    if db.User.find_one({"username":username}) == None:
        db.User.insert({"username":username})
        return str(db.User.find_one({"username":username},{"username":0})["_id"])
    raise APIError("Ese nombre de usuario ya esta ocupado. Prueba con otro :) ! Try again /user/create?username=<username>")
Exemple #13
0
def createChat():
    chat = request.args.get("n_chat")
    usuarios = (request.args.getlist('u'))

    # Check if chat and user is not in the data base
    # Do a query of the name of the chat.
    query = list(db.find({"chatname": chat}))
    # Do a query if user exists.
    for e in usuarios:
        #print(e)
        names = list(db.find({"username": e}))
        #print("nombres",len(names))

    # Conditions
    if len(query) > 0:
        raise APIError(
            "Chat already exists in the dabatabase. Please, chose another name or add the users to a existing chat."
        )
        # Do a query if user exists, if does not, raise an error
    else:
        if len(names) == 0:
            raise APIError("User doesn't exist. You need to creat it first.")
        else:
            # Extract the id of the users
            usuarios_id = []
            for e in usuarios:
                #print(e)
                id_u = list(db.find({"username": e}, {"_id": 1}))
                #print(id_u[0])
                usuarios_id.append(id_u[0]["_id"])

            mychat = {
                "type": "chat",
                "chatname": chat,
                "userlist": usuarios_id
            }
            x = db.insert_one(mychat)
            return {
                "status": "New chat created",
                "dbresponse": dumps(x.inserted_id)
            }
Exemple #14
0
def AddMessageChat(chatname, username):
    usernames = db.users.distinct("user_name")  # all usernames that exist
    user_id = db.users.find_one(
        {'user_name': username},
        {'user_id': 1})['user_id']  # get the user_id for the user
    chatnames = db.chats.distinct("chat_name")  # all chatnames that exist
    chat_id = db.chats.find_one(
        {'chat_name': chatname},
        {'chat_id': 1})['chat_id']  # get the chat_id for the chat
    new_messageid = max(
        db.messages.distinct("message_id")) + 1  #create new message_id
    # create param
    messagetext = request.args["messagetext"]

    #Check chatname and  username exist
    if username not in usernames:  #
        print("ERROR")
        raise APIError("I´m sorry this user doesn´t exist.")
    if chatname not in chatnames:
        print("ERROR")
        raise APIError("I´m sorry this chat doesn´t exist.")

    #Check if user_id in chat
    chat_users = db.chats.find({"chat_name": chatname}, {"users": 1})
    if user_id not in [user["users"] for user in chat_users][0]:
        raise APIError("I´m sorry. This user is not in this chat.")

    #Create new message
    message_details = {
        "user_name": username,
        "user_id": user_id,
        "chat_id": chat_id,
        "chat_name": chatname,
        "message_id": new_messageid,
        "message_text": messagetext
    }
    db.messages.insert_one(message_details)
    return dumps(
        f"Success! chat_id:{chat_id},chat_name:{chatname}, user_id:{user_id}, user_name:{username},message_id:{new_messageid},message_text:{messagetext}"
    )
Exemple #15
0
def recomendUser(user_id):
    # Extract the id of the user from chat name
    u_id = list(db.find({"username": user_id}, {"_id": 1}))
    # Check the user exists
    if len(u_id) == 0:
        raise APIError(
            "Username doesn't exists in the dabatabase. Choose another one")
    else:
        # Look for the id in the dict, and then in the db to extract the name
        rec_name = db.find_one({"_id": dic_rec[(u_id[0]["_id"])]})["username"]
        #print(rec_name)

        return {"status": "Todo OK, José Luís", "recommended user": rec_name}
Exemple #16
0
def CreateUser(new_username):
    usernames = (db.users.distinct("user_name")
                 )  # get all usernames that exist
    new_userid = max(db.users.distinct("user_id")) + 1  # Create new user_id
    #Check if username exist
    if new_username in usernames:
        print("ERROR")
        raise APIError(
            "I´m sorry this username already exists. Try again with another username."
        )
    else:  # Create new user
        username_details = {"user_id": new_userid, "user_name": new_username}
        db.users.insert_one(username_details)
        return dumps(f"Sucess! user_id:{new_userid},user_name:{new_username}")
Exemple #17
0
def insertUsername(username):
    # Check that username is not in the db
    # Do a query of the name
    query = list(db.find({"username": f"{username}"}))
    if len(query) > 0:
        print("Error")
        raise APIError("Username exists in the dabatabase. Choose another one")
    else:
        myuser = {"type": "user", "username": f"{username}"}
        x = db.insert_one(myuser)
        return {
            "status": "New user created",
            "dbresponse": dumps(x.inserted_id)
        }
def createchat():
    """    
    Funct creates a chat base of the Participants param, with a groupname.
    """
    team = request.args.getlist('Participants')
    groupname = request.args.get('groupname')
    if db.Conversation.find_one({"name": groupname}) == None:
        db.Conversation.insert({"Participants": team, "name": groupname})
        return str(
            db.Conversation.find_one({"name": groupname}, {
                "Participants": 0,
                "name": 0
            })["_id"])
    raise APIError(
        "Ese nombre de groupname ya esta ocupado. Prueba con otro :) ! ")
Exemple #19
0
def add_user():
    try:
        username = request.args.get("username").lower()
    except:
        raise APIError(
            "Wrong parameters. Try /user/create?username=<username>")
    #This step is neccesary because mongo method insert_one does not manage correctly uniquenness of the username
    user = db['users'].find_one({"username": username}, {
        "_id": 1,
        "username": 1,
        "conversations": 1,
        "messages": 1
    })
    if user != None:
        raise APIError("This user already exists in database")
    try:
        db["users"].insert_one({
            "username": username,
            "conversations": [],
            "messages": []
        })
        return {username: "******"}
    except:
        raise APIError("This user already exists in database")
Exemple #20
0
def create_usr(name):
    us = db.users.find({}, {'name': 1})
    users = [u['name'] for u in us]
    if name in users:
        raise APIError("The user already exist, please use a different name")
    '''
    Insert a new user and check 
    that it has been correctly inserted by returning 
    a find_one. Need to check for repeated users.
    '''
    db.users.insert_one({
        'name': name,
        'created': str(datetime.datetime.now()),
        'chats': []
    })
    check = db.users.find_one({'name': name})
    return {'result': dumps(check)}
Exemple #21
0
def list_messages_of_chat(conversation_name):
    conversation = db['conversations'].find_one(
        {"conversation_name": conversation_name}, {
            "_id": 1,
            "conversation_name": 1,
            "users": 1,
            "messages": 1
        })
    if not conversation:
        raise Error404("Conversation doesn't exist in database")
    pointers = db["messages"].find({"_id": {"$in": conversation["messages"]}})
    if not pointers:
        raise APIError("Conversation is empty")
    list_of_messages = ([{
        sentence['user'][0]: sentence['message']
    } for sentence in pointers])
    return (json.dumps(list_of_messages))
Exemple #22
0
def get_user(name):
    user = db.users.find_one({'name': name}, {'chats': 0})
    if user == None:
        raise APIError(
            "The user does not exist, you can create it by using the endpoint /user/create/<name>"
        )

    chat = db.users.find_one({'name': name}, {'chats': 1, '_id': 0})['chats']

    chats = []
    for e in chat:
        chats.append(
            db.chats.find_one({'_id': e}, {
                'name': 1,
                '_id': 0
            })['name'])

    return {'user': dumps(user), 'user_chats': chats}
Exemple #23
0
def add_message(user):
    chat = request.args['chat']
    text = request.args['text']

    #Check that the user exist
    cur = db.users.find({}, {'name': 1})
    if user not in [n['name'] for n in cur]:
        raise APIError(
            'That user does not exist, you can create it with the endpoint /user/create/<user_name>'
        )

    id_chat = db.chats.find_one({'name': chat}, {'_id': 1})['_id']
    id_user = db.users.find_one({'name': user}, {'_id': 1})['_id']

    #Check that the user exist in the chat and otherwise, include it:
    participants = db.chats.find_one(
        {'$and': [{
            '_id': id_chat
        }, {
            'participants': id_user
        }]})
    if participants == None:
        db.chats.update({'_id': id_chat},
                        {'$addToSet': {
                            'participants': id_user
                        }})
        message = 'We succeded in adding the message and added the user to the chat'
    else:
        message = 'We succeded in adding the message'

    #Insert the message
    db.messages.insert_one({
        'chat': id_chat,
        'user': id_user,
        'time': str(datetime.datetime.now()),
        'text': text
    })
    db.users.update({'_id': id_user}, {'$addToSet': {'chats': id_chat}})

    return {
        'status': message,
        'entry': dumps(db.messages.find_one({'text': text}))
    }
Exemple #24
0
def pepe():
    saludo = request.args.get("saludo")
    if saludo:
        return {"saludo": f"Hola {saludo}"}
    raise APIError("Tienes que mandar un query parameter ?saludo=<tunombre>")
def extractSentiments(chat_name):
    # Check if chat is in the data base
    # Do a query of the name of the chat.
    query = list(db.find({"chatname": chat_name}))
    print(query)
    if len(query) == 0:
        raise APIError(
            "Chat doesn't exists in the dabatabase. Please, chose another name or create the chat."
        )
    else:
        # Extract the id of the chat from chat name
        id_chat = list(db.find({"chatname": chat_name}, {"_id": 1}))[0]
        #print(id_chat[0]["_id"])
        id_chat["_id"]
        # Lok for all the messages in that chat
        messages = list(
            db.find(
                {"$and": [{
                    "type": "message"
                }, {
                    "chat_id": id_chat["_id"]
                }]}))
        #print(messages)
        lista_msg = [e["text"] for e in messages]

        # Analize the message and store in a list
        sentiment_list = [
            SentimentIntensityAnalyzer().polarity_scores(e) for e in lista_msg
        ]

        # Calculate the mean compound of the whole chat
        # Extract the compound component
        compound_list = []
        for e in sentiment_list:
            for k, v in e.items():
                if k == "compound":
                    compound_list.append(v)
        # Calculate the mean
        compound_mean = sum(compound_list) / len(compound_list)
        # Classify the value of compound metric
        sentiment_chat = ""
        if compound_mean >= 0.05:
            sentiment_chat = "Positive"
        elif compound_mean > -0.05 and compound_mean < 0.05:
            sentiment_chat = "Neutral"
        else:
            sentiment_chat = "Negative"

        # Create a dictionary to serve each message and its valoration
        results = []
        for i in range(len(lista_msg)):
            results.append({
                "message": lista_msg[i],
                "polarity": sentiment_list[i]
            })

        return {
            "Chat overall sentiment":
            f"The overall sentiment of the chat is {sentiment_chat} with a mean compound of {compound_mean}",
            "Sentiment Analysis Results": results
        }