This library provides a complete workflow for extracting text from PDF documents, processing Arabic content, and creating term-definition datasets. It's designed for researchers working with Arabic texts and PDF documents.
This toolkit consists of four Python scripts that work together to:
- Extract text from PDF files (both text-based and scanned)
- Process Arabic text to extract terms and definitions using AI
- Combine multiple CSV files into a single shuffled dataset
Purpose: Extracts text from text-based PDF files using PyPDF2.
What it does:
- Reads PDF files that contain selectable text
- Extracts text from each page
- Saves the extracted pages as a pickle file for further processing
When to use: Use this for PDFs that already contain text (not scanned images).
Usage:
python pdf_extractor.py <PDF_PATH>Example:
python pdf_extractor.py document.pdfOutput: Creates a pickle file named <filename>_pages.pkl containing a list of page texts.
Purpose: Extracts text from scanned PDF files or image-based PDFs using Mistral AI OCR.
What it does:
- Uploads PDF files to Mistral AI OCR service
- Performs Optical Character Recognition (OCR) on scanned documents
- Extracts text from each page
- Saves the extracted pages as a pickle file
When to use: Use this for scanned PDFs, image-based PDFs, or documents where pdf_extractor.py fails to extract text.
Usage:
python ocr_extractor.py <PDF_PATH>Example:
python ocr_extractor.py scanned_document.pdfOutput: Creates a pickle file named <filename>_texts.pkl containing a list of page texts.
Note: Requires a Mistral API key (see API Keys Setup).
Purpose: Extracts Arabic terms and their definitions from text using OpenAI's GPT models.
What it does:
- Reads a pickle file containing page texts
- Sends each page to OpenAI API for Arabic term extraction
- Extracts terms and definitions in JSON format
- Saves results to a CSV file (
extracted_terms.csv)
When to use: Use this after extracting text from PDFs to create a dataset of Arabic terms and definitions.
Usage:
python create_defs.py <PICKLE_FILE>Example:
python create_defs.py document_pages.pklOutput: Creates/updates extracted_terms.csv with columns: word, definition.
Features:
- Automatic retry on API errors
- Progress tracking
- Summary statistics
- Appends to existing CSV (doesn't overwrite)
Note: Requires an OpenAI API key (see API Keys Setup).
Purpose: Combines multiple CSV files containing word-definition pairs into a single shuffled dataset.
What it does:
- Reads all CSV files from a specified folder
- Validates that files contain
wordanddefinitioncolumns - Removes duplicates
- Shuffles the combined data
- Saves to a single output CSV file
When to use: Use this to merge multiple term-definition CSV files into one dataset for training or analysis.
Usage:
python csv_combinor.py <FOLDER_PATH> <OUTPUT_FILE>Example:
python csv_combinor.py ./csv_files combined_dataset.csvOutput: Creates a shuffled CSV file with all unique word-definition pairs.
Features:
- Automatic duplicate removal
- Empty row filtering
- Progress reporting
- UTF-8 encoding support for Arabic text
-
Clone or download this repository
-
Install Python dependencies:
pip install -r requirements.txt- Set up API keys (see API Keys Setup)
Here's a typical workflow for processing PDF documents:
# For text-based PDFs
python pdf_extractor.py my_document.pdf
# OR for scanned PDFs
python ocr_extractor.py scanned_document.pdfThis creates a pickle file (e.g., my_document_pages.pkl).
python create_defs.py my_document_pages.pklThis creates extracted_terms.csv with Arabic terms and definitions.
python csv_combinor.py ./csv_folder combined_output.csvThis creates a single shuffled dataset from all CSV files in the folder.
PDF Document
β
βββ pdf_extractor.py (text-based PDFs)
β βββ document_pages.pkl
β
βββ ocr_extractor.py (scanned PDFs)
βββ document_texts.pkl
β
βββ create_defs.py
βββ extracted_terms.csv
β
βββ csv_combinor.py (combine multiple CSVs)
βββ combined_dataset.csv
- Python 3.7 or higher
See requirements.txt for the complete list. Main dependencies include:
openai- For GPT-based term extractionpandas- For CSV processingmistralai- For OCR functionalityPyPDF2- For PDF text extraction
Option 1: Environment Variable (Recommended)
# Windows (PowerShell)
$env:OPENAI_API_KEY="your-api-key-here"
# Windows (CMD)
set OPENAI_API_KEY=your-api-key-here
# Linux/Mac
export OPENAI_API_KEY="your-api-key-here"Option 2: Set in script (not recommended for security)
Edit create_defs.py and set the API key directly (not recommended).
Option 1: Environment Variable (Recommended)
# Windows (PowerShell)
$env:MISTRAL_API_KEY="your-api-key-here"
# Windows (CMD)
set MISTRAL_API_KEY=your-api-key-here
# Linux/Mac
export MISTRAL_API_KEY="your-api-key-here"Get API Keys:
- OpenAI: https://platform.openai.com/api-keys
- Mistral: https://console.mistral.ai/
You can modify these constants in the script:
MODEL_NAME: OpenAI model to use (default:"gpt-4o-mini")OUTPUT_CSV: Output CSV filename (default:"extracted_terms.csv")MAX_RETRIES: Maximum retry attempts (default:5)RETRY_DELAY: Delay between retries in seconds (default:10)
-
"API key not set" error
- Make sure you've set the environment variable correctly
- Restart your terminal after setting the variable
-
"File not found" error
- Check that the file path is correct
- Use absolute paths if relative paths don't work
-
Empty extraction results
- For scanned PDFs, use
ocr_extractor.pyinstead ofpdf_extractor.py - Check that the PDF contains readable text
- For scanned PDFs, use
-
CSV encoding issues
- The scripts use UTF-8 encoding for Arabic text
- Open CSV files with a UTF-8 compatible editor (e.g., Excel, VS Code)
-
API rate limits
- The scripts include automatic retry logic
- If you hit rate limits, wait and try again
- All scripts support Arabic text and UTF-8 encoding
- Pickle files are Python-specific binary formats
- CSV files are saved with UTF-8-SIG encoding for Excel compatibility
- The
create_defs.pyscript appends to existing CSV files (doesn't overwrite) - Progress indicators (β
,
β οΈ , β) help track script execution
Feel free to improve the code, add features, or fix bugs. Some ideas:
- Add support for other OCR services
- Implement batch processing for multiple PDFs
- Add progress bars for long operations
- Support for other languages
This project is provided as-is for research and educational purposes.
- OpenAI for GPT models
- Mistral AI for OCR capabilities
- PyPDF2 for PDF text extraction
- pandas for data processing