Download docs/PDF_GLOSSARY_EXTRACTION_GUIDE.md from Norelad/TraductAL: direct link, hf CLI and curl.
- Browser
- Download file 13.8 kB
-
https://huggingface.co/spaces/Norelad/TraductAL/resolve/main/docs/PDF_GLOSSARY_EXTRACTION_GUIDE.md
- Command line
-
hf download hf://spaces/Norelad/TraductAL/docs/PDF_GLOSSARY_EXTRACTION_GUIDE.md
-
curl -L -o PDF_GLOSSARY_EXTRACTION_GUIDE.md https://huggingface.co/spaces/Norelad/TraductAL/resolve/main/docs/PDF_GLOSSARY_EXTRACTION_GUIDE.md
A newer version of the Gradio SDK is available: 6.32.0
๐ Historical Glossary PDF Extraction Guide
Tool: glossary_extractor.py
Purpose: Extract lexical data from historical Swiss French dialect glossaries (PDF format)
Example: Glossaire vaudois (1861)
๐ฏ What This Tool Does
Converts historical PDF glossaries into structured CSV data for your Swiss French dataset:
PDF Glossary (1861) โ CSV Database โ Swiss French Dataset
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ
โ PANOSSE, f. โ โ panosse,serpโ โ {"Prompt": "Trans- โ
โ Serpilliรจre โ โ โ illiรจre,vaudโ โ โ late: panosse", โ
โ โ โ ,feminine,...โ โ "Answer": "serp- โ
โ LINGE, m. โ โ linge,servieโ โ illiรจre"} โ
โ Serviette โ โ tte,vaud,...โ โโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
๐ Quick Start
Step 1: Install PDF Library
Choose one (or install all for best compatibility):
# Option 1: PyPDF2 (simplest, most compatible)
pip install PyPDF2
# Option 2: pdfplumber (best for complex layouts)
pip install pdfplumber
# Option 3: PyMuPDF (best for OCR'd/scanned PDFs)
pip install pymupdf
# Recommended: Install all three
pip install PyPDF2 pdfplumber pymupdf
Step 2: Extract Your Glossary
# Basic extraction
python3 glossary_extractor.py \
--pdf glossaire_vaudois_1861.pdf \
--dialect vaud
# This creates:
# datasets/swiss_french/Raw_Data/extracted_vaud_glossary.csv
Step 3: Review and Import
# Review the CSV
head datasets/swiss_french/Raw_Data/extracted_vaud_glossary.csv
# Import into dataset
python3 swiss_french_dataset_builder.py \
--dialect vaud \
--import-csv datasets/swiss_french/Raw_Data/extracted_vaud_glossary.csv
# Check statistics
python3 swiss_french_dataset_builder.py --stats
๐ Detailed Usage
Basic Extraction
python3 glossary_extractor.py --pdf your_glossary.pdf --dialect vaud
Output:
- โ CSV file with structured data
- โ Statistics about extraction
- โ Sample entries displayed
Save Raw Text for Review
If extraction quality is poor, save raw text first:
python3 glossary_extractor.py \
--pdf glossaire_vaudois_1861.pdf \
--save-raw raw_text.txt
Then review raw_text.txt to:
- See what the PDF extractor sees
- Identify patterns in the glossary structure
- Find where the actual glossary starts (after preface)
Process Pre-extracted Text
After reviewing, process the text directly:
python3 glossary_extractor.py \
--raw-text raw_text.txt \
--dialect vaud \
--output vaud_dictionary.csv
Save JSON for Review
To review entries before CSV export:
python3 glossary_extractor.py \
--pdf glossaire.pdf \
--dialect vaud \
--json review_entries.json
Review review_entries.json to check:
- Entry parsing accuracy
- Definition extraction
- Standard French detection
Manual Glossary Start
If auto-detection fails, manually specify start line:
# First, save raw text and find the line number
python3 glossary_extractor.py --pdf glossaire.pdf --save-raw raw.txt
# Open raw.txt, find where glossary starts (e.g., line 120)
# Then extract from that line
python3 glossary_extractor.py \
--raw-text raw.txt \
--start-line 120 \
--dialect vaud
๐ Supported Glossary Formats
The tool recognizes common historical glossary patterns:
Format 1: "WORD, gender. Definition"
PANOSSE, f. Serpilliรจre pour laver le sol
LINGE, m. Serviette de toilette
Format 2: "WORD (gender) Definition"
PANOSSE (f.) Serpilliรจre pour laver le sol
LINGE (m.) Serviette de toilette
Format 3: "WORD. โ Definition"
PANOSSE. โ Serpilliรจre pour laver le sol
LINGE. โ Serviette de toilette
Format 4: "WORD, Definition"
Panosse, serpilliรจre
Linge, serviette
๐ง Troubleshooting
Issue 1: No Text Extracted
Symptoms: "Extracted 0 characters"
Causes:
- PDF is image-based (scanned) without OCR
- PDF has security restrictions
- Encoding issues
Solutions:
A. Run OCR on the PDF first:
# Install ocrmypdf
pip install ocrmypdf
# OCR the PDF (creates searchable text layer)
ocrmypdf glossaire_original.pdf glossaire_ocr.pdf --language fra
# Then extract
python3 glossary_extractor.py --pdf glossaire_ocr.pdf --dialect vaud
B. Use online OCR:
- Upload to Google Drive โ Open with Google Docs โ Download as PDF
- Or use: https://www.onlineocr.net/
C. Try different PDF library:
# PyMuPDF is often best for scanned PDFs
pip install pymupdf
python3 glossary_extractor.py --pdf glossaire.pdf --dialect vaud
Issue 2: Poor Entry Parsing
Symptoms: "Parsed 0 entries" or very few entries
Solutions:
A. Review raw text:
python3 glossary_extractor.py --pdf glossaire.pdf --save-raw raw.txt
less raw.txt # Review the structure
B. Check glossary format:
Look for the entry pattern in raw.txt. If it's different from supported formats, you may need to:
Edit the entry patterns in
glossary_extractor.py:# Around line 30-45 ENTRY_PATTERNS = [ # Add your custom pattern here r'^YOUR_PATTERN$', # ... existing patterns ]Or do manual preprocessing:
- Edit
raw.txtto normalize entries - Then run:
python3 glossary_extractor.py --raw-text raw.txt
- Edit
C. Manually specify start:
# Find where glossary actually starts
grep -n "^A\." raw.txt # Find first "A." section
# Extract from that line
python3 glossary_extractor.py --raw-text raw.txt --start-line 150
Issue 3: Incorrect Standard French Extraction
Symptoms: standard_french column is empty or wrong
Solution: Manual post-processing
# Extract first
python3 glossary_extractor.py --pdf glossaire.pdf --output temp.csv
# Manually review and fill in standard_french column in Excel/LibreOffice
libreoffice temp.csv
# Then import
python3 swiss_french_dataset_builder.py --dialect vaud --import-csv temp.csv
Issue 4: Special Characters Garbled
Symptoms: "รจ" becomes "รยจ", accents broken
Solutions:
A. Force UTF-8 encoding:
# Edit glossary_extractor.py around line 150
# Change: open(output, 'w', encoding='utf-8')
# To: open(output, 'w', encoding='utf-8', errors='replace')
B. Convert encoding:
# After extraction, convert
iconv -f ISO-8859-1 -t UTF-8 output.csv > output_utf8.csv
๐ก Best Practices
1. Start with Small Test
# Extract and save raw text first
python3 glossary_extractor.py --pdf glossaire.pdf --save-raw raw.txt
# Review first 100 lines
head -100 raw.txt
# Check if it looks correct before full extraction
2. Use Two-Stage Process
# Stage 1: Extract to JSON for review
python3 glossary_extractor.py \
--pdf glossaire.pdf \
--json entries.json
# Stage 2: Review JSON, then convert to CSV
# (manually if needed, or re-run with adjustments)
3. Quality Check
After extraction:
# Check number of entries
wc -l extracted_vaud_glossary.csv
# View sample
head -20 extracted_vaud_glossary.csv
# Check for obvious errors
grep "???" extracted_vaud_glossary.csv # OCR errors often have ???
4. Iterative Refinement
For best results:
- First pass: Extract with defaults
- Review: Check sample entries
- Adjust: Modify patterns if needed
- Re-extract: Run again with adjustments
- Manual cleanup: Fix remaining issues in CSV
- Import: Load into dataset
๐ Expected Output
CSV Format
swiss_french,standard_french,dialect,part_of_speech,definition,source,notes
panosse,serpilliรจre,vaud,feminine noun,Serpilliรจre pour laver le sol,Glossaire vaud (1861),Line 150
linge,serviette,vaud,masculine noun,Serviette de toilette,Glossaire vaud (1861),Line 151
Statistics
============================================================
๐ EXTRACTION STATISTICS
============================================================
PDF file: glossaire_vaudois_1861.pdf
Dialect: vaud
Total entries extracted: 1,247
Part of speech distribution:
masculine noun: 542
feminine noun: 423
verb: 187
adjective: 95
Entries with standard French: 892 (71.5%)
Sample entries:
PANOSSE (feminine noun): Serpilliรจre pour laver le sol...
LINGE (masculine noun): Serviette de toilette...
๐ฏ Real-World Example: Glossaire Vaudois (1861)
Scenario
You have: glossaire_vaudois_1861.pdf (reproduced from original 1861 edition)
Structure:
- Pages 1-10: Cover, title page, preface
- Pages 11-250: Glossary entries (A-Z)
- Format: "WORD, gender. Definition in French"
Process
Step 1: OCR (if needed)
# Check if text is extractable
python3 glossary_extractor.py --pdf glossaire_vaudois_1861.pdf --save-raw test.txt
head test.txt
# If empty or garbled, run OCR
ocrmypdf glossaire_vaudois_1861.pdf glossaire_vaudois_1861_ocr.pdf --language fra
Step 2: Extract
python3 glossary_extractor.py \
--pdf glossaire_vaudois_1861_ocr.pdf \
--dialect vaud \
--save-raw raw_vaud.txt \
--json vaud_entries.json
Expected output:
๐ Extracting text from: glossaire_vaudois_1861_ocr.pdf
Pages: 250
โ
Extracted 245,892 characters
๐งน Cleaning text...
Cleaned 3,847 lines from 4,123 total
๐ Identifying glossary start...
Found glossary start at line 145: ABAISER, v. Abaisser
๐ Parsing entries from line 145...
โ
Parsed 1,247 entries
โจ Enriching entries...
โ
Enriched 1,247 entries
============================================================
๐ EXTRACTION STATISTICS
============================================================
PDF file: glossaire_vaudois_1861_ocr.pdf
Dialect: vaud
Total entries extracted: 1,247
๐พ Saving to CSV: datasets/swiss_french/Raw_Data/extracted_vaud_glossary.csv
โ
Saved 1,247 entries
๐ Next step:
python3 swiss_french_dataset_builder.py --dialect vaud --import-csv datasets/swiss_french/Raw_Data/extracted_vaud_glossary.csv
Step 3: Review
# Check quality
head -20 datasets/swiss_french/Raw_Data/extracted_vaud_glossary.csv
# Review JSON for accuracy
less vaud_entries.json
Step 4: Import
python3 swiss_french_dataset_builder.py \
--dialect vaud \
--import-csv datasets/swiss_french/Raw_Data/extracted_vaud_glossary.csv
python3 swiss_french_dataset_builder.py --stats
Expected result:
๐ DATASET STATISTICS
Dictionary entries: 2,494 (1,247 from glossary ร 2 directions)
Total examples: 2,494
Progress: 5.4% vs. Romansh dataset
๐ Success Metrics
Your extraction is successful if:
- โ Extraction rate: >80% of visual entries captured
- โ Accuracy: >90% of entries correctly parsed
- โ Standard French: >60% automatically extracted
- โ Clean data: <5% OCR errors (???, garbled text)
Typical Results
| Glossary Type | Expected Entries | Extraction Rate | Manual Cleanup |
|---|---|---|---|
| Clean modern PDF | 1,000-2,000 | 95%+ | Minimal |
| Scanned (with OCR) | 1,000-2,000 | 80-90% | 10-20% entries |
| Old/poor quality | 1,000-2,000 | 60-80% | 20-40% entries |
๐ Next Steps After Extraction
1. Import to Dataset
python3 swiss_french_dataset_builder.py \
--dialect vaud \
--import-csv extracted_vaud_glossary.csv
2. Check Progress
python3 swiss_french_dataset_builder.py --stats
3. Generate Training Examples
From 1,247 dictionary entries, you get:
- 2,494 bidirectional translation pairs
- ~3,741 with idiom identification
- Total: ~3,700 training examples from one glossary!
4. Find More Glossaries
Search for:
- Other Swiss French dialect glossaries (Geneva, Fribourg, Neuchรขtel, Jura)
- GPSR entries (online or PDF)
- Historical regional dictionaries
- Academic linguistic publications
๐ Resources
OCR Tools
- ocrmypdf:
pip install ocrmypdf(command-line) - Google Drive: Upload PDF โ Open with Google Docs โ Download
- Adobe Acrobat: Built-in OCR
- Online: https://www.onlineocr.net/
Historical Glossaries
Search terms for finding more:
- "glossaire vaudois"
- "patois romand"
- "dialecte suisse romand"
- "glossaire genevois"
- "parler fribourgeois"
Archives:
- GPSR: https://www.gpsr.ch/
- e-rara: https://www.e-rara.ch/ (Swiss historical books)
- Gallica: https://gallica.bnf.fr/ (French archives)
PDF Libraries
# Install all for maximum compatibility
pip install PyPDF2 pdfplumber pymupdf
โ Summary
Tool created: โ
glossary_extractor.py
Capabilities:
- Extract text from PDF (3 library options)
- Parse glossary entries (4 common formats)
- Extract metadata (word, gender, definition, standard French)
- Export to CSV (dataset-compatible format)
- Statistics and quality metrics
Expected impact:
- 1 glossary = 1,000-2,000 entries
- 1,000 entries = 2,000-3,000 training examples
- 3-4 glossaries = Minimum viable dataset (5,000+ examples)
Your Glossaire vaudois (1861) could provide ~3,700 training examples โ that's 74% of your Phase 1 milestone!
Ready to extract your glossary! ๐โ๐