File size: 5,757 Bytes
9a56158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e:\\\\bengla text summarization\\\\train-pegasus-model-on-bengali-text-summarization-using-mlops'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataclasses import dataclass\n",
    "from pathlib import Path\n",
    "\n",
    "@dataclass(frozen=True)\n",
    "class BanTokenizationConfig:\n",
    "    root_dir : Path\n",
    "    source_dir : Path\n",
    "    save_dir : Path\n",
    "    output_file : str\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from src.benglasummarization.constants import *\n",
    "from src.benglasummarization.utils.common import  create_directories, read_yaml\n",
    "\n",
    "class ConfigurationManager:\n",
    "    def __init__(\n",
    "        self,\n",
    "        config_filepath = CONFIG_FILE_PATH,\n",
    "        params_filepath = PARAMS_FILE_PATH):\n",
    "\n",
    "        self.config = read_yaml(config_filepath)\n",
    "        self.params = read_yaml(params_filepath)\n",
    "\n",
    "        create_directories([self.config.artifacts_root])\n",
    "\n",
    "    def get_ben_tokenization_config(self) -> BanTokenizationConfig:\n",
    "        config = self.config.ban_tokenization\n",
    "        params = self.params.pre_tokenize\n",
    "        create_directories([config.root_dir])\n",
    "\n",
    "        ben_tokenization_config = BanTokenizationConfig(\n",
    "            root_dir=config.root_dir,\n",
    "            source_dir=config.source_dir,\n",
    "            save_dir= config.save_dir,\n",
    "            output_file= params.output_file\n",
    "        )\n",
    " \n",
    "        return ben_tokenization_config\n",
    "\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pathlib import Path\n",
    "from src.benglasummarization.logging import logger\n",
    "from tqdm.notebook import tqdm\n",
    "\n",
    "class BanTokenization:\n",
    "    def __init__(self, config: BanTokenizationConfig):\n",
    "        self.config = config\n",
    "\n",
    "    def combine_text_columns(self, text_columns=['main']):\n",
    "        df = pd.read_csv(self.config.source_dir)\n",
    "\n",
    "        # Ensure save_dir is a Path object\n",
    "        save_dir = Path(self.config.save_dir)\n",
    "        \n",
    "        # Create the directory if it doesn't exist\n",
    "        save_dir.mkdir(parents=True, exist_ok=True)\n",
    "\n",
    "        # Combine save_dir and output_file to form the output path\n",
    "        output_txt_file = save_dir / self.config.output_file\n",
    "        \n",
    "        # Write the combined text data to the output file\n",
    "        with open(output_txt_file, 'w', encoding='utf-8') as f:\n",
    "            for index, row in tqdm(df.iterrows(), total=len(df)):\n",
    "                combined_text = ' '.join(str(row[col]) for col in text_columns)\n",
    "                f.write(combined_text + '\\n')\n",
    "\n",
    "        # Log the success of the operation\n",
    "        logger.info(f\"All text data has been combined into {output_txt_file}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024-10-16 19:09:09,141: INFO: common: yaml file: config\\config.yaml loaded successfully]\n",
      "[2024-10-16 19:09:09,143: INFO: common: yaml file: params.yaml loaded successfully]\n",
      "[2024-10-16 19:09:09,145: INFO: common: created directory at: artifacts]\n",
      "[2024-10-16 19:09:09,146: INFO: common: created directory at: artifacts/ban_tokenization]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "46422977ab65463695c98b98ece484c2",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/160000 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2024-10-16 19:10:00,660: INFO: 206824922: All text data has been combined into artifacts\\ban_tokenization\\combined_text.txt]\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    config = ConfigurationManager()\n",
    "    prepare_ben_tok_config = config.get_ben_tokenization_config()  \n",
    "    ben_data_tok = BanTokenization(config=prepare_ben_tok_config)\n",
    "    ben_data_tok.combine_text_columns()\n",
    "except Exception as e:\n",
    "    raise e"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}