A powerful Go application that extracts text from PDF files using OCR when needed, and creates intelligent, meaningful chunks using OpenAI's ChatGPT API.
π Features
- PDF Text Extraction: Extracts text directly from PDF files
- OCR Fallback: Uses Tesseract OCR when direct text extraction fails
- AI-Powered Chunking: Creates intelligent chunks based on themes and content using ChatGPT
- Multi-language Support: Supports English and Indonesian text extraction
- Organized Output: Creates structured chunk directories with numbered files
π Directory Structure
pdf-chunk-extractor/
βββ data/ # Place your PDF files here
βββ output/ # Full extracted text files
βββ chunk/ # AI-processed intelligent chunks
β βββ filename/ # Chunks for each PDF file
β βββ chunk_1.txt
β βββ chunk_2.txt
β βββ ...
βββ main.go
βββ go.mod
βββ README.md
π οΈ Prerequisites
- Go (version 1.16 or higher)
- Tesseract OCR with Indonesian language support
- OpenAI API Key
Installing Tesseract
macOS:
brew install tesseract
brew install tesseract-lang # For additional languages
Ubuntu/Debian:
sudo apt update
sudo apt install tesseract-ocr
sudo apt install tesseract-ocr-ind # Indonesian language pack
Windows:
Download from Tesseract GitHub
π§ Setup
- Clone the repository:
git clone <repository-url>
cd pdf-chunk-extractor
- Install Go dependencies:
go mod tidy
- Set your OpenAI API key:
export OPENAI_API_KEY="your-openai-api-key-here"
- Place PDF files in the data directory:
mkdir -p data
# Copy your PDF files to the data/ directory
π Usage
Run the application:
go run main.go
Or build and run:
go build -o pdf-chunk-extractor
./pdf-chunk-extractor
π Output
The application creates two types of output:
1. Full Text Files (output/)
- Complete extracted text from each PDF
- Includes page separators
- Useful for full document review
2. Intelligent Chunks (chunk/filename/)
- AI-processed chunks based on themes and content
- Each chunk is meaningful and self-contained
- Organized with clear headers and structure
- Perfect for analysis, search, or further processing
π§ AI Chunking Process
The intelligent chunking works as follows:
- Text Extraction: Extracts text from PDF (with OCR fallback)
- Initial Splitting: Splits text into manageable chunks (~4000 characters)
- AI Processing: Sends each chunk to ChatGPT for intelligent organization
- Structured Output: Creates well-formatted chunks with:
- Clear headers and sections
- Logical grouping of related content
- Preserved metadata (document codes, dates)
- Improved readability
π Example Output
Input PDF Content:
--- Page 3 ---
Panen Kelapa Sawit
SOP/KCN-AGR/012/2023
Tujuan
1. Memastikan seluruh Tandan Buah Segar (TBS)...
AI-Processed Chunk:
# Palm Oil Harvesting Standard Operating Procedure
## Document Information
- **SOP Code**: SOP/KCN-AGR/012/2023
- **Document Type**: Standard Operating Procedure
- **Subject**: Palm Oil Harvesting (Panen Kelapa Sawit)
## Purpose (Tujuan)
1. Ensure all harvested Fresh Fruit Bunches (TBS) meet company quality standards
2. Ensure optimal transportation of bunches and loose fruits to Palm Oil Mill (PKS)
## Key Definitions
- **Panen**: Harvesting work of collecting ripe TBS and loose fruits
- **Seksi Panen**: Harvest area that must be completed in one day
- **Interval Panen**: Time between harvests in the same area
βοΈ Configuration
You can modify the following constants in main.go:
const (
DataDir = "data" // Input directory
OutputDir = "output" // Full text output directory
ChunkDir = "chunk" // Chunk output directory
MaxChunkSize = 4000 // Maximum characters per AI chunk
)
π Troubleshooting
Common Issues
-
"OPENAI_API_KEY environment variable is required"
- Set your OpenAI API key:
export OPENAI_API_KEY="your-key"
-
"tesseract command failed"
- Install Tesseract OCR
- Ensure it's in your system PATH
-
OCR quality issues
- Ensure good quality PDFs
- Check if Tesseract language packs are installed
-
API rate limits
- The application includes error handling for API failures
- Falls back to original text if AI processing fails
- Large PDFs: The application processes large files efficiently by chunking them
- API Costs: Each chunk requires an API call, so monitor your OpenAI usage
- Parallel Processing: Consider running multiple instances for different files
π€ Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments