{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"MongoDB Cluster management\n",
"-----------------------\n",
"\n",
"Let us create some function to send (insert documents) and retrieve collections from our cluster database."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We must import the pymongo the client and the server modules."
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"from pymongo.mongo_client import MongoClient\n",
"from pymongo.server_api import ServerApi\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [],
"source": [
"# let us set below the cluster uri\n",
"uri = \"mongodb+srv://oumar199:Jacksparrow360@woloftranslationcluster.u0gk7.mongodb.net/?retryWrites=true&w=majority\""
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inserting new documents"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us insert new documents to our collection."
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"# we must initialize the client\n",
"client = MongoClient(uri, server_api = ServerApi('1'))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us create a new collection."
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"# create a database\n",
"db = client.get_database('WolofTranslation')"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [],
"source": [
"# create a new collection for the new sentences\n",
"sentences = db.sentences"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"It is time to insert the new documents."
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"# recuperate the already created sentences\n",
"corpora = pd.read_csv('wolof-translate/wolof_translate/data/sentences/wolof_french.csv')"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [],
"source": [
"# let us reset the indices\n",
"corpora.reset_index(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" index | \n",
" french | \n",
" wolof | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" J'arrive tout de suite chez toi. | \n",
" Léegui léegui ma egg sa kër. | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" J'en suis sûr, cette photo ci c'est la photo p... | \n",
" Waaw nataal bii nataal la boob ay nit ñu baree... | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" Je vois devant moi une photo sur laquelle beau... | \n",
" Nataal bii maa ngi ciy janloog haa ay nit yu b... | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" Ceux-ci sont des personnes qui sont sortis pou... | \n",
" Lii, ay nit lañu yu génn di ñaxtu. Jëm yi nag ... | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" Salut ! Ceux-là qui ressemblent à des personne... | \n",
" Salaawaalekum ! Ñii de, mel nañ ne, ay nit ñu ... | \n",
"
\n",
" \n",
" 5 | \n",
" 5 | \n",
" Cette photo ci c'est une photo sur laquelle je... | \n",
" Nataal bi ab nataal la boo xamante yni maa ngi... | \n",
"
\n",
" \n",
" 6 | \n",
" 6 | \n",
" Sur la photo, ont voit des personnes qui se ré... | \n",
" Nataal bii ñoo ngi ciy gis ay nit ñuy ñaxtu wa... | \n",
"
\n",
" \n",
" 7 | \n",
" 7 | \n",
" On voit sur la photo beaucoup de personnes sor... | \n",
" Ñu gis ci nataal bi ay nit ñu bari ñu génn ci ... | \n",
"
\n",
" \n",
" 8 | \n",
" 8 | \n",
" C'est des poissons, oui. Ils sont de couleur b... | \n",
" Jën la waaw, Wu am wirgo Wu baxa ak Wu xonq. | \n",
"
\n",
" \n",
" 9 | \n",
" 9 | \n",
" Ah sur cette photo ci cependant, il y a un poi... | \n",
" Aah nataal bii nag, aw jën la. Jën wi mi ngi a... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" index french \\\n",
"0 0 J'arrive tout de suite chez toi. \n",
"1 1 J'en suis sûr, cette photo ci c'est la photo p... \n",
"2 2 Je vois devant moi une photo sur laquelle beau... \n",
"3 3 Ceux-ci sont des personnes qui sont sortis pou... \n",
"4 4 Salut ! Ceux-là qui ressemblent à des personne... \n",
"5 5 Cette photo ci c'est une photo sur laquelle je... \n",
"6 6 Sur la photo, ont voit des personnes qui se ré... \n",
"7 7 On voit sur la photo beaucoup de personnes sor... \n",
"8 8 C'est des poissons, oui. Ils sont de couleur b... \n",
"9 9 Ah sur cette photo ci cependant, il y a un poi... \n",
"\n",
" wolof \n",
"0 Léegui léegui ma egg sa kër. \n",
"1 Waaw nataal bii nataal la boob ay nit ñu baree... \n",
"2 Nataal bii maa ngi ciy janloog haa ay nit yu b... \n",
"3 Lii, ay nit lañu yu génn di ñaxtu. Jëm yi nag ... \n",
"4 Salaawaalekum ! Ñii de, mel nañ ne, ay nit ñu ... \n",
"5 Nataal bi ab nataal la boo xamante yni maa ngi... \n",
"6 Nataal bii ñoo ngi ciy gis ay nit ñuy ñaxtu wa... \n",
"7 Ñu gis ci nataal bi ay nit ñu bari ñu génn ci ... \n",
"8 Jën la waaw, Wu am wirgo Wu baxa ak Wu xonq. \n",
"9 Aah nataal bii nag, aw jën la. Jën wi mi ngi a... "
]
},
"execution_count": 108,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"corpora.head(10)"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"# insert the sentences\n",
"results = sentences.insert_many({\n",
" '_id': corp, # set the id\n",
" 'french': corpora.loc[corp, 'french'],\n",
" 'wolof': corpora.loc[corp, 'wolof']\n",
" } for corp in corpora.index\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us insert the deleted sentences."
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [],
"source": [
"# create a new collection named deleted.\n",
"deleted = db.deleted"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [],
"source": [
"# recuperated the data frame of deleted sentences\n",
"del_corpora = pd.read_csv('wolof-translate/wolof_translate/data/sentences/deleted_lines.csv')"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [],
"source": [
"# reset the indices\n",
"del_corpora.reset_index(inplace=True)"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [],
"source": [
"# insert the deleted sentences\n",
"results = deleted.insert_many({\n",
" '_id': corp, # set the id\n",
" 'french': corpora.loc[corp, 'french'],\n",
" 'wolof': corpora.loc[corp, 'wolof']\n",
" } for corp in del_corpora.index\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Modify sentences"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We want to modify only one sentence at a time."
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [],
"source": [
"# select the id to modify\n",
"id_ = 1\n",
"\n",
"# retrieve new sentences\n",
"french = corpora.loc[id_, 'french']\n",
"wolof = corpora.loc[id_, 'wolof']"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(\"J'en suis sûr, cette photo ci c'est la photo pris au moment où plusieurs personnes font une marche de révolte tendant leurs mains. Ceux là sont assis, ceux là sont debout entrain de marcher. On a écrit sur la photo quelque chose de bleu concernant la Casamance.\",\n",
" 'Waaw nataal bii nataal la boob ay nit ñu baree bari ñoo xam ni dañuy doxub ñaxtu ñoo ci nekk tàllal seen i loxo. Ñee sukku ñeel taxaw jodd di dox. Ñu bind ci kaw nataal bi lu xaw a baxa la bind ci laa kaasamãs.')"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# print the sentences\n",
"french, wolof"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [],
"source": [
"# modify the sentences at the id\n",
"results = sentences.update_one(\n",
" {\n",
" '_id': {'$eq': id_}\n",
" },\n",
" {\n",
" '$set': {\n",
" 'french': french,\n",
" 'wolof': wolof + \"--------\" # we added a modification\n",
" }\n",
" }\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us show the first documents."
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" _id | \n",
" french | \n",
" wolof | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0 | \n",
" J'arrive tout de suite chez toi. | \n",
" Léegui léegui ma egg sa kër. | \n",
"
\n",
" \n",
" 1 | \n",
" 1 | \n",
" J'en suis sûr, cette photo ci c'est la photo p... | \n",
" Waaw nataal bii nataal la boob ay nit ñu baree... | \n",
"
\n",
" \n",
" 2 | \n",
" 2 | \n",
" Je vois devant moi une photo sur laquelle beau... | \n",
" Nataal bii maa ngi ciy janloog haa ay nit yu b... | \n",
"
\n",
" \n",
" 3 | \n",
" 3 | \n",
" Ceux-ci sont des personnes qui sont sortis pou... | \n",
" Lii, ay nit lañu yu génn di ñaxtu. Jëm yi nag ... | \n",
"
\n",
" \n",
" 4 | \n",
" 4 | \n",
" Salut ! Ceux-là qui ressemblent à des personne... | \n",
" Salaawaalekum ! Ñii de, mel nañ ne, ay nit ñu ... | \n",
"
\n",
" \n",
" 5 | \n",
" 5 | \n",
" Cette photo ci c'est une photo sur laquelle je... | \n",
" Nataal bi ab nataal la boo xamante yni maa ngi... | \n",
"
\n",
" \n",
" 6 | \n",
" 6 | \n",
" Sur la photo, ont voit des personnes qui se ré... | \n",
" Nataal bii ñoo ngi ciy gis ay nit ñuy ñaxtu wa... | \n",
"
\n",
" \n",
" 7 | \n",
" 7 | \n",
" On voit sur la photo beaucoup de personnes sor... | \n",
" Ñu gis ci nataal bi ay nit ñu bari ñu génn ci ... | \n",
"
\n",
" \n",
" 8 | \n",
" 8 | \n",
" C'est des poissons, oui. Ils sont de couleur b... | \n",
" Jën la waaw, Wu am wirgo Wu baxa ak Wu xonq. | \n",
"
\n",
" \n",
" 9 | \n",
" 9 | \n",
" Ah sur cette photo ci cependant, il y a un poi... | \n",
" Aah nataal bii nag, aw jën la. Jën wi mi ngi a... | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" _id french \\\n",
"0 0 J'arrive tout de suite chez toi. \n",
"1 1 J'en suis sûr, cette photo ci c'est la photo p... \n",
"2 2 Je vois devant moi une photo sur laquelle beau... \n",
"3 3 Ceux-ci sont des personnes qui sont sortis pou... \n",
"4 4 Salut ! Ceux-là qui ressemblent à des personne... \n",
"5 5 Cette photo ci c'est une photo sur laquelle je... \n",
"6 6 Sur la photo, ont voit des personnes qui se ré... \n",
"7 7 On voit sur la photo beaucoup de personnes sor... \n",
"8 8 C'est des poissons, oui. Ils sont de couleur b... \n",
"9 9 Ah sur cette photo ci cependant, il y a un poi... \n",
"\n",
" wolof \n",
"0 Léegui léegui ma egg sa kër. \n",
"1 Waaw nataal bii nataal la boob ay nit ñu baree... \n",
"2 Nataal bii maa ngi ciy janloog haa ay nit yu b... \n",
"3 Lii, ay nit lañu yu génn di ñaxtu. Jëm yi nag ... \n",
"4 Salaawaalekum ! Ñii de, mel nañ ne, ay nit ñu ... \n",
"5 Nataal bi ab nataal la boo xamante yni maa ngi... \n",
"6 Nataal bii ñoo ngi ciy gis ay nit ñuy ñaxtu wa... \n",
"7 Ñu gis ci nataal bi ay nit ñu bari ñu génn ci ... \n",
"8 Jën la waaw, Wu am wirgo Wu baxa ak Wu xonq. \n",
"9 Aah nataal bii nag, aw jën la. Jën wi mi ngi a... "
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# get the 10 first sentences into a Data Frame\n",
"pd.DataFrame(list(sentences.find().limit(10)))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Delete sentences"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We want to modify only one sentence at a time. The deleted sentences must be added into the 'deleted' collection. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# recuperate the sentences to delete (id = 0)\n",
"id_ = 0\n",
"\n",
"del_sent = sentences.find_one(\n",
" {\n",
" '_id': {'$eq': id_}\n",
" } \n",
")\n",
"\n",
"# delete the sentence and add it into the deleted sentences\n",
"sentences.delete_one(\n",
" {\n",
" '_id': {'$eq': del_sent['_id']}\n",
" }\n",
")\n",
"\n",
"results = deleted.insert_one(\n",
" {\n",
" '_id': len(list(deleted.find())),\n",
" 'french': del_sent['french'],\n",
" 'wolof': del_sent['wolof']\n",
" }\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### From collection to DataFrame"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"We must convert the sentences to csv files in order to use at the training step."
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [],
"source": [
"# recuperate the new corpora\n",
"new_corpora = pd.DataFrame(list(sentences.find()))\n",
"\n",
"# recuperate the deleted sentences as a Data Frame\n",
"deleted_df = pd.DataFrame(list(deleted.find()))\n",
"\n",
"# save the data frames as csv files\n",
"new_corpora.set_index('_id', inplace=True)\n",
"\n",
"deleted_df.set_index('_id', inplace=True)\n",
"\n",
"new_corpora.to_csv('wolof-translate/wolof_translate/data/sentences/wolof_french.csv', index=False)\n",
"\n",
"deleted_df.to_csv('wolof-translate/wolof_translate/data/sentences/deleted_lines.csv', index=False)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### All in one"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Let us create a class which consider each of the methods we investigated previously."
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'_id': 150,\n",
" 'french': \"Sur la photo que vous m'avez envoyée, j'ai vu qu'il s'agissait de gendarmes. Des gendarmes qui portent, cependant, des... des boucliers. Des verres conçus pour les protéger. Ils sont faces au peuple s'échangeant contre eux des cailloux et des pierres.\",\n",
" 'wolof': 'Nataal bi ngeen ma yonnee, gis naa ni ay takk-der la. Takk der yoo xamantane bii nag, jël nañ loo xamantane bii mooy ay,... Ay baar. Ay verre yoo xam ne dañ kaa defar pour ñu leen di baare, ñu jàkkarlook askan wi di sànnanteek ñoom ay xeer ak ay doj.'}"
]
},
"execution_count": 129,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(sentences.find().sort('_id', -1).limit(1))[0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting wolof_translate/utils/database_manager.py\n"
]
}
],
"source": [
"%%writefile wolof_translate/utils/database_manager.py\n",
"from pymongo.mongo_client import MongoClient\n",
"from pymongo.server_api import ServerApi\n",
"import pandas as pd\n",
"\n",
"class TranslationMongoDBManager:\n",
" \n",
" def __init__(self, uri: str, database: str):\n",
" \n",
" # recuperate the client\n",
" self.client = MongoClient(uri)\n",
" \n",
" # recuperate the database\n",
" self.db = self.client.get_database(database)\n",
" \n",
" def insert_documents(self, documents: list, collection: str = \"sentences\"):\n",
" \n",
" # insert documents inside a collection\n",
" results = self.db[collection].insert_many(documents)\n",
" \n",
" return results\n",
" \n",
" def insert_document(self, document: dict, collection: str = \"sentences\"):\n",
" \n",
" assert not '_id' in document\n",
" \n",
" # get the id of the last sentence (recuperate the max id and add 1 to it)\n",
" max_id = self.get_max_id(collection)\n",
" \n",
" # add the new sentences\n",
" document['_id'] = max_id + 1\n",
" \n",
" results = self.db[collection].insert_one(\n",
" document\n",
" )\n",
" \n",
" return results\n",
" \n",
" def update_document(self, id: int, collection: str = \"sentences\", update_collection: str = \"updated\"):\n",
" \n",
" # recuperate the document to update\n",
" upd_sent = self.db[collection].find_one(\n",
" {\n",
" '_id': {\n",
" '$eq': id\n",
" }\n",
" }\n",
" )\n",
" \n",
" # delete the document\n",
" self.db[collection].delete_one(\n",
" {\n",
" '_id': {'$eq': upd_sent['_id']}\n",
" }\n",
" )\n",
" \n",
" # add the sentences to the deleted sentences\n",
" upd_sent['_id'] = len(list(self.db[update_collection].find()))\n",
" \n",
" results = self.db[update_collection].insert_one(\n",
" upd_sent\n",
" )\n",
" \n",
" return results\n",
" \n",
" def delete_document(self, id: int, collection: str = \"sentences\", del_collection: str = \"deleted\"):\n",
" \n",
" # recuperate the document to delete\n",
" del_sent = self.db[collection].find_one(\n",
" {\n",
" '_id': {\n",
" '$eq': id\n",
" }\n",
" }\n",
" )\n",
" \n",
" # delete the sentence\n",
" self.db[collection].delete_one(\n",
" {\n",
" '_id': {'$eq': del_sent['_id']}\n",
" }\n",
" )\n",
" \n",
" # add the sentences to the deleted sentences\n",
" del_sent['_id'] = len(list(self.db[del_collection].find()))\n",
" \n",
" results = self.db[del_collection].insert_one(\n",
" del_sent\n",
" )\n",
" \n",
" return results\n",
" \n",
" def get_max_id(self, collection: str = \"sentences\"):\n",
" \n",
" # recuperate the maximum id\n",
" id = list(self.db[collection].find().sort('_id', -1).limit(1))[0]['_id']\n",
" \n",
" return id\n",
" \n",
" def save_data_frames(self, sentences_path: str, deleted_path: str, collection: str = \"sentences\", del_collection: str = \"deleted\"):\n",
" \n",
" # recuperate the new corpora\n",
" new_corpora = pd.DataFrame(list(self.db[collection].find()))\n",
"\n",
" # recuperate the deleted sentences as a Data Frame\n",
" deleted_df = pd.DataFrame(list(self.db[del_collection].find()))\n",
"\n",
" # save the data frames as csv files\n",
" new_corpora.set_index('_id', inplace=True)\n",
"\n",
" deleted_df.set_index('_id', inplace=True)\n",
"\n",
" new_corpora.to_csv(sentences_path, index=False)\n",
"\n",
" deleted_df.to_csv(deleted_path, index=False)\n",
" \n",
" def load_data_frames(self, collection: str = \"sentences\", del_collection: str = \"deleted\"):\n",
" \n",
" # recuperate the new corpora\n",
" new_corpora = pd.DataFrame(list(self.db[collection].find()))\n",
"\n",
" # recuperate the deleted sentences as a Data Frame\n",
" deleted_df = pd.DataFrame(list(self.db[del_collection].find()))\n",
" \n",
" return new_corpora, deleted_df"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "pytorch1-HleOW5am-py3.10",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.8"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}