File size: 12,617 Bytes
ff740ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: python-docx in c:\\users\\dell\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\\localcache\\local-packages\\python310\\site-packages (1.1.2)\n",
      "Requirement already satisfied: lxml>=3.1.0 in c:\\users\\dell\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\\localcache\\local-packages\\python310\\site-packages (from python-docx) (5.3.0)\n",
      "Requirement already satisfied: typing-extensions>=4.9.0 in c:\\users\\dell\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\\localcache\\local-packages\\python310\\site-packages (from python-docx) (4.12.2)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 23.0.1 -> 25.0.1\n",
      "[notice] To update, run: C:\\Users\\Dell\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe -m pip install --upgrade pip\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CSV file created successfully: devdas1-hi-bn.csv\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "import re\n",
    "!pip install python-docx\n",
    "import docx\n",
    "from docx import Document\n",
    "\n",
    "def read_write_csv_from_docx(docx_file1, docx_file2, output_file):\n",
    "    \"\"\"\n",
    "    Reads two .docx files, splits lines using '।' and '?' as delimiters, \n",
    "    escapes quotes properly, and writes their contents into a CSV file with UTF-8 BOM.\n",
    "    \"\"\"\n",
    "    def extract_text_from_docx(docx_file):\n",
    "        \"\"\"\n",
    "        Extracts text from a .docx file and returns it as a single string.\n",
    "        \"\"\"\n",
    "        document = Document(docx_file)\n",
    "        text = []\n",
    "        for paragraph in document.paragraphs:\n",
    "            text.append(paragraph.text)\n",
    "        return \" \".join(text)\n",
    "\n",
    "    try:\n",
    "        # Define the delimiters\n",
    "        delimiters = r\"[।?]\"\n",
    "\n",
    "        # Extract and split content from the first .docx file\n",
    "        content1 = extract_text_from_docx(docx_file1)\n",
    "        lines1 = re.split(delimiters, content1.strip())\n",
    "\n",
    "        # Extract and split content from the second .docx file\n",
    "        content2 = extract_text_from_docx(docx_file2)\n",
    "        lines2 = re.split(delimiters, content2.strip())\n",
    "\n",
    "        # Open the CSV file for writing with UTF-8 BOM\n",
    "        with open(output_file, 'w', encoding='utf-8-sig', newline='') as csv_file:\n",
    "            writer = csv.writer(csv_file, quoting=csv.QUOTE_MINIMAL)\n",
    "\n",
    "            # Write the header\n",
    "            writer.writerow([\"Hindi Text\", \"Bengali Text\"])\n",
    "\n",
    "            # Write the content line by line\n",
    "            for line1, line2 in zip(lines1, lines2):\n",
    "                writer.writerow([line1.strip().replace('\"', '\"\"'), line2.strip().replace('\"', '\"\"')])\n",
    "\n",
    "            # Handle mismatched lines\n",
    "            if len(lines1) > len(lines2):\n",
    "                for line1 in lines1[len(lines2):]:\n",
    "                    writer.writerow([line1.strip().replace('\"', '\"\"'), \"N/A\"])\n",
    "            elif len(lines2) > len(lines1):\n",
    "                for line2 in lines2[len(lines1):]:\n",
    "                    writer.writerow([\"N/A\", line2.strip().replace('\"', '\"\"')])\n",
    "\n",
    "        print(f\"CSV file created successfully: {output_file}\")\n",
    "\n",
    "    except FileNotFoundError:\n",
    "        print(\"Error: One or both .docx files not found.\")\n",
    "    except Exception as e:\n",
    "        print(f\"An error occurred: {e}\")\n",
    "\n",
    "# Example usage\n",
    "docx_file1 = \"devdas-hi.docx\"\n",
    "docx_file2 = \"devdas-bn.docx\"\n",
    "output_file = \"devdas1-hi-bn.csv\"\n",
    "\n",
    "read_write_csv_from_docx(docx_file1, docx_file2, output_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: python-docx in c:\\users\\dell\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\\localcache\\local-packages\\python310\\site-packages (1.1.2)\n",
      "Requirement already satisfied: lxml>=3.1.0 in c:\\users\\dell\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\\localcache\\local-packages\\python310\\site-packages (from python-docx) (5.3.0)\n",
      "Requirement already satisfied: typing-extensions>=4.9.0 in c:\\users\\dell\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.10_qbz5n2kfra8p0\\localcache\\local-packages\\python310\\site-packages (from python-docx) (4.12.2)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip is available: 23.0.1 -> 25.0.1\n",
      "[notice] To update, run: C:\\Users\\Dell\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe -m pip install --upgrade pip\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CSV file created successfully: Ati-hi-tel.csv\n"
     ]
    }
   ],
   "source": [
    "import csv\n",
    "import re\n",
    "!pip install python-docx\n",
    "import docx\n",
    "from docx import Document\n",
    "\n",
    "def read_write_csv_from_docx(docx_file1, docx_file2, output_file):\n",
    "    \"\"\"\n",
    "    Reads two .docx files, splits lines using '।' and '?' as delimiters, \n",
    "    escapes quotes properly, and writes their contents into a CSV file with UTF-8 BOM.\n",
    "    \"\"\"\n",
    "    def extract_text_from_docx(docx_file):\n",
    "        \"\"\"\n",
    "        Extracts text from a .docx file and returns it as a single string.\n",
    "        \"\"\"\n",
    "        document = Document(docx_file)\n",
    "        text = []\n",
    "        for paragraph in document.paragraphs:\n",
    "            text.append(paragraph.text)\n",
    "        return \" \".join(text)\n",
    "\n",
    "    try:\n",
    "        # Define the delimiters\n",
    "        delimiters = r\"[.?]\"\n",
    "\n",
    "        # Extract and split content from the first .docx file\n",
    "        content1 = extract_text_from_docx(docx_file1)\n",
    "        lines1 = re.split(delimiters, content1.strip())\n",
    "\n",
    "        # Extract and split content from the second .docx file\n",
    "        content2 = extract_text_from_docx(docx_file2)\n",
    "        lines2 = re.split(delimiters, content2.strip())\n",
    "\n",
    "        # Open the CSV file for writing with UTF-8 BOM\n",
    "        with open(output_file, 'w', encoding='utf-8-sig', newline='') as csv_file:\n",
    "            writer = csv.writer(csv_file, quoting=csv.QUOTE_MINIMAL)\n",
    "\n",
    "            # Write the header\n",
    "            writer.writerow([\"Hindi Text\", \"Telugu Text\"])\n",
    "\n",
    "            # Write the content line by line\n",
    "            for line1, line2 in zip(lines1, lines2):\n",
    "                writer.writerow([line1.strip().replace('\"', '\"\"'), line2.strip().replace('\"', '\"\"')])\n",
    "\n",
    "            # Handle mismatched lines\n",
    "            if len(lines1) > len(lines2):\n",
    "                for line1 in lines1[len(lines2):]:\n",
    "                    writer.writerow([line1.strip().replace('\"', '\"\"'), \"N/A\"])\n",
    "            elif len(lines2) > len(lines1):\n",
    "                for line2 in lines2[len(lines1):]:\n",
    "                    writer.writerow([\"N/A\", line2.strip().replace('\"', '\"\"')])\n",
    "\n",
    "        print(f\"CSV file created successfully: {output_file}\")\n",
    "\n",
    "    except FileNotFoundError:\n",
    "        print(\"Error: One or both .docx files not found.\")\n",
    "    except Exception as e:\n",
    "        print(f\"An error occurred: {e}\")\n",
    "\n",
    "# Example usage\n",
    "docx_file1 = \"Ati_hi.docx\"\n",
    "docx_file2 = \"Ati_tel.docx\"\n",
    "output_file = \"Ati-hi-tel.csv\"\n",
    "\n",
    "read_write_csv_from_docx(docx_file1, docx_file2, output_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "\n",
    "def read_sentences_from_csv(csv_file, column_name):\n",
    "    \"\"\"\n",
    "    Reads sentences from a CSV file under a specific column.\n",
    "    \"\"\"\n",
    "    sentences = []\n",
    "    with open(csv_file, 'r', encoding='utf-8') as file:\n",
    "        reader = csv.DictReader(file)\n",
    "        for row in reader:\n",
    "            sentences.append(row[column_name])\n",
    "    return sentences\n",
    "\n",
    "def split_telugu_sentences(telugu_sentences):\n",
    "    \"\"\"\n",
    "    Splits Telugu sentences using '.' as the delimiter.\n",
    "    \"\"\"\n",
    "    split_sentences = []\n",
    "    for sentence in telugu_sentences:\n",
    "        # Split using '.' and remove leading/trailing whitespace\n",
    "        parts = [part.strip() for part in sentence.split('.') if part.strip()]\n",
    "        split_sentences.extend(parts)\n",
    "    return split_sentences\n",
    "\n",
    "def create_parallel_csv(hindi_sentences, telugu_sentences, output_file):\n",
    "    \"\"\"\n",
    "    Creates a 2-column CSV file with Hindi and Telugu sentences.\n",
    "    \"\"\"\n",
    "    with open(output_file, 'w', encoding='utf-8-sig', newline='') as csvfile:\n",
    "        writer = csv.writer(csvfile)\n",
    "        # Write header\n",
    "        writer.writerow(['Hindi Sentence', 'Telugu Sentence'])\n",
    "        # Write sentence pairs\n",
    "        for hindi, telugu in zip(hindi_sentences, telugu_sentences):\n",
    "            writer.writerow([hindi, telugu + '.'])  # Add '.' to Telugu sentences\n",
    "\n",
    "def main():\n",
    "    # Input CSV files\n",
    "    hindi_csv_file = 'hindi.csv'  # Replace with your Hindi CSV file path\n",
    "    telugu_csv_file = 'telugu.csv'  # Replace with your Telugu CSV file path\n",
    "    output_csv_file = 'hindi_telugu_parallel.csv'  # Replace with your desired output CSV file path\n",
    "\n",
    "    # Column names in the CSV files\n",
    "    hindi_column = 'Hindi Sentence'  # Replace with the actual column name in the Hindi CSV\n",
    "    telugu_column = 'Telugu Sentence'  # Replace with the actual column name in the Telugu CSV\n",
    "\n",
    "    # Read Hindi sentences from the CSV file\n",
    "    hindi_sentences = read_sentences_from_csv(hindi_csv_file, hindi_column)\n",
    "\n",
    "    # Read Telugu sentences from the CSV file\n",
    "    telugu_sentences = read_sentences_from_csv(telugu_csv_file, telugu_column)\n",
    "\n",
    "    # Split Telugu sentences using '.' as the delimiter\n",
    "    telugu_sentences_split = split_telugu_sentences(telugu_sentences)\n",
    "\n",
    "    # Ensure both lists have the same length (pad with empty strings if necessary)\n",
    "    max_length = max(len(hindi_sentences), len(telugu_sentences_split))\n",
    "    hindi_sentences += [''] * (max_length - len(hindi_sentences))\n",
    "    telugu_sentences_split += [''] * (max_length - len(telugu_sentences_split))\n",
    "\n",
    "    # Create the parallel CSV file\n",
    "    create_parallel_csv(hindi_sentences, telugu_sentences_split, output_csv_file)\n",
    "\n",
    "    print(f\"Parallel CSV file created successfully: {output_csv_file}\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    main()"
   ]
  }
 ],
 "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.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}