Update risk_detector.py
Browse files- risk_detector.py +120 -20
risk_detector.py
CHANGED
|
@@ -1,30 +1,48 @@
|
|
|
|
|
| 1 |
from typing import List, Dict, Any
|
| 2 |
import re
|
| 3 |
|
| 4 |
class RiskDetector:
|
| 5 |
def __init__(self):
|
| 6 |
-
# Define risk keywords/patterns
|
| 7 |
self.risk_patterns = {
|
| 8 |
"penalty_clauses": [
|
| 9 |
r"penalty", r"fine", r"liquidated damages", r"breach of contract",
|
| 10 |
-
r"default", r"violation", r"non-compliance"
|
|
|
|
| 11 |
],
|
| 12 |
"termination_risks": [
|
| 13 |
r"terminate", r"termination", r"cancel", r"cancellation",
|
| 14 |
-
r"void", r"null and void", r"rescind"
|
|
|
|
| 15 |
],
|
| 16 |
"liability_risks": [
|
| 17 |
r"liable", r"liability", r"responsible for", r"accountable",
|
| 18 |
-
r"damages", r"compensation", r"indemnify"
|
|
|
|
| 19 |
],
|
| 20 |
"payment_risks": [
|
| 21 |
r"payment default", r"non-payment", r"overdue", r"interest",
|
| 22 |
-
r"late fee", r"collection", r"debt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
]
|
| 24 |
}
|
| 25 |
|
| 26 |
def detect_risks(self, chunks: List[str]) -> List[Dict[str, Any]]:
|
| 27 |
-
"""Detect risky terms across all chunks"""
|
| 28 |
risk_terms = []
|
| 29 |
|
| 30 |
for chunk_idx, chunk in enumerate(chunks):
|
|
@@ -34,44 +52,126 @@ class RiskDetector:
|
|
| 34 |
for pattern in patterns:
|
| 35 |
matches = re.finditer(pattern, chunk_lower, re.IGNORECASE)
|
| 36 |
for match in matches:
|
| 37 |
-
# Get context around the match
|
| 38 |
-
start = max(0, match.start() -
|
| 39 |
-
end = min(len(chunk), match.end() +
|
| 40 |
context = chunk[start:end].strip()
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
risk_terms.append({
|
| 43 |
"term": match.group(),
|
| 44 |
"category": risk_category,
|
| 45 |
"context": context,
|
| 46 |
"chunk_index": chunk_idx,
|
| 47 |
-
"confidence":
|
|
|
|
|
|
|
| 48 |
})
|
| 49 |
|
| 50 |
-
# Remove duplicates and sort by confidence
|
| 51 |
unique_risks = self._deduplicate_risks(risk_terms)
|
| 52 |
-
return sorted(unique_risks, key=lambda x: x["confidence"], reverse=True)
|
| 53 |
|
| 54 |
-
def _calculate_confidence(self, pattern: str, context: str) -> float:
|
| 55 |
-
"""
|
| 56 |
confidence = 0.5 # Base confidence
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
for indicator in high_risk_indicators:
|
| 61 |
-
if indicator in
|
| 62 |
-
confidence += 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
return min(confidence, 1.0)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def _deduplicate_risks(self, risks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 67 |
-
"""Remove duplicate risk terms"""
|
| 68 |
seen = set()
|
| 69 |
unique_risks = []
|
| 70 |
|
| 71 |
for risk in risks:
|
| 72 |
-
key
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
if key not in seen:
|
| 74 |
seen.add(key)
|
| 75 |
unique_risks.append(risk)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
return unique_risks
|
|
|
|
| 1 |
+
# risk_detector.py - Enhanced risk detection for legal documents
|
| 2 |
from typing import List, Dict, Any
|
| 3 |
import re
|
| 4 |
|
| 5 |
class RiskDetector:
|
| 6 |
def __init__(self):
|
| 7 |
+
# Define comprehensive risk keywords/patterns
|
| 8 |
self.risk_patterns = {
|
| 9 |
"penalty_clauses": [
|
| 10 |
r"penalty", r"fine", r"liquidated damages", r"breach of contract",
|
| 11 |
+
r"default", r"violation", r"non-compliance", r"forfeiture",
|
| 12 |
+
r"damages", r"monetary penalty", r"punitive"
|
| 13 |
],
|
| 14 |
"termination_risks": [
|
| 15 |
r"terminate", r"termination", r"cancel", r"cancellation",
|
| 16 |
+
r"void", r"null and void", r"rescind", r"dissolution",
|
| 17 |
+
r"breach", r"expire", r"end", r"cease"
|
| 18 |
],
|
| 19 |
"liability_risks": [
|
| 20 |
r"liable", r"liability", r"responsible for", r"accountable",
|
| 21 |
+
r"damages", r"compensation", r"indemnify", r"hold harmless",
|
| 22 |
+
r"sue", r"legal action", r"claims", r"losses"
|
| 23 |
],
|
| 24 |
"payment_risks": [
|
| 25 |
r"payment default", r"non-payment", r"overdue", r"interest",
|
| 26 |
+
r"late fee", r"collection", r"debt", r"delinquent",
|
| 27 |
+
r"unpaid", r"outstanding", r"arrears"
|
| 28 |
+
],
|
| 29 |
+
"compliance_risks": [
|
| 30 |
+
r"compliance", r"regulatory", r"legal requirement", r"statute",
|
| 31 |
+
r"regulation", r"mandatory", r"must comply", r"obligation",
|
| 32 |
+
r"duty", r"requirement"
|
| 33 |
+
],
|
| 34 |
+
"confidentiality_risks": [
|
| 35 |
+
r"confidential", r"non-disclosure", r"proprietary", r"trade secret",
|
| 36 |
+
r"confidentiality", r"disclosure", r"leak", r"unauthorized use"
|
| 37 |
+
],
|
| 38 |
+
"force_majeure": [
|
| 39 |
+
r"force majeure", r"act of god", r"natural disaster", r"pandemic",
|
| 40 |
+
r"war", r"emergency", r"unforeseeable", r"beyond control"
|
| 41 |
]
|
| 42 |
}
|
| 43 |
|
| 44 |
def detect_risks(self, chunks: List[str]) -> List[Dict[str, Any]]:
|
| 45 |
+
"""Detect risky terms across all chunks with enhanced analysis"""
|
| 46 |
risk_terms = []
|
| 47 |
|
| 48 |
for chunk_idx, chunk in enumerate(chunks):
|
|
|
|
| 52 |
for pattern in patterns:
|
| 53 |
matches = re.finditer(pattern, chunk_lower, re.IGNORECASE)
|
| 54 |
for match in matches:
|
| 55 |
+
# Get context around the match (larger window)
|
| 56 |
+
start = max(0, match.start() - 100)
|
| 57 |
+
end = min(len(chunk), match.end() + 100)
|
| 58 |
context = chunk[start:end].strip()
|
| 59 |
|
| 60 |
+
# Calculate confidence based on context and pattern
|
| 61 |
+
confidence = self._calculate_confidence(pattern, context, chunk)
|
| 62 |
+
|
| 63 |
risk_terms.append({
|
| 64 |
"term": match.group(),
|
| 65 |
"category": risk_category,
|
| 66 |
"context": context,
|
| 67 |
"chunk_index": chunk_idx,
|
| 68 |
+
"confidence": confidence,
|
| 69 |
+
"severity": self._assess_severity(risk_category, context),
|
| 70 |
+
"position_in_chunk": match.start()
|
| 71 |
})
|
| 72 |
|
| 73 |
+
# Remove duplicates and sort by confidence and severity
|
| 74 |
unique_risks = self._deduplicate_risks(risk_terms)
|
| 75 |
+
return sorted(unique_risks, key=lambda x: (x["confidence"], x["severity"]), reverse=True)[:25] # Top 25 risks
|
| 76 |
|
| 77 |
+
def _calculate_confidence(self, pattern: str, context: str, full_chunk: str) -> float:
|
| 78 |
+
"""Enhanced confidence scoring based on context and surrounding text"""
|
| 79 |
confidence = 0.5 # Base confidence
|
| 80 |
|
| 81 |
+
context_lower = context.lower()
|
| 82 |
+
|
| 83 |
+
# Boost confidence for certain high-risk indicators
|
| 84 |
+
high_risk_indicators = [
|
| 85 |
+
"shall", "must", "required", "obligation", "duty", "liable",
|
| 86 |
+
"penalty", "fine", "terminate", "breach", "void", "damages"
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
medium_risk_indicators = [
|
| 90 |
+
"may", "could", "should", "potential", "possible", "risk"
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
# Legal certainty indicators
|
| 94 |
+
legal_certainty = [
|
| 95 |
+
"hereby", "whereas", "therefore", "notwithstanding", "pursuant to"
|
| 96 |
+
]
|
| 97 |
+
|
| 98 |
for indicator in high_risk_indicators:
|
| 99 |
+
if indicator in context_lower:
|
| 100 |
+
confidence += 0.15
|
| 101 |
+
|
| 102 |
+
for indicator in medium_risk_indicators:
|
| 103 |
+
if indicator in context_lower:
|
| 104 |
+
confidence += 0.08
|
| 105 |
+
|
| 106 |
+
for indicator in legal_certainty:
|
| 107 |
+
if indicator in context_lower:
|
| 108 |
+
confidence += 0.10
|
| 109 |
+
|
| 110 |
+
# Pattern-specific confidence adjustments
|
| 111 |
+
if pattern in ["penalty", "fine", "liquidated damages"]:
|
| 112 |
+
confidence += 0.2 # Financial penalties are high risk
|
| 113 |
+
elif pattern in ["terminate", "void", "rescind"]:
|
| 114 |
+
confidence += 0.15 # Contract termination is high risk
|
| 115 |
+
elif pattern in ["liable", "responsible for", "accountable"]:
|
| 116 |
+
confidence += 0.12 # Liability is significant risk
|
| 117 |
+
|
| 118 |
+
# Context length penalty (very short contexts are less reliable)
|
| 119 |
+
if len(context) < 50:
|
| 120 |
+
confidence -= 0.1
|
| 121 |
|
| 122 |
return min(confidence, 1.0)
|
| 123 |
|
| 124 |
+
def _assess_severity(self, risk_category: str, context: str) -> float:
|
| 125 |
+
"""Assess the severity of the risk based on category and context"""
|
| 126 |
+
severity_map = {
|
| 127 |
+
"penalty_clauses": 0.9,
|
| 128 |
+
"liability_risks": 0.85,
|
| 129 |
+
"termination_risks": 0.8,
|
| 130 |
+
"compliance_risks": 0.75,
|
| 131 |
+
"payment_risks": 0.7,
|
| 132 |
+
"confidentiality_risks": 0.6,
|
| 133 |
+
"force_majeure": 0.5
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
base_severity = severity_map.get(risk_category, 0.5)
|
| 137 |
+
|
| 138 |
+
# Adjust based on context
|
| 139 |
+
context_lower = context.lower()
|
| 140 |
+
|
| 141 |
+
if any(word in context_lower for word in ["immediate", "immediately", "urgent", "critical"]):
|
| 142 |
+
base_severity += 0.1
|
| 143 |
+
|
| 144 |
+
if any(word in context_lower for word in ["substantial", "significant", "major", "severe"]):
|
| 145 |
+
base_severity += 0.08
|
| 146 |
+
|
| 147 |
+
if any(word in context_lower for word in ["minor", "minimal", "limited"]):
|
| 148 |
+
base_severity -= 0.1
|
| 149 |
+
|
| 150 |
+
return min(base_severity, 1.0)
|
| 151 |
+
|
| 152 |
def _deduplicate_risks(self, risks: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
| 153 |
+
"""Remove duplicate risk terms with improved logic"""
|
| 154 |
seen = set()
|
| 155 |
unique_risks = []
|
| 156 |
|
| 157 |
for risk in risks:
|
| 158 |
+
# Create a more sophisticated key for deduplication
|
| 159 |
+
key = (
|
| 160 |
+
risk["term"].lower().strip(),
|
| 161 |
+
risk["category"],
|
| 162 |
+
risk["chunk_index"]
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
if key not in seen:
|
| 166 |
seen.add(key)
|
| 167 |
unique_risks.append(risk)
|
| 168 |
+
else:
|
| 169 |
+
# If duplicate found, keep the one with higher confidence
|
| 170 |
+
existing_idx = next(
|
| 171 |
+
i for i, r in enumerate(unique_risks)
|
| 172 |
+
if (r["term"].lower().strip(), r["category"], r["chunk_index"]) == key
|
| 173 |
+
)
|
| 174 |
+
if risk["confidence"] > unique_risks[existing_idx]["confidence"]:
|
| 175 |
+
unique_risks[existing_idx] = risk
|
| 176 |
|
| 177 |
return unique_risks
|