scripts-historical-judge
#!/usr/bin/env python3 """Historical Judge - Compares against historical classification patterns."""
from typing import Optional from .base import BaseJudge, JudgeDecision
class HistoricalJudge(BaseJudge): """Compares against historical classifications of similar documents."""
name = "historical"
has_veto_authority = False
weight = 0.7
def __init__(self, learner=None):
super().__init__()
self.learner = learner
def evaluate(self, document, votes, consensus) -> JudgeDecision:
if not self.learner:
return JudgeDecision(
judge=self.name,
approved=True,
reason="No learning system available",
confidence=0.5,
metadata={'no_learner': True}
)
doc_path = str(getattr(document, 'path', ''))
classification = consensus.classification if consensus.classification else ""
try:
similar = self.learner.find_similar_by_path(doc_path, limit=5)
if similar:
# Check if similar documents have different types
type_counts = {}
for doc in similar:
actual = doc.get('actual_type')
if actual:
type_counts[actual] = type_counts.get(actual, 0) + 1
if type_counts:
most_common = max(type_counts, key=type_counts.get)
if most_common != classification:
return JudgeDecision(
judge=self.name,
approved=False,
reason=f"Similar documents classified as '{most_common}' not '{classification}'",
confidence=0.6,
metadata={'historical_type': most_common, 'current': classification, 'count': type_counts[most_common]}
)
return JudgeDecision(
judge=self.name,
approved=True,
reason=f"Consistent with historical pattern ({most_common})",
confidence=0.75,
metadata={'matches': type_counts}
)
except Exception as e:
return JudgeDecision(
judge=self.name,
approved=True,
reason=f"Historical check failed: {e}",
confidence=0.5,
metadata={'error': str(e)}
)
return JudgeDecision(
judge=self.name,
approved=True,
reason="No conflicting historical patterns",
confidence=0.6,
metadata={'no_conflicts': True}
)