| |
| """Pdf-Data 1.ipynb |
| |
| Automatically generated by Colab. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/1IB0DbFJbA27C0womZkoMQZ7oIkgJkHYU |
| |
| # Install & import libs |
| """ |
|
|
| !pip install pypdf pandas tqdm |
|
|
| !apt-get install -y tesseract-ocr |
| !pip install pytesseract pdf2image pypdf pandas tqdm pillow |
| !apt-get install -y tesseract-ocr-ara |
|
|
| !apt-get install -y poppler-utils |
|
|
| import os |
| import pandas as pd |
| from pypdf import PdfReader |
| from tqdm import tqdm |
|
|
| """# Mount Google Drive""" |
|
|
| from google.colab import drive |
| drive.mount('/content/drive') |
|
|
| """# Config paths""" |
|
|
| BASE_FOLDER = "/content/drive/MyDrive/OitLab/Text" |
| OUTPUT_CSV = "/content/drive/MyDrive/OitLab/Text/pdf_dataset1.csv" |
|
|
| """# Core extraction logic""" |
|
|
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| |
| |
| |
|
|
| |
| |
|
|
| |
| |
|
|
| |
|
|
| |
|
|
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
|
|
| import os |
| import pandas as pd |
| from pypdf import PdfReader |
| from pdf2image import convert_from_path |
| import pytesseract |
| from tqdm import tqdm |
| import re |
| from multiprocessing import Pool, cpu_count |
| from functools import partial |
|
|
| |
| def clean_text(text): |
| text = re.sub(r'\s+', ' ', text) |
| return text.strip() |
|
|
| |
| BASE_FOLDER = "/content/drive/MyDrive/OitLab/Text" |
| OUTPUT_CSV = "/content/drive/MyDrive/OitLab/Text/pdf_dataset1.csv" |
| PREFERRED_CATEGORIES = ["Historique","Religion","Muslim"] |
| N_WORKERS = max(1, cpu_count() - 1) |
|
|
| print(f"N_WORKERS: {N_WORKERS}") |
|
|
| |
| if os.path.exists(OUTPUT_CSV): |
| df_existing = pd.read_csv(OUTPUT_CSV) |
| else: |
| df_existing = pd.DataFrame(columns=["name","page","content","category","char"]) |
|
|
| processed_set = set(zip(df_existing['category'], df_existing['name'])) |
|
|
| |
| def process_pdf(task): |
| category, pdf_path, file_name = task |
| pdf_rows = [] |
|
|
| try: |
| reader = PdfReader(pdf_path) |
| total_pages = len(reader.pages) |
|
|
| for page_num, page in enumerate(reader.pages): |
| text = page.extract_text() |
| text = "" if text is None else clean_text(text) |
|
|
| |
| if len(text) < 30: |
| images = convert_from_path( |
| pdf_path, |
| first_page=page_num + 1, |
| last_page=page_num + 1 |
| ) |
| ocr_text = pytesseract.image_to_string( |
| images[0], |
| lang="ara+eng" |
| ) |
| text = clean_text(ocr_text) |
|
|
| pdf_rows.append({ |
| "name": file_name, |
| "page": page_num + 1, |
| "content": text, |
| "category": category, |
| "char": len(text) |
| }) |
|
|
| except Exception as e: |
| print(f"Error processing {pdf_path}: {e}") |
|
|
| return pdf_rows |
|
|
| |
| all_categories = [f for f in os.listdir(BASE_FOLDER) |
| if os.path.isdir(os.path.join(BASE_FOLDER, f))] |
|
|
| sorted_categories = [] |
| for p_cat in PREFERRED_CATEGORIES: |
| if p_cat in all_categories: |
| sorted_categories.append(p_cat) |
| all_categories.remove(p_cat) |
| sorted_categories.extend(all_categories) |
|
|
| tasks = [] |
| for category in sorted_categories: |
| category_path = os.path.join(BASE_FOLDER, category) |
| files_in_category = [f for f in os.listdir(category_path) |
| if f.lower().endswith(".pdf")] |
|
|
| for file_name in files_in_category: |
| if (category, file_name) in processed_set: |
| continue |
| pdf_path = os.path.join(category_path, file_name) |
| tasks.append((category, pdf_path, file_name)) |
|
|
| print(f"Total PDFs to process: {len(tasks)}") |
| print(f"Using {N_WORKERS} workers") |
|
|
| |
| all_rows = [] |
|
|
| with Pool(N_WORKERS) as pool: |
| for pdf_rows in tqdm(pool.imap_unordered(process_pdf, tasks), |
| total=len(tasks)): |
| if pdf_rows: |
| all_rows.extend(pdf_rows) |
|
|
| |
| df_temp = pd.DataFrame(pdf_rows) |
| write_header = not os.path.exists(OUTPUT_CSV) or df_existing.empty |
| df_temp.to_csv( |
| OUTPUT_CSV, |
| mode='a', |
| header=write_header, |
| index=False |
| ) |
|
|
| print("Processing complete!") |