File size: 14,060 Bytes
993fce6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
import os
import re
import pandas as pd
from tqdm import tqdm

try:
    import spacy
    nlp = spacy.load("en_core_web_sm")
    SPACY_AVAILABLE = True
except Exception:
    SPACY_AVAILABLE = False
    print("⚠  spaCy not available β€” long paragraphs won't be split")
    print("   Run: pip install spacy && python -m spacy download en_core_web_sm")

LONG_PARA_WORDS = 40  # paragraphs over this get spaCy sentence splitting

try:
    from bs4 import BeautifulSoup
    BS4_AVAILABLE = True
except ImportError:
    BS4_AVAILABLE = False
    print("⚠  beautifulsoup4 not found β€” HTML docs will use regex fallback")
    print("   Run: pip install beautifulsoup4 lxml")

SCRIPT_DIR    = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT  = os.path.dirname(os.path.dirname(SCRIPT_DIR))
EXTRACTED_DIR = os.path.join(PROJECT_ROOT, "data", "processed")
SEGMENTED_DIR = os.path.join(PROJECT_ROOT, "data", "segmented")
os.makedirs(SEGMENTED_DIR, exist_ok=True)

MIN_WORDS = 10
MAX_WORDS = 120
MAX_CLAUSES_PER_DOC = 120  

print("=" * 60)
print("HYBRID CLAUSE SEGMENTATION (Rule-based + spaCy)")
print("=" * 60)


# ============================================================
# HELPERS
# ============================================================

def is_html(text):
    return bool(re.search(r'<(html|body|div|p|head|span|table)[^>]*>', text, re.IGNORECASE))

def strip_html(text):
    if BS4_AVAILABLE:
        soup = BeautifulSoup(text, "lxml")
        for tag in soup(["script", "style", "nav", "footer", "header"]):
            tag.decompose()
        return soup.get_text(separator="\n")
    else:
        text = re.sub(r'<(script|style)[^>]*>.*?</(script|style)>', '', text, flags=re.DOTALL|re.IGNORECASE)
        text = re.sub(r'<[^>]+>', ' ', text)
        text = re.sub(r'&[a-z]+;', ' ', text)
        return text

def clean_clause(text):
    text = re.sub(r'\s+', ' ', text)
    return text.strip(' \t\n\r.,;:')

def is_valid(text):
    wc = len(text.split())
    return MIN_WORDS <= wc <= MAX_WORDS

def spacy_split(text):
    """Split long paragraph into sentences using spaCy."""
    if not SPACY_AVAILABLE:
        return [text]
    doc = nlp(text[:900000])
    return [s.text.strip() for s in doc.sents if s.text.strip()]


# ============================================================
# PATTERNS
# ============================================================

# Shared β€” catches BOTH line-start and inline numbered items
# e.g.  "1. text"  or  text running into "2. next clause"
NUMBERED       = re.compile(r'(?m)(?:^|\s)(\d+(\.\d+)*)\.\s+(?=\S)')
SUBBULLET      = re.compile(r'(?m)^\s*(-{1,2}|[*β€’β†’β–ͺ‣–])\s+(?=\S)')

# Legal β€” lettered parens only after newline or sentence boundary
# Avoids matching mid-sentence e.g. "company (a division of...)"
LETTERED_PAREN = re.compile(r'(?:^|\n)\s*\([a-zA-Z]\)\s+(?=\S)', re.IGNORECASE)
EXHIBIT        = re.compile(r'(?m)^\s*[Ee][Xx][Hh][Ii][Bb][Ii][Tt]\s+[\dA-Z][\d\.]*\s*$')
DEFINITION     = re.compile(r'(?m)^\s*["\u201c][A-Z][^""\u201d]{1,60}["\u201d]\s+(means|shall mean|refers to|is defined as)')
INITIALS_LINE  = re.compile(r'(?i)initials?\s*[_\-]{2,}')

# Privacy β€” also catches inline Section/Article references
SECTION_ARTICLE = re.compile(
    r'(?:^|\n)\s*(Section|Article|Amendment|Clause|Schedule)\s+[\dIVXivx]+[\d\.]*',
    re.IGNORECASE
)
LEGAL_KEYWORDS = re.compile(
    r'\b(dispute|claim|controversy|violation|investigation|notice|request|demand|'
    r'obligation|indemnif|terminat|arbitrat|liabilit|penalt|sancti|enforce|'
    r'prohibit|restrict|disclos)\w*\b',
    re.IGNORECASE
)


# ============================================================
# SEGMENTERS
# ============================================================

def segment_academic_hr(text):
    results = []

    if is_html(text):
        text = strip_html(text)

    paragraphs = re.split(r'\n{2,}', text)

    for para in paragraphs:
        para = para.strip()
        if not para or len(para.split()) <= 4:
            continue

        num_match = re.match(r'^\s*(\d+(\.\d+)*)\.\s+', para)

        if num_match:
            lines        = para.splitlines()
            heading_text = re.sub(r'^\s*\d+(\.\d+)*\.\s+', '', lines[0]).strip()
            sub_clauses  = []

            for line in lines[1:]:
                line = line.strip()
                if SUBBULLET.match(line):
                    bullet_text = re.sub(r'^[-β€’*β†’]\s+', '', line).strip()
                    combined    = f"{heading_text}: {bullet_text}" if heading_text else bullet_text
                    sub_clauses.append((combined, "semi_clause_bullet"))
                elif line:
                    heading_text = f"{heading_text} {line}".strip()

            if sub_clauses:
                results.extend(sub_clauses)
            else:
                results.append((heading_text, "numbered_clause"))

        else:
            bullets = SUBBULLET.split(para)
            if len(bullets) > 1:
                for b in bullets:
                    b = b.strip()
                    if b:
                        results.append((b, "bullet_clause"))
            else:
                # spaCy fallback for long unstructured paragraphs
                if SPACY_AVAILABLE and len(para.split()) > LONG_PARA_WORDS:
                    for sent in spacy_split(para):
                        results.append((sent, "spacy_sentence"))
                else:
                    results.append((para, "paragraph"))

    return results


def segment_legal(text):
    results = []

    if is_html(text):
        text = strip_html(text)

    # Remove noise lines
    text = EXHIBIT.sub('', text)
    text = INITIALS_LINE.sub('', text)

    paragraphs = re.split(r'\n{2,}', text)

    for para in paragraphs:
        para = para.strip()
        if not para or len(para.split()) <= 3:
            continue

        # Definitions first
        if DEFINITION.search(para):
            results.append((para, "definition_clause"))
            continue

        # Lettered sub-clauses (a) (b)
        lettered_parts = LETTERED_PAREN.split(para)
        if len(lettered_parts) > 1:
            for part in lettered_parts:
                part = part.strip()
                if part:
                    results.append((part, "lettered_subclause"))
            continue

        # Numbered clauses
        numbered_parts = NUMBERED.split(para)
        if len(numbered_parts) > 1:
            clean_parts = [p.strip() for p in numbered_parts
                           if p and not re.fullmatch(r'\d+(\.\d+)*', p.strip())]
            for part in clean_parts:
                if part:
                    results.append((part, "numbered_clause"))
            continue

        # spaCy fallback for long legal paragraphs
        if SPACY_AVAILABLE and len(para.split()) > LONG_PARA_WORDS:
            for sent in spacy_split(para):
                results.append((sent, "spacy_sentence"))
        else:
            results.append((para, "paragraph"))

    return results


def segment_privacy(text):
    if is_html(text):
        text = strip_html(text)

    # Try Section/Article split first (old-style privacy docs)
    parts = [p.strip() for p in SECTION_ARTICLE.split(text) if p.strip()]

    # If no Section/Article structure β€” doc uses numbered clauses
    # (HIPAA, GDPR, COPPA, BIPA etc.) β€” route through academic_hr
    if len(parts) <= 1:
        raw = segment_academic_hr(text)
        return [
            (c, "legal_keyword_clause" if LEGAL_KEYWORDS.search(c) else lbl)
            for c, lbl in raw
        ]

    results = []
    for part in parts:
        if not part or len(part.split()) <= 4:
            continue

        numbered_parts = NUMBERED.split(part)
        if len(numbered_parts) > 1:
            clean_parts = [p.strip() for p in numbered_parts
                           if p and not re.fullmatch(r'\d+(\.\d+)*', p.strip())]
            for np in clean_parts:
                if np:
                    label = "legal_keyword_clause" if LEGAL_KEYWORDS.search(np) else "numbered_clause"
                    results.append((np, label))
        else:
            label = "legal_keyword_clause" if LEGAL_KEYWORDS.search(part) else "section_clause"
            if SPACY_AVAILABLE and len(part.split()) > LONG_PARA_WORDS:
                for sent in spacy_split(part):
                    results.append((sent, label))
            else:
                results.append((part, label))

    return results


SEGMENTERS = {
    "academic" : segment_academic_hr,
    "hr"       : segment_academic_hr,
    "legal"    : segment_legal,
    "privacy"  : segment_privacy,
}

def segment(text, domain):
    return SEGMENTERS.get(domain, segment_academic_hr)(text)


# ============================================================
# MAIN
# ============================================================
all_clauses  = []
domain_stats = {}

domains = sorted([
    d for d in os.listdir(EXTRACTED_DIR)
    if os.path.isdir(os.path.join(EXTRACTED_DIR, d))
])

for domain in domains:
    domain_dir = os.path.join(EXTRACTED_DIR, domain)
    files      = [f for f in os.listdir(domain_dir) if f.endswith(".txt")]

    print(f"\n[{domain.upper()}] β€” {len(files)} documents")

    domain_count = 0
    skipped      = 0

    for fname in tqdm(files, desc="  Segmenting"):
        fpath = os.path.join(domain_dir, fname)
        try:
            with open(fpath, "r", encoding="utf-8", errors="ignore") as f:
                text = f.read()
        except Exception:
            continue

        if len(text.strip()) < 50:
            skipped += 1
            continue

        doc_clauses = 0
        for clause_text, pattern_label in segment(text, domain):
            if doc_clauses >= MAX_CLAUSES_PER_DOC:
                break
            clause_text = clean_clause(clause_text)
            if not is_valid(clause_text):
                continue
            all_clauses.append({
                "clause_id"     : f"C{len(all_clauses) + 1:06d}",
                "source_doc"    : os.path.splitext(fname)[0],
                "domain"        : domain,
                "raw_text"      : clause_text,
                "word_count"    : len(clause_text.split()),
                "pattern_label" : pattern_label,
            })
            domain_count += 1
            doc_clauses  += 1

    domain_stats[domain] = domain_count
    print(f"  βœ“ {domain_count} clauses  |  {skipped} docs skipped")


# ============================================================
# PASS 2 β€” spaCy sentence splitting on surviving paragraphs
# Any clause tagged "paragraph" that is still over MAX_WORDS
# gets re-split into individual sentences here
# ============================================================
print("\nPass 2 β€” spaCy re-split on long paragraphs...")

if SPACY_AVAILABLE:
    refined = []
    resplit  = 0

    for clause in all_clauses:
        if (clause["pattern_label"] == "paragraph"
                and clause["word_count"] > LONG_PARA_WORDS):
            sents = spacy_split(clause["raw_text"])
            if len(sents) > 1:
                resplit += 1
                for sent in sents:
                    sent = clean_clause(sent)
                    if is_valid(sent):
                        new_clause = clause.copy()
                        new_clause["raw_text"]      = sent
                        new_clause["word_count"]    = len(sent.split())
                        new_clause["pattern_label"] = "spacy_pass2"
                        refined.append(new_clause)
            else:
                refined.append(clause)
        else:
            refined.append(clause)

    # Re-assign sequential clause IDs
    for i, clause in enumerate(refined):
        clause["clause_id"] = f"C{i + 1:06d}"

    all_clauses = refined
    print(f"  βœ“ Re-split {resplit} long paragraphs via spaCy Pass 2")
else:
    print("  ⚠  spaCy not available β€” Pass 2 skipped")

# ============================================================
# SAVE
# ============================================================
df       = pd.DataFrame(all_clauses)
out_path = os.path.join(SEGMENTED_DIR, "clauses_raw.csv")
df.to_csv(out_path, index=False, encoding="utf-8")
print(f"\nβœ“ Saved {len(df)} clauses β†’ {out_path}")


# ============================================================
# REPORT
# ============================================================
print("\n" + "=" * 60)
print("SEGMENTATION REPORT")
print("=" * 60)

for domain, count in domain_stats.items():
    bar = "β–ˆ" * (count // 50)
    print(f"  {domain:<20} {count:>5} clauses  {bar}")

print(f"\n  Total clauses    : {len(df)}")
print(f"  Avg words/clause : {df['word_count'].mean():.1f}")
print(f"  Min / Max words  : {df['word_count'].min()} / {df['word_count'].max()}")

print(f"\n  Pattern breakdown:")
for label, cnt in df['pattern_label'].value_counts().items():
    pct = cnt / len(df) * 100
    print(f"    {label:<28} {cnt:>5}  ({pct:.1f}%)")

print("\n── Sanity Checks ──────────────────────────────────────")
checks = [
    (len(df) < 2000,               "⚠  Low total β€” segmenter merging too much. Lower MIN_WORDS."),
    (len(df) > 10000,              "⚠  High total β€” over-splitting. Raise MIN_WORDS."),
    (df['word_count'].mean() < 10, "⚠  Avg too low β€” many fragments. Raise MIN_WORDS to 10."),
    (df['word_count'].mean() > 50, "⚠  Avg too high β€” clauses under-split."),
]
any_warn = False
for condition, msg in checks:
    if condition:
        print(f"  {msg}")
        any_warn = True
if not any_warn:
    print("  βœ“ All checks passed")