File size: 21,044 Bytes
345d925 |
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
{
"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
}
|