Objective
Implement automatic document classification using AI embeddings and rule-based systems. This enables intelligent document routing, automated filing, and content organization.
Step 1: Create Categories Table
Create a table for document categories.
CREATE TABLE document_categories (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
description TEXT,
parent_id INTEGER,
keywords VARCHAR(500),
category_embedding VECTOR(384),
is_active BOOLEAN DEFAULT TRUE,
FOREIGN KEY (parent_id) REFERENCES document_categories(id)
);
Step 2: Create Classifiable Documents Table
Create a table for documents to classify.
CREATE TABLE classifiable_documents (
id INTEGER PRIMARY KEY,
document_pdf PDF,
filename VARCHAR(255) NOT NULL,
extracted_text TEXT,
content_embedding VECTOR(384),
predicted_category_id INTEGER,
prediction_confidence DECIMAL(5, 4),
actual_category_id INTEGER,
classification_status VARCHAR(20) DEFAULT 'pending',
classified_at TIMESTAMP,
verified_by VARCHAR(100),
verified_at TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (predicted_category_id) REFERENCES document_categories(id),
FOREIGN KEY (actual_category_id) REFERENCES document_categories(id)
);
Step 3: Create Classification Rules Table
Define rule-based classification.
CREATE TABLE classification_rules (
id INTEGER PRIMARY KEY,
category_id INTEGER NOT NULL,
rule_name VARCHAR(100) NOT NULL,
rule_type VARCHAR(20) NOT NULL,
pattern VARCHAR(500),
priority INTEGER DEFAULT 100,
is_active BOOLEAN DEFAULT TRUE,
FOREIGN KEY (category_id) REFERENCES document_categories(id)
);
Step 4: Insert Document Categories
Add classification categories.
INSERT INTO document_categories (id, name, description, parent_id, keywords) VALUES
-- Top-level categories
(1, 'Financial', 'Financial documents and reports', NULL, 'invoice,payment,budget,expense,revenue,financial'),
(2, 'Legal', 'Legal documents and contracts', NULL, 'contract,agreement,legal,terms,liability,court'),
(3, 'Human Resources', 'HR-related documents', NULL, 'employee,hiring,benefits,payroll,performance'),
(4, 'Technical', 'Technical documentation', NULL, 'specification,architecture,API,technical,system'),
(5, 'Marketing', 'Marketing and sales materials', NULL, 'campaign,brand,marketing,promotion,sales'),
-- Financial subcategories
(6, 'Invoices', 'Customer and vendor invoices', 1, 'invoice,bill,payment due,amount owed'),
(7, 'Reports', 'Financial reports and statements', 1, 'report,statement,quarterly,annual,P&L'),
(8, 'Budgets', 'Budget documents and forecasts', 1, 'budget,forecast,allocation,spending'),
-- Legal subcategories
(9, 'Contracts', 'Business contracts', 2, 'contract,agreement,parties,effective date'),
(10, 'Policies', 'Company policies', 2, 'policy,guideline,procedure,compliance'),
-- Technical subcategories
(11, 'Specifications', 'Product/system specifications', 4, 'specification,requirements,design,features'),
(12, 'Documentation', 'User and technical documentation', 4, 'documentation,manual,guide,instructions');
Step 5: Generate Category Embeddings
Create embeddings for categories.
UPDATE document_categories
SET category_embedding = EMBED(name || ' ' || COALESCE(description, '') || ' ' || COALESCE(keywords, ''))
WHERE category_embedding IS NULL;
Step 6: Insert Classification Rules
Add keyword-based rules.
INSERT INTO classification_rules (id, category_id, rule_name, rule_type, pattern, priority) VALUES
-- Invoice rules
(1, 6, 'Invoice Number Pattern', 'keyword', 'invoice number', 90),
(2, 6, 'Payment Due Pattern', 'keyword', 'payment due', 85),
(3, 6, 'Amount Due Pattern', 'keyword', 'amount due', 85),
-- Contract rules
(4, 9, 'Agreement Pattern', 'keyword', 'hereby agrees', 90),
(5, 9, 'Parties Pattern', 'keyword', 'between the parties', 88),
(6, 9, 'Effective Date Pattern', 'keyword', 'effective date', 85),
-- Report rules
(7, 7, 'Quarterly Report', 'keyword', 'quarterly report', 90),
(8, 7, 'Financial Statement', 'keyword', 'financial statement', 90),
-- Specification rules
(9, 11, 'Requirements Pattern', 'keyword', 'system requirements', 88),
(10, 11, 'Specification Pattern', 'keyword', 'technical specification', 90);