File size: 2,451 Bytes
f8a39f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from codesense.parser import parse_code
from codesense.features import extract_features
from codesense.rules import detect_algorithm
from codesense.complexity import estimate_complexity
from codesense.explanations import generate_explanation

# ============================================================
# STRICT ML IMPORT (Mentor Requirement)
# This file provides the logic, NOT the server.
# ============================================================
from codesense.similarity import predict_algorithm

def analyze_code(source: str) -> dict:
    """

    Main analysis pipeline called by app.py.

    """
    # 1. Structural Analysis via AST
    tree = parse_code(source)
    features = extract_features(tree)
    detection = detect_algorithm(features)
    
    # 2. Semantic Analysis via CodeT5 (Checker 2)
    ml_result = predict_algorithm(source)

    rule_pattern = detection.get("pattern")
    category = detection.get("category")
    ml_prediction = ml_result.get("ml_prediction")
    ml_confidence = ml_result.get("confidence")

    # Override Policy: Does CodeT5 see something the Rules missed?
    resolved_pattern = rule_pattern
    ml_refined = False

    if ml_confidence is not None:
        if (ml_confidence >= 0.93 and ml_prediction != rule_pattern):
            resolved_pattern = ml_prediction
            category = ml_result.get("ml_category")
            ml_refined = True
        elif (ml_confidence >= 0.90 and rule_pattern in ["Linear Iterative", "Nested Iterative"]):
            resolved_pattern = ml_prediction
            ml_refined = True

    # 3. Complexity
    complexity = estimate_complexity(features)

    # Clean up for JSON
    if "function_calls" in features:
        features["function_calls"] = list(features["function_calls"])

    detection["pattern"] = resolved_pattern
    
    base_result = {
        "features": features, 
        "analysis": detection, 
        "complexity": complexity
    }
    explanation = generate_explanation(base_result)

    return {
        "pattern": resolved_pattern,
        "category": category,
        "time_complexity": complexity.get("time_complexity"),
        "summary": explanation.get("summary"),
        "ml_insights": {
            "ml_prediction": ml_prediction,
            "confidence": ml_confidence if ml_confidence is not None else 0.0,
            "refined": ml_refined
        }
    }