{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Transcriptions have been extracted and saved successfully.\n" ] } ], "source": [ "import re\n", "import os\n", "\n", "# Path to the transcription file\n", "transcription_file = r\"ASR Malayalam\\✅\\mono_male\\transcriptions.txt\"\n", "\n", "# Create a directory to store the output files (optional)\n", "output_dir = r\"ASR Malayalam\\✅\\mono_male\\transcriptions\"\n", "os.makedirs(output_dir, exist_ok=True)\n", "\n", "# Read the transcription file\n", "with open(transcription_file, 'r', encoding='utf-8') as file:\n", " lines = file.readlines()\n", "\n", "# Process each line in the transcription file\n", "for line in lines:\n", " # Use regular expression to match the format and extract the transcription\n", " match = re.match(r'\\((\\S+) \"(.*?)\"\\)', line.strip())\n", " if match:\n", " # Extract the filename (ID) and the Malayalam transcription\n", " filename = match.group(1)\n", " transcription = match.group(2)\n", " \n", " # Construct the output file path\n", " output_file_path = os.path.join(output_dir, f'{filename}.txt')\n", " \n", " # Write the transcription to the file\n", " with open(output_file_path, 'w', encoding='utf-8') as output_file:\n", " output_file.write(transcription)\n", "\n", "print(\"Transcriptions have been extracted and saved successfully.\")\n", "\n", "# Paths to the directories containing audio and transcription files\n", "audio_dir = r\"ASR Malayalam\\✅\\mono_male\\wav\" # Directory containing .wav files\n", "transcription_dir = r\"ASR Malayalam\\✅\\mono_male\\transcriptions\" # Directory containing .txt files\n", "\n", "# Get a list of all .wav files and .txt files in the respective directories\n", "audio_files = {f.replace('.wav', '') for f in os.listdir(audio_dir) if f.endswith('.wav')}\n", "transcription_files = {f.replace('.txt', '') for f in os.listdir(transcription_dir) if f.endswith('.txt')}\n", "\n", "# Find the extra files in each directory that don't have a corresponding match\n", "extra_audio_files = audio_files - transcription_files\n", "extra_transcription_files = transcription_files - audio_files\n", "\n", "# Delete extra audio files (those without matching transcription)\n", "for file in extra_audio_files:\n", " audio_file_path = os.path.join(audio_dir, f\"{file}.wav\")\n", " if os.path.exists(audio_file_path):\n", " os.remove(audio_file_path)\n", " print(f\"Deleted audio file: {audio_file_path}\")\n", "\n", "# Delete extra transcription files (those without matching audio)\n", "for file in extra_transcription_files:\n", " transcription_file_path = os.path.join(transcription_dir, f\"{file}.txt\")\n", " if os.path.exists(transcription_file_path):\n", " os.remove(transcription_file_path)\n", " print(f\"Deleted transcription file: {transcription_file_path}\")\n", "\n", "print(\"Matching files checked and extra files deleted.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import re\n", "\n", "# Paths to the directories containing audio and transcription files\n", "audio_dir = r\"💗\\wav\" # Directory containing .wav files\n", "transcription_file = r\"💗\\transcriptions.txt\" # Path to the transcription file\n", "\n", "# Create a directory to store the output files (optional)\n", "output_dir = r\"💗\\transcriptions\"\n", "os.makedirs(output_dir, exist_ok=True)\n", "\n", "# Read the transcription file\n", "with open(transcription_file, 'r', encoding='utf-8') as file:\n", " lines = file.readlines()\n", "\n", "# Create sets to track the audio files and transcriptions\n", "audio_files = {f.replace('.wav', '') for f in os.listdir(audio_dir) if f.endswith('.wav')}\n", "transcription_files = set()\n", "\n", "# Regular expression to extract audio file name and Malayalam text\n", "# This pattern matches the audio file name after \"audio/\" and captures the Malayalam transcription at the end\n", "transcription_pattern = re.compile(r'\\t(audio/([\\w\\-]+\\.wav))\\t[\\w\\-]+\\t\\d+\\t(.*?)(\\.)')\n", "\n", "# Process each line in the transcription file\n", "for line in lines:\n", " # Strip leading/trailing whitespace and search for the pattern\n", " match = transcription_pattern.search(line.strip())\n", " \n", " if match:\n", " # Extract the audio file name (without 'audio/' prefix) and the transcription text\n", " audio_filename = match.group(2)\n", " transcription = match.group(3).strip() # Malayalam transcription\n", " \n", " # Logging to check if the regex is matching correctly\n", " print(f\"Found transcription: Audio File = {audio_filename}, Text = {transcription}\")\n", " \n", " # Construct the output transcription file path\n", " transcription_filename = f'{audio_filename.replace(\".wav\", \"\")}.txt'\n", " transcription_file_path = os.path.join(output_dir, transcription_filename)\n", " \n", " # Write the transcription to the file\n", " try:\n", " with open(transcription_file_path, 'w', encoding='utf-8') as output_file:\n", " output_file.write(transcription)\n", " print(f\"Created transcription file: {transcription_file_path}\")\n", " except Exception as e:\n", " print(f\"Error writing file {transcription_file_path}: {e}\")\n", " \n", " # Add the transcription identifier to the set\n", " transcription_files.add(audio_filename.replace('.wav', ''))\n", " else:\n", " # If the regex doesn't match, print the problematic line\n", " print(f\"Failed to match line: {line.strip()}\")\n", "\n", "# Find unmatched audio files (those without a corresponding transcription)\n", "extra_audio_files = audio_files - transcription_files\n", "extra_transcription_files = transcription_files - audio_files\n", "\n", "# Delete extra audio files (those without matching transcription)\n", "for file in extra_audio_files:\n", " audio_file_path = os.path.join(audio_dir, f\"{file}.wav\")\n", " if os.path.exists(audio_file_path):\n", " os.remove(audio_file_path)\n", " print(f\"Deleted audio file: {audio_file_path}\")\n", "\n", "# Delete extra transcription files (those without matching audio)\n", "for file in extra_transcription_files:\n", " transcription_file_path = os.path.join(output_dir, f\"{file}.txt\")\n", " if os.path.exists(transcription_file_path):\n", " os.remove(transcription_file_path)\n", " print(f\"Deleted transcription file: {transcription_file_path}\")\n", "\n", "print(\"Matching files checked and extra files deleted.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matching files checked and extra files deleted.\n" ] } ], "source": [ "import os\n", "\n", "# Paths to the directories containing audio and transcription files\n", "audio_dir = \"🗿\\wav\" # Directory containing .wav files\n", "transcription_file = r\"🗿\\transcriptions.txt\" # Path to the transcription file\n", "\n", "# Create a directory to store the output files (optional)\n", "output_dir = r\"🗿\\transcriptions\"\n", "os.makedirs(output_dir, exist_ok=True)\n", "\n", "# Read the transcription file\n", "with open(transcription_file, 'r', encoding='utf-8') as file:\n", " lines = file.readlines()\n", "\n", "# Create sets to track the audio files and transcriptions\n", "audio_files = {f.replace('.wav', '') for f in os.listdir(audio_dir) if f.endswith('.wav')}\n", "transcription_files = set()\n", "\n", "# Process each line in the transcription file\n", "for line in lines:\n", " columns = line.strip().split('\\t')\n", " if len(columns) > 1:\n", " # Extract the transcription identifier and the Malayalam transcription\n", " transcription_id = columns[0]\n", " transcription = columns[1]\n", "\n", " # Construct the output transcription file path\n", " transcription_filename = f'{transcription_id}.txt'\n", " transcription_file_path = os.path.join(output_dir, transcription_filename)\n", " \n", " # Write the transcription to the file\n", " with open(transcription_file_path, 'w', encoding='utf-8') as output_file:\n", " output_file.write(transcription)\n", "\n", " # Add the transcription identifier to the set\n", " transcription_files.add(transcription_id)\n", "\n", "# Find unmatched audio files (those without a corresponding transcription)\n", "extra_audio_files = audio_files - transcription_files\n", "extra_transcription_files = transcription_files - audio_files\n", "\n", "# Delete extra audio files (those without matching transcription)\n", "for file in extra_audio_files:\n", " audio_file_path = os.path.join(audio_dir, f\"{file}.wav\")\n", " if os.path.exists(audio_file_path):\n", " os.remove(audio_file_path)\n", " print(f\"Deleted audio file: {audio_file_path}\")\n", "\n", "# Delete extra transcription files (those without matching audio)\n", "for file in extra_transcription_files:\n", " transcription_file_path = os.path.join(output_dir, f\"{file}.txt\")\n", " if os.path.exists(transcription_file_path):\n", " os.remove(transcription_file_path)\n", " print(f\"Deleted transcription file: {transcription_file_path}\")\n", "\n", "print(\"Matching files checked and extra files deleted.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Matching files checked and extra files deleted.\n" ] } ], "source": [ "import os\n", "\n", "# Paths to the directories containing audio and transcription files\n", "audio_dir = r\"😭\\Vidhya\\wavs\" # Directory containing .wav files\n", "transcription_file = r\"😭\\Vidhya\\transcriptions.txt\" # Path to the transcription file\n", "\n", "# Create a directory to store the output files (optional)\n", "output_dir = r\"😭\\Vidhya\\transcriptions\"\n", "os.makedirs(output_dir, exist_ok=True)\n", "\n", "# Read the transcription file\n", "with open(transcription_file, 'r', encoding='utf-8') as file:\n", " lines = file.readlines()\n", "\n", "# Create sets to track the audio files and transcriptions\n", "audio_files = {f.replace('.wav', '') for f in os.listdir(audio_dir) if f.endswith('.wav')}\n", "transcription_files = set()\n", "\n", "# Process each line in the transcription file\n", "for line in lines:\n", " # Split the line at the '|' symbol to separate the identifier and the transcription\n", " columns = line.strip().split('|')\n", " if len(columns) == 2:\n", " transcription_id = columns[0].strip()\n", " transcription = columns[1].strip()\n", "\n", " # Construct the output transcription file path\n", " transcription_filename = f'{transcription_id}.txt'\n", " transcription_file_path = os.path.join(output_dir, transcription_filename)\n", " \n", " # Write the transcription to the file\n", " with open(transcription_file_path, 'w', encoding='utf-8') as output_file:\n", " output_file.write(transcription)\n", "\n", " # Add the transcription identifier to the set\n", " transcription_files.add(transcription_id)\n", "\n", "# Find unmatched audio files (those without a corresponding transcription)\n", "extra_audio_files = audio_files - transcription_files\n", "extra_transcription_files = transcription_files - audio_files\n", "\n", "# Delete extra audio files (those without matching transcription)\n", "for file in extra_audio_files:\n", " audio_file_path = os.path.join(audio_dir, f\"{file}.wav\")\n", " if os.path.exists(audio_file_path):\n", " os.remove(audio_file_path)\n", " print(f\"Deleted audio file: {audio_file_path}\")\n", "\n", "# Delete extra transcription files (those without matching audio)\n", "for file in extra_transcription_files:\n", " transcription_file_path = os.path.join(output_dir, f\"{file}.txt\")\n", " if os.path.exists(transcription_file_path):\n", " os.remove(transcription_file_path)\n", " print(f\"Deleted transcription file: {transcription_file_path}\")\n", "\n", "print(\"Matching files checked and extra files deleted.\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import re\n", "\n", "# Paths to the directories containing audio and transcription files\n", "audio_dir = r\"🤑\\wav\" # Directory containing .wav files\n", "transcription_file = r\"🤑\\transcriptions.txt\" # Path to the transcription file\n", "\n", "# Create a directory to store the output files (optional)\n", "output_dir = r\"🤑\\transcriptions\"\n", "os.makedirs(output_dir, exist_ok=True)\n", "\n", "# Read the transcription file\n", "with open(transcription_file, 'r', encoding='utf-8') as file:\n", " lines = file.readlines()\n", "\n", "# Create sets to track the audio files and transcriptions\n", "audio_files = {f.replace('.wav', '') for f in os.listdir(audio_dir) if f.endswith('.wav')}\n", "transcription_files = set()\n", "\n", "# Regular expression to extract transcription ID and Malayalam text\n", "# The pattern matches 'mal_' followed by digits and captures everything after that as the transcription text\n", "transcription_pattern = re.compile(r'(mal_\\d+)\\s+(.+)')\n", "\n", "# Process each line in the transcription file\n", "for line in lines:\n", " # Strip leading/trailing whitespace and search for the pattern\n", " match = transcription_pattern.search(line.strip())\n", " \n", " if match:\n", " transcription_id = match.group(1)\n", " transcription = match.group(2).strip() # Capture the Malayalam text and remove surrounding whitespace\n", " \n", " # Logging to check if the regex is matching correctly\n", " print(f\"Found transcription: ID = {transcription_id}, Text = {transcription}\")\n", " \n", " # Construct the output transcription file path\n", " transcription_filename = f'{transcription_id}.txt'\n", " transcription_file_path = os.path.join(output_dir, transcription_filename)\n", " \n", " # Write the transcription to the file\n", " try:\n", " with open(transcription_file_path, 'w', encoding='utf-8') as output_file:\n", " output_file.write(transcription)\n", " print(f\"Created transcription file: {transcription_file_path}\")\n", " except Exception as e:\n", " print(f\"Error writing file {transcription_file_path}: {e}\")\n", " \n", " # Add the transcription identifier to the set\n", " transcription_files.add(transcription_id)\n", " else:\n", " # If the regex doesn't match, print the problematic line\n", " print(f\"Failed to match line: {line.strip()}\")\n", "\n", "# Find unmatched audio files (those without a corresponding transcription)\n", "extra_audio_files = audio_files - transcription_files\n", "extra_transcription_files = transcription_files - audio_files\n", "\n", "# Delete extra audio files (those without matching transcription)\n", "for file in extra_audio_files:\n", " audio_file_path = os.path.join(audio_dir, f\"{file}.wav\")\n", " if os.path.exists(audio_file_path):\n", " os.remove(audio_file_path)\n", " print(f\"Deleted audio file: {audio_file_path}\")\n", "\n", "# Delete extra transcription files (those without matching audio)\n", "for file in extra_transcription_files:\n", " transcription_file_path = os.path.join(output_dir, f\"{file}.txt\")\n", " if os.path.exists(transcription_file_path):\n", " os.remove(transcription_file_path)\n", " print(f\"Deleted transcription file: {transcription_file_path}\")\n", "\n", "print(\"Matching files checked and extra files deleted.\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### CREATE JSON MAPPING BETWEEN WAV & TRANSCRIPTIONS" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Mapping created successfully. Output saved to D:\\ASR Malayalam\\map.json\n" ] } ], "source": [ "import os\n", "import json\n", "\n", "# Directory structure\n", "base_dir = r\"./\" # Path to the directory containing the 6 folders\n", "output_json_file = r\"map.json\" # Output file for the JSON mapping\n", "\n", "# Initialize the dictionary to hold the mappings\n", "file_mapping = {}\n", "\n", "# List all the folders in the base directory\n", "folders = [folder for folder in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, folder))]\n", "\n", "# Loop over each folder (assumed to be named with 6 unique names)\n", "for folder in folders:\n", " wav_folder = os.path.join(base_dir, folder, 'wav') # Path to the 'wav' subfolder\n", " transcription_folder = os.path.join(base_dir, folder, 'transcription') # Path to the 'transcription' subfolder\n", "\n", " # List all .wav files in the 'wav' folder\n", " wav_files = [f for f in os.listdir(wav_folder) if f.endswith('.wav')]\n", " \n", " # List all .txt files in the 'transcription' folder\n", " transcription_files = [f for f in os.listdir(transcription_folder) if f.endswith('.txt')]\n", "\n", " # Create mappings for each .wav file to its corresponding .txt transcription file\n", " for wav_file in wav_files:\n", " # Assuming the transcription filename is the same as the wav filename but with a .txt extension\n", " transcription_file = wav_file.replace('.wav', '.txt')\n", "\n", " # Check if the corresponding transcription file exists\n", " if transcription_file in transcription_files:\n", " # Add to the mapping\n", " file_mapping[os.path.join(wav_folder, wav_file)] = os.path.join(transcription_folder, transcription_file)\n", "\n", "# Write the JSON output to a file\n", "with open(output_json_file, 'w', encoding='utf-8') as json_file:\n", " json.dump(file_mapping, json_file, ensure_ascii=False, indent=4)\n", "\n", "print(f\"Mapping created successfully. Output saved to {output_json_file}\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1\n" ] } ], "source": [ "import os\n", "\n", "# Set the environment variable\n", "os.environ[\"HF_HUB_ENABLE_HF_TRANSFER\"] = \"1\"\n", "\n", "# Check if the environment variable is set\n", "print(os.environ.get(\"HF_HUB_ENABLE_HF_TRANSFER\"))" ] } ], "metadata": { "kernelspec": { "display_name": "tf", "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.10.11" } }, "nbformat": 4, "nbformat_minor": 2 }