Upload 2 files
Browse files- scripts/Data_create_code.ipynb +132 -0
- scripts/data_read.ipynb +298 -0
scripts/Data_create_code.ipynb
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 3,
|
| 6 |
+
"id": "1da63fbc",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"Defaulting to user installation because normal site-packages is not writeableNote: you may need to restart the kernel to use updated packages.\n",
|
| 14 |
+
"\n",
|
| 15 |
+
"Collecting pdf2image\n",
|
| 16 |
+
" Obtaining dependency information for pdf2image from https://files.pythonhosted.org/packages/62/33/61766ae033518957f877ab246f87ca30a85b778ebaad65b7f74fa7e52988/pdf2image-1.17.0-py3-none-any.whl.metadata\n",
|
| 17 |
+
" Downloading pdf2image-1.17.0-py3-none-any.whl.metadata (6.2 kB)\n",
|
| 18 |
+
"Requirement already satisfied: pillow in c:\\programdata\\anaconda3\\lib\\site-packages (from pdf2image) (9.4.0)\n",
|
| 19 |
+
"Downloading pdf2image-1.17.0-py3-none-any.whl (11 kB)\n",
|
| 20 |
+
"Installing collected packages: pdf2image\n",
|
| 21 |
+
"Successfully installed pdf2image-1.17.0\n"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"ename": "ModuleNotFoundError",
|
| 26 |
+
"evalue": "No module named 'pytesseract'",
|
| 27 |
+
"output_type": "error",
|
| 28 |
+
"traceback": [
|
| 29 |
+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
| 30 |
+
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
|
| 31 |
+
"Cell \u001b[1;32mIn[3], line 5\u001b[0m\n\u001b[0;32m 1\u001b[0m get_ipython()\u001b[38;5;241m.\u001b[39mrun_line_magic(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpip\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124minstall pdf2image\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mpdf2image\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m convert_from_path\n\u001b[1;32m----> 5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mpytesseract\u001b[39;00m\n\u001b[0;32m 7\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mextract_text_from_image_pdf\u001b[39m(pdf_file):\n\u001b[0;32m 8\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[0;32m 9\u001b[0m \u001b[38;5;124;03m Extract text from image-based PDF using OCR.\u001b[39;00m\n\u001b[0;32m 10\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n",
|
| 32 |
+
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'pytesseract'"
|
| 33 |
+
]
|
| 34 |
+
}
|
| 35 |
+
],
|
| 36 |
+
"source": [
|
| 37 |
+
"%pip install pdf2image\n",
|
| 38 |
+
"\n",
|
| 39 |
+
"\n",
|
| 40 |
+
"from pdf2image import convert_from_path\n",
|
| 41 |
+
"import pytesseract\n",
|
| 42 |
+
"\n",
|
| 43 |
+
"def extract_text_from_image_pdf(pdf_file):\n",
|
| 44 |
+
" \"\"\"\n",
|
| 45 |
+
" Extract text from image-based PDF using OCR.\n",
|
| 46 |
+
" \"\"\"\n",
|
| 47 |
+
" pages = convert_from_path(pdf_file)\n",
|
| 48 |
+
" text = []\n",
|
| 49 |
+
" for page in pages:\n",
|
| 50 |
+
" text.append(pytesseract.image_to_string(page, lang='hin+ben'))\n",
|
| 51 |
+
" return text\n",
|
| 52 |
+
"\n",
|
| 53 |
+
"def read_write_excel(pdf_file1, pdf_file2, output_file):\n",
|
| 54 |
+
" \"\"\"\n",
|
| 55 |
+
" Reads two PDF files, extracts their text, and writes their contents line by line\n",
|
| 56 |
+
" into two separate columns of an Excel file.\n",
|
| 57 |
+
"\n",
|
| 58 |
+
" Args:\n",
|
| 59 |
+
" pdf_file1 (str): Path to the first PDF file.\n",
|
| 60 |
+
" pdf_file2 (str): Path to the second PDF file.\n",
|
| 61 |
+
" output_file (str): Path to the output Excel file.\n",
|
| 62 |
+
" \"\"\"\n",
|
| 63 |
+
" # Extract text from PDF files\n",
|
| 64 |
+
" text1 = extract_text_from_pdf(pdf_file1)\n",
|
| 65 |
+
" text2 = extract_text_from_pdf(pdf_file2)\n",
|
| 66 |
+
"\n",
|
| 67 |
+
" # Open the Excel workbook\n",
|
| 68 |
+
" workbook = openpyxl.Workbook()\n",
|
| 69 |
+
" worksheet = workbook.active\n",
|
| 70 |
+
"\n",
|
| 71 |
+
" # Set column titles (optional)\n",
|
| 72 |
+
" worksheet.cell(row=1, column=1).value = \"Hindi\"\n",
|
| 73 |
+
" worksheet.cell(row=1, column=2).value = \"Bengali\"\n",
|
| 74 |
+
"\n",
|
| 75 |
+
" # Write data to Excel\n",
|
| 76 |
+
" row = 2\n",
|
| 77 |
+
" for line1, line2 in zip(text1, text2):\n",
|
| 78 |
+
" worksheet.cell(row=row, column=1).value = line1.strip()\n",
|
| 79 |
+
" worksheet.cell(row=row, column=2).value = line2.strip()\n",
|
| 80 |
+
" row += 1\n",
|
| 81 |
+
"\n",
|
| 82 |
+
" # Save the workbook\n",
|
| 83 |
+
" workbook.save(output_file)\n",
|
| 84 |
+
" print(f\"Excel file created: {output_file}\")\n",
|
| 85 |
+
"\n",
|
| 86 |
+
"# Example usage\n",
|
| 87 |
+
"pdf_file1 = \"atithi-hin.pdf\"\n",
|
| 88 |
+
"pdf_file2 = \"atithi-ben.pdf\"\n",
|
| 89 |
+
"output_file = \"Atithi-hi-bn.xlsx\"\n",
|
| 90 |
+
"\n",
|
| 91 |
+
"read_write_excel(pdf_file1, pdf_file2, output_file)\n"
|
| 92 |
+
]
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"cell_type": "code",
|
| 96 |
+
"execution_count": null,
|
| 97 |
+
"id": "d4bb9932",
|
| 98 |
+
"metadata": {},
|
| 99 |
+
"outputs": [],
|
| 100 |
+
"source": []
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"cell_type": "code",
|
| 104 |
+
"execution_count": null,
|
| 105 |
+
"id": "de6a63fe",
|
| 106 |
+
"metadata": {},
|
| 107 |
+
"outputs": [],
|
| 108 |
+
"source": []
|
| 109 |
+
}
|
| 110 |
+
],
|
| 111 |
+
"metadata": {
|
| 112 |
+
"kernelspec": {
|
| 113 |
+
"display_name": "Python 3",
|
| 114 |
+
"language": "python",
|
| 115 |
+
"name": "python3"
|
| 116 |
+
},
|
| 117 |
+
"language_info": {
|
| 118 |
+
"codemirror_mode": {
|
| 119 |
+
"name": "ipython",
|
| 120 |
+
"version": 3
|
| 121 |
+
},
|
| 122 |
+
"file_extension": ".py",
|
| 123 |
+
"mimetype": "text/x-python",
|
| 124 |
+
"name": "python",
|
| 125 |
+
"nbconvert_exporter": "python",
|
| 126 |
+
"pygments_lexer": "ipython3",
|
| 127 |
+
"version": "3.10.11"
|
| 128 |
+
}
|
| 129 |
+
},
|
| 130 |
+
"nbformat": 4,
|
| 131 |
+
"nbformat_minor": 5
|
| 132 |
+
}
|
scripts/data_read.ipynb
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 2,
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"outputs": [
|
| 8 |
+
{
|
| 9 |
+
"name": "stdout",
|
| 10 |
+
"output_type": "stream",
|
| 11 |
+
"text": [
|
| 12 |
+
"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",
|
| 13 |
+
"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",
|
| 14 |
+
"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"
|
| 15 |
+
]
|
| 16 |
+
},
|
| 17 |
+
{
|
| 18 |
+
"name": "stderr",
|
| 19 |
+
"output_type": "stream",
|
| 20 |
+
"text": [
|
| 21 |
+
"\n",
|
| 22 |
+
"[notice] A new release of pip is available: 23.0.1 -> 25.0.1\n",
|
| 23 |
+
"[notice] To update, run: C:\\Users\\Dell\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe -m pip install --upgrade pip\n"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"name": "stdout",
|
| 28 |
+
"output_type": "stream",
|
| 29 |
+
"text": [
|
| 30 |
+
"CSV file created successfully: devdas1-hi-bn.csv\n"
|
| 31 |
+
]
|
| 32 |
+
}
|
| 33 |
+
],
|
| 34 |
+
"source": [
|
| 35 |
+
"import csv\n",
|
| 36 |
+
"import re\n",
|
| 37 |
+
"!pip install python-docx\n",
|
| 38 |
+
"import docx\n",
|
| 39 |
+
"from docx import Document\n",
|
| 40 |
+
"\n",
|
| 41 |
+
"def read_write_csv_from_docx(docx_file1, docx_file2, output_file):\n",
|
| 42 |
+
" \"\"\"\n",
|
| 43 |
+
" Reads two .docx files, splits lines using '।' and '?' as delimiters, \n",
|
| 44 |
+
" escapes quotes properly, and writes their contents into a CSV file with UTF-8 BOM.\n",
|
| 45 |
+
" \"\"\"\n",
|
| 46 |
+
" def extract_text_from_docx(docx_file):\n",
|
| 47 |
+
" \"\"\"\n",
|
| 48 |
+
" Extracts text from a .docx file and returns it as a single string.\n",
|
| 49 |
+
" \"\"\"\n",
|
| 50 |
+
" document = Document(docx_file)\n",
|
| 51 |
+
" text = []\n",
|
| 52 |
+
" for paragraph in document.paragraphs:\n",
|
| 53 |
+
" text.append(paragraph.text)\n",
|
| 54 |
+
" return \" \".join(text)\n",
|
| 55 |
+
"\n",
|
| 56 |
+
" try:\n",
|
| 57 |
+
" # Define the delimiters\n",
|
| 58 |
+
" delimiters = r\"[।?]\"\n",
|
| 59 |
+
"\n",
|
| 60 |
+
" # Extract and split content from the first .docx file\n",
|
| 61 |
+
" content1 = extract_text_from_docx(docx_file1)\n",
|
| 62 |
+
" lines1 = re.split(delimiters, content1.strip())\n",
|
| 63 |
+
"\n",
|
| 64 |
+
" # Extract and split content from the second .docx file\n",
|
| 65 |
+
" content2 = extract_text_from_docx(docx_file2)\n",
|
| 66 |
+
" lines2 = re.split(delimiters, content2.strip())\n",
|
| 67 |
+
"\n",
|
| 68 |
+
" # Open the CSV file for writing with UTF-8 BOM\n",
|
| 69 |
+
" with open(output_file, 'w', encoding='utf-8-sig', newline='') as csv_file:\n",
|
| 70 |
+
" writer = csv.writer(csv_file, quoting=csv.QUOTE_MINIMAL)\n",
|
| 71 |
+
"\n",
|
| 72 |
+
" # Write the header\n",
|
| 73 |
+
" writer.writerow([\"Hindi Text\", \"Bengali Text\"])\n",
|
| 74 |
+
"\n",
|
| 75 |
+
" # Write the content line by line\n",
|
| 76 |
+
" for line1, line2 in zip(lines1, lines2):\n",
|
| 77 |
+
" writer.writerow([line1.strip().replace('\"', '\"\"'), line2.strip().replace('\"', '\"\"')])\n",
|
| 78 |
+
"\n",
|
| 79 |
+
" # Handle mismatched lines\n",
|
| 80 |
+
" if len(lines1) > len(lines2):\n",
|
| 81 |
+
" for line1 in lines1[len(lines2):]:\n",
|
| 82 |
+
" writer.writerow([line1.strip().replace('\"', '\"\"'), \"N/A\"])\n",
|
| 83 |
+
" elif len(lines2) > len(lines1):\n",
|
| 84 |
+
" for line2 in lines2[len(lines1):]:\n",
|
| 85 |
+
" writer.writerow([\"N/A\", line2.strip().replace('\"', '\"\"')])\n",
|
| 86 |
+
"\n",
|
| 87 |
+
" print(f\"CSV file created successfully: {output_file}\")\n",
|
| 88 |
+
"\n",
|
| 89 |
+
" except FileNotFoundError:\n",
|
| 90 |
+
" print(\"Error: One or both .docx files not found.\")\n",
|
| 91 |
+
" except Exception as e:\n",
|
| 92 |
+
" print(f\"An error occurred: {e}\")\n",
|
| 93 |
+
"\n",
|
| 94 |
+
"# Example usage\n",
|
| 95 |
+
"docx_file1 = \"devdas-hi.docx\"\n",
|
| 96 |
+
"docx_file2 = \"devdas-bn.docx\"\n",
|
| 97 |
+
"output_file = \"devdas1-hi-bn.csv\"\n",
|
| 98 |
+
"\n",
|
| 99 |
+
"read_write_csv_from_docx(docx_file1, docx_file2, output_file)"
|
| 100 |
+
]
|
| 101 |
+
},
|
| 102 |
+
{
|
| 103 |
+
"cell_type": "code",
|
| 104 |
+
"execution_count": 5,
|
| 105 |
+
"metadata": {},
|
| 106 |
+
"outputs": [
|
| 107 |
+
{
|
| 108 |
+
"name": "stdout",
|
| 109 |
+
"output_type": "stream",
|
| 110 |
+
"text": [
|
| 111 |
+
"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",
|
| 112 |
+
"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",
|
| 113 |
+
"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"
|
| 114 |
+
]
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"name": "stderr",
|
| 118 |
+
"output_type": "stream",
|
| 119 |
+
"text": [
|
| 120 |
+
"\n",
|
| 121 |
+
"[notice] A new release of pip is available: 23.0.1 -> 25.0.1\n",
|
| 122 |
+
"[notice] To update, run: C:\\Users\\Dell\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\python.exe -m pip install --upgrade pip\n"
|
| 123 |
+
]
|
| 124 |
+
},
|
| 125 |
+
{
|
| 126 |
+
"name": "stdout",
|
| 127 |
+
"output_type": "stream",
|
| 128 |
+
"text": [
|
| 129 |
+
"CSV file created successfully: Ati-hi-tel.csv\n"
|
| 130 |
+
]
|
| 131 |
+
}
|
| 132 |
+
],
|
| 133 |
+
"source": [
|
| 134 |
+
"import csv\n",
|
| 135 |
+
"import re\n",
|
| 136 |
+
"!pip install python-docx\n",
|
| 137 |
+
"import docx\n",
|
| 138 |
+
"from docx import Document\n",
|
| 139 |
+
"\n",
|
| 140 |
+
"def read_write_csv_from_docx(docx_file1, docx_file2, output_file):\n",
|
| 141 |
+
" \"\"\"\n",
|
| 142 |
+
" Reads two .docx files, splits lines using '।' and '?' as delimiters, \n",
|
| 143 |
+
" escapes quotes properly, and writes their contents into a CSV file with UTF-8 BOM.\n",
|
| 144 |
+
" \"\"\"\n",
|
| 145 |
+
" def extract_text_from_docx(docx_file):\n",
|
| 146 |
+
" \"\"\"\n",
|
| 147 |
+
" Extracts text from a .docx file and returns it as a single string.\n",
|
| 148 |
+
" \"\"\"\n",
|
| 149 |
+
" document = Document(docx_file)\n",
|
| 150 |
+
" text = []\n",
|
| 151 |
+
" for paragraph in document.paragraphs:\n",
|
| 152 |
+
" text.append(paragraph.text)\n",
|
| 153 |
+
" return \" \".join(text)\n",
|
| 154 |
+
"\n",
|
| 155 |
+
" try:\n",
|
| 156 |
+
" # Define the delimiters\n",
|
| 157 |
+
" delimiters = r\"[.?]\"\n",
|
| 158 |
+
"\n",
|
| 159 |
+
" # Extract and split content from the first .docx file\n",
|
| 160 |
+
" content1 = extract_text_from_docx(docx_file1)\n",
|
| 161 |
+
" lines1 = re.split(delimiters, content1.strip())\n",
|
| 162 |
+
"\n",
|
| 163 |
+
" # Extract and split content from the second .docx file\n",
|
| 164 |
+
" content2 = extract_text_from_docx(docx_file2)\n",
|
| 165 |
+
" lines2 = re.split(delimiters, content2.strip())\n",
|
| 166 |
+
"\n",
|
| 167 |
+
" # Open the CSV file for writing with UTF-8 BOM\n",
|
| 168 |
+
" with open(output_file, 'w', encoding='utf-8-sig', newline='') as csv_file:\n",
|
| 169 |
+
" writer = csv.writer(csv_file, quoting=csv.QUOTE_MINIMAL)\n",
|
| 170 |
+
"\n",
|
| 171 |
+
" # Write the header\n",
|
| 172 |
+
" writer.writerow([\"Hindi Text\", \"Telugu Text\"])\n",
|
| 173 |
+
"\n",
|
| 174 |
+
" # Write the content line by line\n",
|
| 175 |
+
" for line1, line2 in zip(lines1, lines2):\n",
|
| 176 |
+
" writer.writerow([line1.strip().replace('\"', '\"\"'), line2.strip().replace('\"', '\"\"')])\n",
|
| 177 |
+
"\n",
|
| 178 |
+
" # Handle mismatched lines\n",
|
| 179 |
+
" if len(lines1) > len(lines2):\n",
|
| 180 |
+
" for line1 in lines1[len(lines2):]:\n",
|
| 181 |
+
" writer.writerow([line1.strip().replace('\"', '\"\"'), \"N/A\"])\n",
|
| 182 |
+
" elif len(lines2) > len(lines1):\n",
|
| 183 |
+
" for line2 in lines2[len(lines1):]:\n",
|
| 184 |
+
" writer.writerow([\"N/A\", line2.strip().replace('\"', '\"\"')])\n",
|
| 185 |
+
"\n",
|
| 186 |
+
" print(f\"CSV file created successfully: {output_file}\")\n",
|
| 187 |
+
"\n",
|
| 188 |
+
" except FileNotFoundError:\n",
|
| 189 |
+
" print(\"Error: One or both .docx files not found.\")\n",
|
| 190 |
+
" except Exception as e:\n",
|
| 191 |
+
" print(f\"An error occurred: {e}\")\n",
|
| 192 |
+
"\n",
|
| 193 |
+
"# Example usage\n",
|
| 194 |
+
"docx_file1 = \"Ati_hi.docx\"\n",
|
| 195 |
+
"docx_file2 = \"Ati_tel.docx\"\n",
|
| 196 |
+
"output_file = \"Ati-hi-tel.csv\"\n",
|
| 197 |
+
"\n",
|
| 198 |
+
"read_write_csv_from_docx(docx_file1, docx_file2, output_file)"
|
| 199 |
+
]
|
| 200 |
+
},
|
| 201 |
+
{
|
| 202 |
+
"cell_type": "code",
|
| 203 |
+
"execution_count": null,
|
| 204 |
+
"metadata": {},
|
| 205 |
+
"outputs": [],
|
| 206 |
+
"source": [
|
| 207 |
+
"import csv\n",
|
| 208 |
+
"\n",
|
| 209 |
+
"def read_sentences_from_csv(csv_file, column_name):\n",
|
| 210 |
+
" \"\"\"\n",
|
| 211 |
+
" Reads sentences from a CSV file under a specific column.\n",
|
| 212 |
+
" \"\"\"\n",
|
| 213 |
+
" sentences = []\n",
|
| 214 |
+
" with open(csv_file, 'r', encoding='utf-8') as file:\n",
|
| 215 |
+
" reader = csv.DictReader(file)\n",
|
| 216 |
+
" for row in reader:\n",
|
| 217 |
+
" sentences.append(row[column_name])\n",
|
| 218 |
+
" return sentences\n",
|
| 219 |
+
"\n",
|
| 220 |
+
"def split_telugu_sentences(telugu_sentences):\n",
|
| 221 |
+
" \"\"\"\n",
|
| 222 |
+
" Splits Telugu sentences using '.' as the delimiter.\n",
|
| 223 |
+
" \"\"\"\n",
|
| 224 |
+
" split_sentences = []\n",
|
| 225 |
+
" for sentence in telugu_sentences:\n",
|
| 226 |
+
" # Split using '.' and remove leading/trailing whitespace\n",
|
| 227 |
+
" parts = [part.strip() for part in sentence.split('.') if part.strip()]\n",
|
| 228 |
+
" split_sentences.extend(parts)\n",
|
| 229 |
+
" return split_sentences\n",
|
| 230 |
+
"\n",
|
| 231 |
+
"def create_parallel_csv(hindi_sentences, telugu_sentences, output_file):\n",
|
| 232 |
+
" \"\"\"\n",
|
| 233 |
+
" Creates a 2-column CSV file with Hindi and Telugu sentences.\n",
|
| 234 |
+
" \"\"\"\n",
|
| 235 |
+
" with open(output_file, 'w', encoding='utf-8-sig', newline='') as csvfile:\n",
|
| 236 |
+
" writer = csv.writer(csvfile)\n",
|
| 237 |
+
" # Write header\n",
|
| 238 |
+
" writer.writerow(['Hindi Sentence', 'Telugu Sentence'])\n",
|
| 239 |
+
" # Write sentence pairs\n",
|
| 240 |
+
" for hindi, telugu in zip(hindi_sentences, telugu_sentences):\n",
|
| 241 |
+
" writer.writerow([hindi, telugu + '.']) # Add '.' to Telugu sentences\n",
|
| 242 |
+
"\n",
|
| 243 |
+
"def main():\n",
|
| 244 |
+
" # Input CSV files\n",
|
| 245 |
+
" hindi_csv_file = 'hindi.csv' # Replace with your Hindi CSV file path\n",
|
| 246 |
+
" telugu_csv_file = 'telugu.csv' # Replace with your Telugu CSV file path\n",
|
| 247 |
+
" output_csv_file = 'hindi_telugu_parallel.csv' # Replace with your desired output CSV file path\n",
|
| 248 |
+
"\n",
|
| 249 |
+
" # Column names in the CSV files\n",
|
| 250 |
+
" hindi_column = 'Hindi Sentence' # Replace with the actual column name in the Hindi CSV\n",
|
| 251 |
+
" telugu_column = 'Telugu Sentence' # Replace with the actual column name in the Telugu CSV\n",
|
| 252 |
+
"\n",
|
| 253 |
+
" # Read Hindi sentences from the CSV file\n",
|
| 254 |
+
" hindi_sentences = read_sentences_from_csv(hindi_csv_file, hindi_column)\n",
|
| 255 |
+
"\n",
|
| 256 |
+
" # Read Telugu sentences from the CSV file\n",
|
| 257 |
+
" telugu_sentences = read_sentences_from_csv(telugu_csv_file, telugu_column)\n",
|
| 258 |
+
"\n",
|
| 259 |
+
" # Split Telugu sentences using '.' as the delimiter\n",
|
| 260 |
+
" telugu_sentences_split = split_telugu_sentences(telugu_sentences)\n",
|
| 261 |
+
"\n",
|
| 262 |
+
" # Ensure both lists have the same length (pad with empty strings if necessary)\n",
|
| 263 |
+
" max_length = max(len(hindi_sentences), len(telugu_sentences_split))\n",
|
| 264 |
+
" hindi_sentences += [''] * (max_length - len(hindi_sentences))\n",
|
| 265 |
+
" telugu_sentences_split += [''] * (max_length - len(telugu_sentences_split))\n",
|
| 266 |
+
"\n",
|
| 267 |
+
" # Create the parallel CSV file\n",
|
| 268 |
+
" create_parallel_csv(hindi_sentences, telugu_sentences_split, output_csv_file)\n",
|
| 269 |
+
"\n",
|
| 270 |
+
" print(f\"Parallel CSV file created successfully: {output_csv_file}\")\n",
|
| 271 |
+
"\n",
|
| 272 |
+
"if __name__ == \"__main__\":\n",
|
| 273 |
+
" main()"
|
| 274 |
+
]
|
| 275 |
+
}
|
| 276 |
+
],
|
| 277 |
+
"metadata": {
|
| 278 |
+
"kernelspec": {
|
| 279 |
+
"display_name": "Python 3",
|
| 280 |
+
"language": "python",
|
| 281 |
+
"name": "python3"
|
| 282 |
+
},
|
| 283 |
+
"language_info": {
|
| 284 |
+
"codemirror_mode": {
|
| 285 |
+
"name": "ipython",
|
| 286 |
+
"version": 3
|
| 287 |
+
},
|
| 288 |
+
"file_extension": ".py",
|
| 289 |
+
"mimetype": "text/x-python",
|
| 290 |
+
"name": "python",
|
| 291 |
+
"nbconvert_exporter": "python",
|
| 292 |
+
"pygments_lexer": "ipython3",
|
| 293 |
+
"version": "3.12.0"
|
| 294 |
+
}
|
| 295 |
+
},
|
| 296 |
+
"nbformat": 4,
|
| 297 |
+
"nbformat_minor": 2
|
| 298 |
+
}
|