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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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""
SAIL v2 Data Categorizer
=========================
Automatically classifies training data into semantic categories
using a multi-signal classification pipeline:

  1. Rule-based fast classifier   (keyword patterns, regex)
  2. Statistical classifier       (TF-IDF + Naive Bayes / SVM)
  3. Embedding-based classifier   (sentence-transformers cosine similarity)

Categories:
  text              β€” General prose / articles
  code              β€” Programming code (any language)
  math              β€” Mathematical problems, proofs, equations
  science           β€” Scientific content (biology, chemistry, physics)
  audio_transcript  β€” Transcribed speech from audio
  video_transcript  β€” Transcribed speech from video
  dialogue          β€” Conversational exchanges
  reasoning         β€” Chain-of-thought, logical reasoning
  creative          β€” Fiction, poetry, creative writing
  factual           β€” Encyclopedia, reference material
  instruction       β€” Task instructions, how-to guides, Q&A
  multilingual      β€” Non-English content

Each output JSONL gets a confidence score:
  {"category": "code", "confidence": 0.94, "signals": ["keyword", "embed"], ...}
"""

import os
import re
import json
import math
from typing import Dict, List, Tuple, Optional
from datetime import datetime
from utils.logger import get_logger

logger = get_logger("sail.categorizer")


# ─────────────────────────────────────────────────────────────────────────────
# Rule-Based Fast Classifier
# ─────────────────────────────────────────────────────────────────────────────
class RuleClassifier:
    """Fast O(n) keyword/pattern-based pre-classifier."""

    CODE_PATTERNS = [
        r"def\s+\w+\s*\(",
        r"class\s+\w+\s*[:\(]",
        r"import\s+\w+",
        r"from\s+\w+\s+import",
        r"function\s+\w+\s*\(",
        r"const\s+\w+\s*=",
        r"let\s+\w+\s*=",
        r"var\s+\w+\s*=",
        r"#include\s*<",
        r"fn\s+\w+\s*\(",
        r"public\s+class\s+\w+",
        r"SELECT\s+.+FROM\s+\w+",
        r"<[a-zA-Z]+[^>]*>.*</[a-zA-Z]+>",  # HTML
    ]

    MATH_PATTERNS = [
        r"\\[a-zA-Z]+\{",            # LaTeX
        r"∫|βˆ‘|∏|√|βˆ‚|βˆ‡|∞",          # Math symbols
        r"\$\$.*\$\$",               # LaTeX equations
        r"[A-Z]\s*=\s*[\d\w\s\+\-\*\/\^]+",  # equations
        r"\b(theorem|lemma|proof|corollary|conjecture|axiom)\b",
        r"\b(equation|formula|integral|derivative|matrix|vector)\b",
        r"\d+\s*[\+\-\*\/]\s*\d+\s*=\s*\d+",
    ]

    SCIENCE_KEYWORDS = {
        "biology", "chemistry", "physics", "genome", "dna", "rna",
        "protein", "molecule", "electron", "quantum", "photon",
        "hypothesis", "experiment", "cell", "organism", "reaction",
        "entropy", "relativity", "neuron", "catalyst", "isotope",
    }

    DIALOGUE_PATTERNS = [
        r"^(User|Human|Assistant|AI|Bot|Q|A):\s",
        r"^\-\s.+\n^\-\s",            # bullet dialogue
        r"\[INST\]|\[/INST\]",
        r"<\|user\|>|<\|assistant\|>",
    ]

    REASONING_KEYWORDS = {
        "step 1", "step 2", "therefore", "thus", "because",
        "as a result", "it follows that", "given that", "since",
        "chain of thought", "let me think", "reasoning:", "analysis:",
    }

    CREATIVE_KEYWORDS = {
        "once upon a time", "she whispered", "he said", "the sun set",
        "in a world where", "poem", "verse", "stanza", "rhyme",
        "protagonist", "antagonist", "chapter", "novel", "story",
    }

    TRANSCRIPT_KEYWORDS = {
        "speaker", "transcript", "[crosstalk]", "[inaudible]",
        "[laughter]", "interviewer", "interviewee", "host:", "guest:",
        "caption", "subtitle",
    }

    def __init__(self):
        self.code_re      = [re.compile(p, re.IGNORECASE | re.MULTILINE) for p in self.CODE_PATTERNS]
        self.math_re      = [re.compile(p, re.IGNORECASE | re.MULTILINE) for p in self.MATH_PATTERNS]
        self.dialogue_re  = [re.compile(p, re.IGNORECASE | re.MULTILINE) for p in self.DIALOGUE_PATTERNS]

    def classify(self, text: str, source_category: Optional[str] = None) -> Tuple[str, float]:
        """Returns (category, confidence)"""
        # Use source hint if confident
        if source_category in ("code", "audio_transcript", "video_transcript", "math"):
            return source_category, 0.90

        text_lower = text.lower()

        # Code: pattern matching
        code_hits = sum(1 for p in self.code_re if p.search(text))
        if code_hits >= 3:
            return "code", min(0.65 + code_hits * 0.05, 0.95)

        # Math: pattern + symbol density
        math_hits = sum(1 for p in self.math_re if p.search(text))
        if math_hits >= 2:
            return "math", min(0.60 + math_hits * 0.06, 0.95)

        # Dialogue
        dial_hits = sum(1 for p in self.dialogue_re if p.search(text))
        if dial_hits >= 2:
            return "dialogue", 0.80

        # Transcript
        tr_hits = sum(1 for k in self.TRANSCRIPT_KEYWORDS if k in text_lower)
        if tr_hits >= 2:
            return "audio_transcript", 0.75

        # Reasoning
        rs_hits = sum(1 for k in self.REASONING_KEYWORDS if k in text_lower)
        if rs_hits >= 3:
            return "reasoning", 0.75

        # Science
        sc_hits = sum(1 for k in self.SCIENCE_KEYWORDS if k in text_lower)
        if sc_hits >= 4:
            return "science", min(0.55 + sc_hits * 0.04, 0.90)

        # Creative
        cr_hits = sum(1 for k in self.CREATIVE_KEYWORDS if k in text_lower)
        if cr_hits >= 2:
            return "creative", 0.70

        # Instruction (Q&A format)
        if any(text_lower.startswith(p) for p in ["question:", "q:", "how to", "how do", "what is", "explain"]):
            return "instruction", 0.70

        # Factual (Wikipedia-style)
        if source_category == "factual" or "== history ==" in text_lower or "according to" in text_lower:
            return "factual", 0.65

        # Non-English detection (rough)
        ascii_ratio = sum(1 for c in text if ord(c) < 128) / max(len(text), 1)
        if ascii_ratio < 0.80:
            return "multilingual", 0.70

        return "text", 0.50


# ─────────────────────────────────────────────────────────────────────────────
# Statistical TF-IDF Classifier (boosted confidence)
# ─────────────────────────────────────────────────────────────────────────────
class TFIDFClassifier:
    """
    Fast TF-IDF based secondary classifier.
    Builds a vocabulary from category seed documents,
    then scores new documents against each category profile.
    """

    # Seed terms that are highly indicative of each category
    CATEGORY_SEEDS = {
        "code":             ["function", "variable", "return", "loop", "array", "object", "class", "method", "api", "algorithm", "compile", "syntax", "runtime"],
        "math":             ["theorem", "proof", "equation", "matrix", "integral", "derivative", "polynomial", "algebra", "calculus", "geometry", "statistics", "probability"],
        "science":          ["experiment", "hypothesis", "molecule", "cell", "species", "evolution", "quantum", "particle", "reaction", "biology", "chemistry", "physics"],
        "reasoning":        ["therefore", "because", "conclude", "infer", "logic", "premise", "argument", "analysis", "deduce", "evidence", "reasoning"],
        "creative":         ["story", "poem", "character", "plot", "narrative", "fiction", "imagination", "metaphor", "prose", "verse", "scene", "dialogue"],
        "instruction":      ["step", "tutorial", "guide", "install", "configure", "setup", "how-to", "procedure", "instruction", "follow"],
        "dialogue":         ["conversation", "chat", "response", "question", "answer", "user", "assistant", "message", "exchange"],
        "factual":          ["according", "history", "encyclopedia", "information", "description", "definition", "overview", "summary", "background"],
        "multilingual":     ["le", "la", "de", "en", "es", "der", "die", "das", "il", "est", "una", "este"],
        "audio_transcript": ["speaker", "said", "told", "mentioned", "discussed", "interview", "podcast", "recording"],
        "text":             ["the", "and", "or", "but", "however", "furthermore", "therefore", "article", "report"],
    }

    def __init__(self):
        self._build_profiles()

    def _tf(self, words: List[str]) -> Dict[str, float]:
        counts: Dict[str, int] = {}
        for w in words:
            counts[w] = counts.get(w, 0) + 1
        total = max(len(words), 1)
        return {w: c / total for w, c in counts.items()}

    def _build_profiles(self):
        self.profiles: Dict[str, Dict[str, float]] = {}
        for cat, seeds in self.CATEGORY_SEEDS.items():
            self.profiles[cat] = {w: 1.0 for w in seeds}

    def _cosine(self, a: Dict[str, float], b: Dict[str, float]) -> float:
        keys = set(a) & set(b)
        if not keys:
            return 0.0
        dot  = sum(a[k] * b[k] for k in keys)
        norm_a = math.sqrt(sum(v**2 for v in a.values()))
        norm_b = math.sqrt(sum(v**2 for v in b.values()))
        return dot / (norm_a * norm_b + 1e-9)

    def classify(self, text: str) -> Tuple[str, float]:
        words = re.findall(r'\b[a-z]{3,}\b', text.lower())
        if len(words) < 10:
            return "text", 0.40

        doc_tf = self._tf(words)
        scores = {cat: self._cosine(doc_tf, profile)
                  for cat, profile in self.profiles.items()}

        best_cat  = max(scores, key=scores.get)
        best_score = scores[best_cat]

        # Normalize to confidence
        total = sum(scores.values()) + 1e-9
        confidence = min(best_score / total * len(scores) * 0.5, 0.90)

        return best_cat, confidence


# ─────────────────────────────────────────────────────────────────────────────
# Ensemble Categorizer
# ─────────────────────────────────────────────────────────────────────────────
class EnsembleCategorizer:
    """
    Combines rule-based and TF-IDF classifiers with weighted voting.
    Optionally uses sentence-transformers for high-confidence disambiguation.
    """

    def __init__(self, confidence_threshold: float = 0.75):
        self.rule_clf   = RuleClassifier()
        self.tfidf_clf  = TFIDFClassifier()
        self.threshold  = confidence_threshold
        self._try_load_embed_model()

    def _try_load_embed_model(self):
        """Optionally load sentence-transformers for embedding-based classification."""
        self.embed_model = None
        try:
            from sentence_transformers import SentenceTransformer
            import numpy as np
            self.embed_model = SentenceTransformer("all-MiniLM-L6-v2")
            self._np = np
            logger.info("Sentence-transformers loaded for embedding classification")
            print("  βœ“ Embedding classifier: all-MiniLM-L6-v2 (high accuracy)")
        except ImportError:
            print("  β„Ή  sentence-transformers not installed β€” using rule+TF-IDF only")
            print("     (Install: pip install sentence-transformers for better accuracy)")

    def categorize(self, record: Dict) -> Dict:
        text            = record.get("text", "")
        source_category = record.get("category")

        # Stage 1: Rule-based
        rule_cat, rule_conf = self.rule_clf.classify(text, source_category)

        # Stage 2: TF-IDF
        tfidf_cat, tfidf_conf = self.tfidf_clf.classify(text)

        # Ensemble: weighted vote
        scores: Dict[str, float] = {}
        for cat, conf, weight in [(rule_cat, rule_conf, 0.5), (tfidf_cat, tfidf_conf, 0.4)]:
            scores[cat] = scores.get(cat, 0) + conf * weight

        # Source hint as weak signal
        if source_category in ("code", "math", "audio_transcript", "science"):
            scores[source_category] = scores.get(source_category, 0) + 0.3

        final_cat  = max(scores, key=scores.get)
        final_conf = min(scores[final_cat], 1.0)

        signals = ["rule", "tfidf"]

        # Stage 3: Embedding model for low-confidence cases
        if final_conf < self.threshold and self.embed_model and len(text) > 100:
            embed_cat, embed_conf = self._embed_classify(text)
            if embed_conf > final_conf:
                scores[embed_cat] = scores.get(embed_cat, 0) + embed_conf * 0.5
                final_cat  = max(scores, key=scores.get)
                final_conf = min(scores[final_cat], 1.0)
                signals.append("embed")

        return {
            **record,
            "category":       final_cat,
            "confidence":     round(final_conf, 3),
            "signals":        signals,
            "category_scores": {k: round(v, 3) for k, v in scores.items()},
        }

    def _embed_classify(self, text: str) -> Tuple[str, float]:
        CATEGORY_SENTENCES = {
            "code":        "This is programming code with functions, variables, and algorithms.",
            "math":        "This is a mathematical problem with equations, proofs, and calculations.",
            "science":     "This is scientific content about biology, chemistry, or physics.",
            "reasoning":   "This demonstrates logical reasoning and step-by-step thinking.",
            "creative":    "This is creative writing like fiction, poetry, or storytelling.",
            "instruction": "This is an instruction guide or tutorial with steps to follow.",
            "dialogue":    "This is a conversation or dialogue between people.",
            "factual":     "This is factual encyclopedic information and reference material.",
            "text":        "This is general informational text or an article.",
        }
        try:
            sample = text[:512]
            categories = list(CATEGORY_SENTENCES.keys())
            sentences  = [CATEGORY_SENTENCES[c] for c in categories]
            all_texts  = [sample] + sentences
            embeddings = self.embed_model.encode(all_texts, convert_to_numpy=True)
            doc_emb    = embeddings[0]
            cat_embs   = embeddings[1:]

            def cosine(a, b):
                return float(self._np.dot(a, b) / (self._np.linalg.norm(a) * self._np.linalg.norm(b) + 1e-9))

            sims = [cosine(doc_emb, ce) for ce in cat_embs]
            best_idx = int(self._np.argmax(sims))
            return categories[best_idx], float(sims[best_idx])
        except Exception as e:
            logger.warning(f"Embed classify error: {e}")
            return "text", 0.40


# ─────────────────────────────────────────────────────────────────────────────
# Main DataCategorizer
# ─────────────────────────────────────────────────────────────────────────────
class DataCategorizer:
    def __init__(self, settings):
        self.settings   = settings
        self.input_dir  = settings.categorizer_input_dir
        self.output_dir = settings.categorizer_output_dir
        os.makedirs(self.output_dir, exist_ok=True)

    def run(self):
        print(f"\n{'='*60}")
        print(f"  SAIL DATA CATEGORIZER")
        print(f"{'='*60}")
        print(f"  Input     : {self.input_dir}")
        print(f"  Output    : {self.output_dir}")
        print(f"  Threshold : {self.settings.categorizer_confidence_threshold}")
        print(f"{'='*60}\n")

        if self.settings.dry_run:
            print("  DRY RUN β€” skipping categorization\n")
            return

        categorizer = EnsembleCategorizer(self.settings.categorizer_confidence_threshold)

        total = low_conf = 0
        cat_counts: Dict[str, int] = {}
        out_files:  Dict[str, object] = {}

        for fname in sorted(os.listdir(self.input_dir)):
            if not fname.endswith(".jsonl"):
                continue
            path = os.path.join(self.input_dir, fname)
            print(f"  Processing: {fname}")

            with open(path, "r", encoding="utf-8") as f:
                for line in f:
                    line = line.strip()
                    if not line:
                        continue
                    try:
                        record = json.loads(line)
                    except json.JSONDecodeError:
                        continue

                    result = categorizer.categorize(record)
                    total += 1
                    cat   = result["category"]
                    conf  = result["confidence"]

                    if conf < self.settings.categorizer_confidence_threshold:
                        low_conf += 1

                    cat_counts[cat] = cat_counts.get(cat, 0) + 1

                    # Write to per-category output file
                    out_path = os.path.join(self.output_dir, f"{cat}.jsonl")
                    if cat not in out_files:
                        out_files[cat] = open(out_path, "a", encoding="utf-8")
                    out_files[cat].write(json.dumps(result, ensure_ascii=False) + "\n")

                    if total % 5000 == 0:
                        print(f"    β†’ {total:,} records categorized")

        for f in out_files.values():
            f.close()

        print(f"\n  βœ“ Categorization complete!")
        print(f"    Total records    : {total:,}")
        print(f"    Low confidence   : {low_conf:,} ({100*low_conf/max(total,1):.1f}%)")
        print(f"\n  Category distribution:")
        for cat, n in sorted(cat_counts.items(), key=lambda x: -x[1]):
            bar = "β–ˆ" * min(int(n / max(cat_counts.values()) * 30), 30)
            print(f"    {cat:<20} {n:>8,}  {bar}")

        manifest = {
            "timestamp":       datetime.utcnow().isoformat(),
            "total":           total,
            "low_confidence":  low_conf,
            "categories":      cat_counts,
            "threshold":       self.settings.categorizer_confidence_threshold,
        }
        with open(os.path.join(self.output_dir, "category_manifest.json"), "w") as f:
            json.dump(manifest, f, indent=2)

        return cat_counts