File size: 12,396 Bytes
bec2e2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4ccb3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bec2e2c
 
 
f4ccb3e
 
bec2e2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4ccb3e
bec2e2c
f4ccb3e
bec2e2c
 
 
 
 
 
3116f23
bec2e2c
3116f23
bec2e2c
 
 
3116f23
 
f4ccb3e
 
3116f23
 
 
 
 
 
bec2e2c
 
 
 
f4ccb3e
 
bec2e2c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
ClauseGuard β€” Obligation Tracker v3.0
═════════════════════════════════════
FIXED in v3.0:
  β€’ Reduced false positives (filter out generic service descriptions)
  β€’ Better party extraction with role detection
  β€’ Obligation priority scoring
  β€’ Context-aware obligation type detection
"""

import re
from collections import defaultdict
from datetime import datetime, timedelta

# Obligation keywords by category β€” more specific patterns to reduce false positives
OBLIGATION_PATTERNS = {
    "monetary": [
        r"(?:shall|must|will|agrees? to)\s+pay\s+(?:a\s+)?(?:(?:monthly|annual|quarterly)\s+)?(?:fee|amount|sum|payment)?\s*(?:of\s+)?(?:\$[\d,]+(?:\.\d{2})?)",
        r"(?:fee|payment|compensation|reimburs(?:e|ement))\s+(?:of|in the amount of)\s+\$[\d,]+",
        r"(?:shall|must|will)\s+remit\s+\$[\d,]+",
        r"(?:liquidated damages|penalty)\s+(?:of|in the amount of)\s+\$[\d,]+",
        r"(?:shall|must)\s+(?:pay|reimburse)\s+(?:all|any)\s+(?:outstanding|overdue|unpaid)",
    ],
    "compliance": [
        r"(?:shall|must|will)\s+comply\s+with\s+(?:all\s+)?(?:applicable\s+)?(?:laws|regulations|standards|requirements)",
        r"(?:shall|must|will)\s+(?:adhere|conform)\s+to\s+(?:the|all|applicable)",
        r"(?:shall|must|will)\s+(?:obtain|maintain|procure)\s+(?:all\s+)?(?:necessary|required|applicable)\s+(?:approvals?|permits?|licenses?|certifications?)",
        r"(?:shall|must|will)\s+maintain\s+(?:insurance|coverage|bond|policy)",
        r"(?:shall|must|will)\s+ensure\s+(?:compliance|conformance|adherence)",
    ],
    "reporting": [
        r"(?:shall|must|will)\s+(?:report|disclose)\s+(?:to|any)\s+(?:the|supervisory|regulatory)",
        r"(?:shall|must|will)\s+provide\s+(?:regular|monthly|quarterly|annual|periodic)\s+(?:reports?|updates?|statements?)",
        r"(?:shall|must|will)\s+(?:notify|inform)\s+(?:the other party|promptly|immediately|within)",
        r"(?:shall|must|will)\s+deliver\s+(?:a|an|the)\s+(?:report|statement|notice|certificate)",
        r"(?:shall|must|will)\s+provide\s+(?:SOC|audit|compliance)\s+(?:\d+\s+)?(?:Type\s+)?(?:reports?|certificates?)",
    ],
    "delivery": [
        r"(?:shall|must|will)\s+deliver\s+(?:the|all|any)\s+(?:products?|goods?|materials?|deliverables?|services?)",
        r"(?:shall|must|will)\s+(?:furnish|supply)\s+(?:the|all|any)",
        r"(?:shall|must|will)\s+(?:submit|produce|complete)\s+(?:the|all|any)\s+(?:work|deliverables?|results?)",
        r"(?:delivery|performance)\s+(?:date|schedule|deadline|timeline|milestone)",
    ],
    "termination": [
        r"(?:shall|must|will)\s+(?:return|surrender)\s+(?:all|any)\s+(?:materials?|property|documents?|data|information|equipment)",
        r"(?:shall|must|will)\s+(?:destroy|delete|erase)\s+(?:all|any)\s+(?:copies|data|information|records?|materials?)",
        r"(?:shall|must|will)\s+(?:cease|discontinue)\s+(?:all|any)\s+(?:use|access|activities)",
        r"(?:upon|after|following)\s+termination.*(?:shall|must|will)\s+(?:pay|return|destroy|cease)",
        r"(?:surviving|post-termination)\s+obligations?",
    ],
}

# More restrictive β€” patterns that DON'T indicate obligations (false positive filters)
_FALSE_POSITIVE_PATTERNS = [
    r"^(?:the|this)\s+(?:agreement|contract|document)\s+(?:shall|will)\s+(?:be|become|remain)",
    r"(?:shall|will)\s+(?:be\s+)?(?:governed|construed|interpreted)",
    r"(?:shall|will)\s+(?:constitute|represent|mean|include)",
    r"(?:shall|will)\s+(?:not\s+)?(?:be\s+)?(?:deemed|considered|construed)",
    r"(?:shall|will)\s+(?:have|possess)\s+(?:the\s+)?(?:right|authority|power)",
    r"(?:shall|will)\s+(?:survive|remain\s+in\s+(?:effect|force))",
]

# Timeframe extraction
TIME_PATTERNS = [
    (r"within\s+(\d+)\s+(day|week|month|year)s?", "relative"),
    (r"no\s+later\s+than\s+(\d+)\s+(day|week|month|year)s?", "relative"),
    (r"within\s+(\d+)\s+business\s+days?", "business_days"),
    (r"by\s+([A-Z][a-z]+\s+\d{1,2},?\s+\d{4})", "absolute"),
    (r"on\s+or\s+before\s+([A-Z][a-z]+\s+\d{1,2},?\s+\d{4})", "absolute"),
    (r"(\d{1,2}/\d{1,2}/\d{2,4})", "absolute_date"),
    (r"(?:promptly|immediately)(?:\s+(?:upon|after|following))?", "immediate"),
]

PARTY_PATTERNS = [
    r"\b(?:Party A|Party B|Disclosing Party|Receiving Party|Licensor|Licensee|Buyer|Seller|Tenant|Landlord|Employer|Employee|Company|Customer|Vendor|Client|Provider|Contractor)\b",
    r"\b[A-Z][A-Za-z0-9\s&]+?(?:Inc\.?|LLC|Ltd\.?|Limited|Corp\.?|Corporation|PLC|GmbH)\b",
]

# Priority scoring for obligation types
_PRIORITY_MAP = {
    "monetary": 3,
    "termination": 3,
    "compliance": 2,
    "reporting": 2,
    "delivery": 1,
}

# FIX v4.2: Pre-compile obligation patterns at module level (was recompiling per sentence)
_OBLIGATION_PATTERNS_COMPILED = {
    otype: [re.compile(p, re.IGNORECASE) for p in patterns]
    for otype, patterns in OBLIGATION_PATTERNS.items()
}

# FIX v4.2: Pre-compile false positive patterns
_FALSE_POSITIVE_PATTERNS_COMPILED = [re.compile(p, re.IGNORECASE) for p in _FALSE_POSITIVE_PATTERNS]

# FIX v4.2: Pre-compile time patterns
_TIME_PATTERNS_COMPILED = [(re.compile(p, re.IGNORECASE), ptype) for p, ptype in TIME_PATTERNS]

# FIX v4.2: Pre-compile party patterns
_PARTY_PATTERNS_COMPILED = [re.compile(p) for p in PARTY_PATTERNS]


def _is_false_positive(sentence):
    """Check if a sentence is a common false positive (definition/interpretation, not obligation)."""
    for fp in _FALSE_POSITIVE_PATTERNS_COMPILED:
        if fp.search(sentence):
            return True
    return False


def extract_obligations(text):
    """Extract obligations from contract text with false positive filtering."""
    obligations = []

    # Split into sentences
    sentences = re.split(r'(?<=[.!?])\s+(?=[A-Z])', text)

    for sentence in sentences:
        sentence = sentence.strip()
        if len(sentence) < 30 or len(sentence) > 1000:
            continue

        # Skip false positives
        if _is_false_positive(sentence):
            continue

        found_types = set()
        for otype, patterns in _OBLIGATION_PATTERNS_COMPILED.items():
            for pat in patterns:
                if pat.search(sentence):
                    found_types.add(otype)
                    break

        if not found_types:
            continue

        # Extract party (Fix 8: scope to sentence only, reject >40 char strings)
        party = "Unknown"
        # First try structured direction detection
        obligation_direction = _detect_obligation_direction(sentence)
        if obligation_direction:
            party = obligation_direction
        else:
            # Fallback to pattern matching within the sentence
            for pp in _PARTY_PATTERNS_COMPILED:
                m = pp.search(sentence)
                if m:
                    candidate = m.group(0).strip()
                    # Fix 8: Reject party strings >40 chars (header bleed-through)
                    if len(candidate) <= 40:
                        party = candidate
                    break

        # Extract timeframe
        deadline = "Not specified"
        deadline_urgency = 0
        for pat, ptype in _TIME_PATTERNS_COMPILED:
            m = pat.search(sentence)
            if m:
                if ptype == "relative":
                    num = m.group(1)
                    unit = m.group(2)
                    deadline = f"Within {num} {unit}(s)"
                    deadline_urgency = int(num)
                elif ptype == "business_days":
                    num = m.group(1)
                    deadline = f"Within {num} business day(s)"
                    deadline_urgency = int(num)
                elif ptype in ("absolute", "absolute_date"):
                    deadline = m.group(1)
                    deadline_urgency = 1
                elif ptype == "immediate":
                    deadline = "Immediately"
                    deadline_urgency = 0
                break

        for otype in found_types:
            priority = _PRIORITY_MAP.get(otype, 1)
            if deadline_urgency > 0 and deadline_urgency <= 7:
                priority += 1  # Urgent deadlines get higher priority

            obligations.append({
                "type": otype,
                "party": party,
                "description": sentence[:250] + ("..." if len(sentence) > 250 else ""),
                "deadline": deadline,
                "full_text": sentence,
                "priority": priority,
            })

    # Sort by priority (highest first)
    obligations.sort(key=lambda x: x.get("priority", 0), reverse=True)

    return obligations


def _detect_obligation_direction(sentence):
    """Try to detect who bears the obligation from sentence structure."""
    patterns = [
        (r"^(?:The\s+)?(Provider|Company|Licensor|Landlord|Employer|Seller|Vendor)\s+(?:shall|must|will)", None),
        (r"^(?:The\s+)?(Customer|Client|Licensee|Tenant|Employee|Buyer)\s+(?:shall|must|will)", None),
        (r"^(?:Each|Both)\s+part(?:y|ies)\s+(?:shall|must|will)", "Both parties"),
        (r"^(?:Neither|No)\s+party\s+(?:shall|may)", "Neither party"),
    ]
    for pat, override in patterns:
        m = re.search(pat, sentence, re.IGNORECASE)
        if m:
            return override or m.group(1)
    return None


def render_obligations_html(obligations):
    """Render obligations as HTML cards for Gradio."""
    if not obligations:
        return '<div style="padding:16px;color:#6b7280;text-align:center;">No obligations detected.</div>'

    # Group by type
    grouped = defaultdict(list)
    for ob in obligations:
        grouped[ob["type"]].append(ob)

    type_icons = {
        "monetary": "πŸ’°",
        "compliance": "βš–οΈ",
        "reporting": "πŸ“Š",
        "delivery": "πŸ“¦",
        "termination": "πŸ›‘",
    }
    type_colors = {
        "monetary": "#22c55e",
        "compliance": "#f59e0b",
        "reporting": "#3b82f6",
        "delivery": "#8b5cf6",
        "termination": "#ef4444",
    }

    html = '<div style="font-family:system-ui,sans-serif;">'

    # Summary counts
    html += '<div style="display:grid;grid-template-columns:repeat(auto-fit,minmax(120px,1fr));gap:8px;margin-bottom:16px;">'
    for otype, obs in sorted(grouped.items()):
        color = type_colors.get(otype, "#6b7280")
        icon = type_icons.get(otype, "πŸ“‹")
        html += f'''
        <div style="text-align:center;padding:10px;border-radius:8px;background:{color}15;border:1px solid {color}30;">
          <div style="font-size:20px;">{icon}</div>
          <div style="font-size:20px;font-weight:700;color:{color};">{len(obs)}</div>
          <div style="font-size:11px;color:{color};text-transform:capitalize;">{otype}</div>
        </div>
        '''
    html += '</div>'

    # Individual cards
    for otype, obs in sorted(grouped.items()):
        color = type_colors.get(otype, "#6b7280")
        icon = type_icons.get(otype, "πŸ“‹")
        html += f'<h3 style="font-size:14px;color:#374151;margin:16px 0 8px 0;border-bottom:2px solid {color}30;padding-bottom:4px;">{icon} {otype.title()} Obligations</h3>'
        for ob in obs:
            priority = ob.get("priority", 1)
            priority_badge = ""
            if priority >= 3:
                priority_badge = '<span style="font-size:9px;background:#fef2f2;color:#dc2626;padding:1px 4px;border-radius:3px;margin-left:4px;">HIGH PRIORITY</span>'
            elif priority >= 2:
                priority_badge = '<span style="font-size:9px;background:#fefce8;color:#ca8a04;padding:1px 4px;border-radius:3px;margin-left:4px;">MEDIUM</span>'

            html += f'''
            <div style="border:1px solid #e5e7eb;border-left:4px solid {color};border-radius:6px;padding:10px;margin-bottom:8px;background:#fafafa;">
              <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:4px;">
                <span style="font-size:12px;font-weight:600;color:{color};">{ob["party"]}{priority_badge}</span>
                <span style="font-size:11px;color:#6b7280;background:#f3f4f6;padding:2px 8px;border-radius:4px;">{ob["deadline"]}</span>
              </div>
              <p style="font-size:12px;color:#4b5563;margin:0;line-height:1.5;">{ob["description"]}</p>
            </div>
            '''

    html += '</div>'
    return html