CodeSense / codesense /analyzer.py
Yooshiii's picture
Upload 36 files
f8a39f0 verified
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
}
}