{ "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 }