def addUserToChat(chat_id): ''' Adds a user to a chat (just in case you want to add more users to a chat after it's creation) input: the user's id (dic): e.g. {'userId': 0} output: user_id, chat_id ''' db, coll = connectCollection('chats', 'chats') data = list(coll.find({'idChat': int(chat_id)})) # Checking if chat exists: if len(data) == 0: error = "Sorry, this chat doesn't exist. You must create it first." return {'Exception': error} user = int(request.forms.get('userId')) db2, coll2 = connectCollection('chats', 'users') usersData = list(coll2.aggregate([{'$project': {'idUser': 1}}])) # Checking if user exists: if user not in [e['idUser'] for e in usersData]: error = 'Sorry, the user you are trying to include does not exist in the database. You must create it first.' return {'Exception': error} users = [ int(e) for e in re.sub('\[|\]', '', data[0]['users']).split(', ') ] # Checking if user is already part of the chat: if user in users: error = 'Sorry, this user is already part of the chat' return {'Exception': error} users.append(user) # Adding the new user to the chat: coll.update_one({'idChat': int(chat_id)}, {'$set': { 'users': str(users) }}) return {'UserId': user, 'ChatId': int(chat_id)}
def createChat(): ''' Creates a conversation to load messages input: A dictionary containing an array of user ids: e.g. {'users': '[8, 9, 10]'} output: chat_id ''' users = str(request.forms.get('users')) print(users) db, coll = connectCollection('chats', 'chats') data = list(coll.aggregate([{'$project': {'idChat': 1}}])) newChat = { 'idChat': max([e['idChat'] for e in data]) + 1, 'users': users } print(newChat) # Checking if users exist in the database db2, coll2 = connectCollection('chats', 'users') usersData = list(coll2.aggregate([{'$project': {'idUser': 1}}])) chatUsers = [ int(e) for e in re.sub('\[|\]', '', newChat['users']).split(', ') ] for e in chatUsers: if e not in [f['idUser'] for f in usersData]: error = 'Sorry, one or more of the users you are trying to include do not exist in the database. You must create them first.' return {'Exception': error} # Inserting new chat to the database: chat_id = coll.insert_one(newChat).inserted_id return {'ObjectId': str(chat_id), 'ChatId': newChat['idChat']}
def addMessageToChat(chat_id): ''' Adds a message to an existing chat input: chat_id / user_id and message as dict: e.g. {'user':0, 'message':'Hi!'} output: message_id ''' db, coll = connectCollection('chats', 'messages_linked') data = list(coll.find({'idChat': int(chat_id)})) user = int(request.forms.get('user')) message = str(request.forms.get('message')) # Checking if the chat exists db2, coll2 = connectCollection('chats', 'chats') chat = list(coll2.find({'idChat': int(chat_id)})) if len(chat) == 0: error = "Sorry, this chat doesn't exist. You must create it first." return {'Exception': error} # Checking that the incoming user is part of this chat before adding the chat message to the database. chatUsers = [ int(e) for e in re.sub('\[|\]', '', chat[0]['users']).split(', ') ] if user not in chatUsers: error = 'Sorry, this user is not part of the chat. You must add him/her first.' return {'Exception': error} # Adding the new message: db3, coll3 = connectCollection('chats', 'users') selectedUser = list(coll3.find({'idUser': user})) if len(data) == 0: newMessageId = 0 else: newMessageId = max([e['idMessage'] for e in data]) + 1 newMessage = { 'datetime': datetime.datetime.utcnow(), 'idChat': int(chat_id), 'idMessage': newMessageId, 'idUser': user, 'text': message, 'userName': selectedUser[0]['userName'], 'user_id': selectedUser[0]['_id'] } message_id = coll.insert_one(newMessage).inserted_id return { 'ObjectId': str(message_id), 'MessageId': newMessage['idMessage'] }
def recommendUsers(user_id): ''' Recommends a friend to a user based on chat contents input: user_id output: json array with top 3 similar users ''' db, coll = connectCollection('chats', 'messages_linked') data = list(coll.find({})) # Checking if the user exists in the database: if int(user_id) not in list(set([e['idUser'] for e in data])): error = 'Sorry, this user does not exist in the database' return {'Exception': error} tokenizer = RegexpTokenizer(r'\w+') stop_words = set(stopwords.words('english')) TokensDict = {} for userId in list(set([e['idUser'] for e in data])): usersData = list(coll.find({'idUser': userId})) usersTokens = [tokenizer.tokenize(e['text']) for e in usersData] usersTokens_clean = [ word for message in usersTokens for word in message if word not in stop_words ] TokensDict[f'{userId}'] = ' '.join(usersTokens_clean) sim_df = similarityDF(TokensDict) recommendedIds = [ int(e) for e in list(sim_df[str(user_id)].sort_values( ascending=False)[0:3].index) ] db2, coll2 = connectCollection('chats', 'users') recommendedNames = [] for e in recommendedIds: userInfo = list(coll2.find({'idUser': e})) recommendedNames.append(userInfo[0]['userName']) return { 'recommended_users_ids': recommendedIds, 'recommended_users_names': recommendedNames }
def createUser(): ''' Creates a user and saves it into the database input: the user's name (dic): e.g. {'username': '******'} output: user_id ''' username = str(request.forms.get('username')) db, coll = connectCollection('chats', 'users') data = list(coll.aggregate([{'$project': {'idUser': 1}}])) newUser = { 'idUser': max([e['idUser'] for e in data]) + 1, 'userName': username } # Checking that the given username does not already exist in the database: takenNames = list(coll.aggregate([{'$project': {'userName': 1}}])) if newUser['userName'] in [e['userName'] for e in takenNames]: error = 'Sorry, there is already a user with that exact name in the database' return {'Exception': error} else: # Inserting new user to the database: user_id = coll.insert_one(newUser).inserted_id return {'ObjectId': str(user_id), 'UserId': newUser['idUser']}
def getMessages(chat_id): ''' Get all messages from 'chat_id' input: chat_id output: json array with all messages from this chat_id ''' db, coll = connectCollection('chats', 'messages_linked') data = list(coll.find({'idChat': int(chat_id)})) messages = {} for index, dictionary in enumerate(data): index += 1 messages[f'message_{index}'] = { 'user': dictionary['userName'], 'date': str(dictionary['datetime'])[0:10], 'time': str(dictionary['datetime'])[11:19], 'text': dictionary['text'] } if len(messages) == 0: error = 'Sorry, this chat does not exist in the database' return {'Exception': error} else: return messages
#!/usr/bin/python3 from pymongo import MongoClient, ASCENDING from bottle import request, response, post, get, run, route from bson.json_util import dumps import requests import os import json import datetime from src.mongo import connectCollection, stop_existence, stop_not_existence, get_name from src.nltk import analyzer, get_text from src.recommender import get_users_mod, get_messages_user, similarities_matrix, recommendations # collections db, chats = connectCollection('API', 'chats') db, users = connectCollection('API', 'users') db, messages = connectCollection('API', 'messages') @route('/') def main(): return 'Welcome to my first API! Check this project in: https://github.com/jgph91/API_project' # users endpoints @post('/users/create') def user_creator(): '''Create a user and save into DB''' #text entered via params dict(request.forms)
def main(): # Importing original messages data to MongoDB: db, coll = connectCollection('chats','messages') with open('./input/original.json', encoding="utf8") as f: chats_json = json.load(f) coll.insert_many(chats_json) # Users collection: df = pd.read_json('./input/original.json') userDF = df[['idUser','userName']] userDF.drop_duplicates(inplace=True) coll2 = db['users'] coll2.insert_many(userDF.to_dict('records')) # userDF.to_json('./input/users.json', orient="records") # Adding users_id to messages collection: db, coll = connectCollection('chats','users') data = list(coll.find({})) user_id_list = [] for i in range(len(df)): for e in data: if df.at[i,'idUser'] == e['idUser']: user_id_list.append(e['_id']) df['user_id'] = user_id_list coll3 = db['messages_linked'] coll3.insert_many(df.to_dict('records')) # Chats collection: chatsDF = df[['idChat', 'idUser']] chatsDF.drop_duplicates(inplace=True) chatsDF.reset_index(drop=True, inplace=True) uniqueChats = list(set(chatsDF['idChat'])) users_total = [] for e in uniqueChats: users = [] for i in range(len(chatsDF)): if chatsDF.at[i,'idChat'] == e: users.append(chatsDF.at[i,'idUser']) users_total.append(str(users)) chatsDF = pd.DataFrame(uniqueChats, columns=['idChat']) chatsDF['users'] = users_total coll4 = db['chats'] coll4.insert_many(chatsDF.to_dict('records')) # chatsDF.to_json('./input/chatsID.json', orient="records") # Backup of collections: db, coll = connectCollection('chats','messages_linked') data = list(coll.find({})) with open('./input/messages.json', 'w') as file: json.dump(data, file, default=json_util.default) db, coll = connectCollection('chats','users') data = list(coll.find({})) with open('./input/users.json', 'w') as file: json.dump(data, file, default=json_util.default) db, coll = connectCollection('chats','chats') data = list(coll.find({})) with open('./input/chats.json', 'w') as file: json.dump(data, file, default=json_util.default)
from sklearn.feature_extraction.text import CountVectorizer from sklearn.metrics.pairwise import cosine_similarity as distance from bottle import route, run, get, post, request, template from nltk.tokenize import RegexpTokenizer from nltk.corpus import stopwords from bson.json_util import dumps from dotenv import load_dotenv nltk.download('stopwords') nltk.download('punkt') nltk.download('vader_lexicon') load_dotenv() db, coll = connectCollection('api-project', 'chats') db, users = connectCollection('api-project', 'users') db, chatsCR = connectCollection('api-project', 'chatsCR') def main(): @get("/") def home(): return template('info', title="WELCOME TO CHAT ANALYSIS API") @get('/user/create') def insert_name(): return template('users') @post("/user/create") def createUser():
from nltk import download from nltk.sentiment.vader import SentimentIntensityAnalyzer from pymongo import MongoClient, ASCENDING from src.mongo import connectCollection # collections db, messages = connectCollection('API', 'messages') def get_text(id, field): '''Returns a string with the text of all the message from the specified chat''' id = int(id) text_messages = messages.find({field: id}) text = '' for e in text_messages: text += e['text'] return text def analyzer(text): '''Rates the specified text''' download('vader_lexicon') mood = SentimentIntensityAnalyzer().polarity_scores(text) return mood