Spaces:
Sleeping
Sleeping
File size: 3,825 Bytes
9b14109 2cb4802 9b14109 df1b485 9b14109 8bda43d 2cb4802 9b14109 8bda43d 9b14109 30bb00b 9b14109 2cb4802 9b14109 2cb4802 9b14109 2cb4802 9b14109 96c8541 9b14109 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import pandas as pd
class TranslationMongoDBManager:
def __init__(self, uri: str, database: str):
# recuperate the client
self.client = MongoClient(uri)
# recuperate the database
self.db = self.client.get_database(database)
def insert_documents(self, documents: list, collection: str = "sentences"):
# insert documents inside a collection
results = self.db[collection].insert_many(documents)
return results
def insert_document(self, document: dict, collection: str = "sentences"):
assert not '_id' in document
# get the id of the last sentence (recuperate the max id and add 1 to it)
max_id = self.get_max_id(collection)
# add the new sentences
document['_id'] = max_id + 1
results = self.db[collection].insert_one(
document
)
return results
def update_document(self, id: int, collection: str = "sentences", update_collection: str = "updated"):
# recuperate the document to update
upd_sent = self.db[collection].find_one(
{
'_id': {
'$eq': id
}
}
)
# delete the document
self.db[collection].delete_one(
{
'_id': {'$eq': upd_sent['_id']}
}
)
# add the sentences to the deleted sentences
upd_sent['_id'] = len(list(self.db[update_collection].find()))
results = self.db[update_collection].insert_one(
upd_sent
)
return results
def delete_document(self, id: int, collection: str = "sentences", del_collection: str = "deleted"):
# recuperate the document to delete
del_sent = self.db[collection].find_one(
{
'_id': {
'$eq': id
}
}
)
# delete the sentence
self.db[collection].delete_one(
{
'_id': {'$eq': del_sent['_id']}
}
)
# add the sentences to the deleted sentences
del_sent['_id'] = len(list(self.db[del_collection].find()))
results = self.db[del_collection].insert_one(
del_sent
)
return results
def get_max_id(self, collection: str = "sentences"):
# recuperate the maximum id
id = list(self.db[collection].find().sort('_id', -1).limit(1))[0]['_id']
return id
def save_data_frames(self, sentences_path: str, deleted_path: str, collection: str = "sentences", del_collection: str = "deleted"):
# recuperate the new corpora
new_corpora = pd.DataFrame(list(self.db[collection].find()))
# recuperate the deleted sentences as a Data Frame
deleted_df = pd.DataFrame(list(self.db[del_collection].find()))
# save the data frames as csv files
new_corpora.set_index('_id', inplace=True)
deleted_df.set_index('_id', inplace=True)
new_corpora.to_csv(sentences_path, index=False)
deleted_df.to_csv(deleted_path, index=False)
def load_data_frames(self, collection: str = "sentences", del_collection: str = "deleted"):
# recuperate the new corpora
new_corpora = pd.DataFrame(list(self.db[collection].find()))
# recuperate the deleted sentences as a Data Frame
deleted_df = pd.DataFrame(list(self.db[del_collection].find()))
return new_corpora, deleted_df
|