| 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
|
|
|
|
|
|
|
|
|
|
|
| from codesense.similarity import predict_algorithm
|
|
|
| def analyze_code(source: str) -> dict:
|
| """
|
| Main analysis pipeline called by app.py.
|
| """
|
|
|
| tree = parse_code(source)
|
| features = extract_features(tree)
|
| detection = detect_algorithm(features)
|
|
|
|
|
| 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")
|
|
|
|
|
| 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
|
|
|
|
|
| complexity = estimate_complexity(features)
|
|
|
|
|
| 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
|
| }
|
| } |