File size: 7,503 Bytes
1c93e67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

# Import libraries
import gradio as gr
from transformers import AutoTokenizer, AutoModel
from sentence_transformers import SentenceTransformer, util
from pygments import highlight
from pygments.lexers import guess_lexer, TextLexer
from pygments.formatters import HtmlFormatter
import networkx as nx
import torch

# Load the CodeBERT model
model = SentenceTransformer('microsoft/codebert-base')

# Build the knowledge graph
knowledge_graph = nx.DiGraph()
concepts = {
    'Binary Search': ['Arrays', 'Search Algorithms'],
    'Quick Sort': ['Arrays', 'Sorting Algorithms', 'Recursion'],
    'Merge Sort': ['Arrays', 'Sorting Algorithms', 'Recursion'],
    'Dynamic Programming': ['Recursion', 'Algorithms'],
    'Linked List': ['Pointers', 'Data Structures'],
    'Hash Tables': ['Arrays', 'Data Structures'],
    'Graphs': ['Trees', 'Data Structures'],
    'Trees': ['Recursion', 'Data Structures'],
    'Recursion': ['Functions', 'Mathematical Induction'],
    'Object-Oriented Programming': ['Classes', 'Inheritance', 'Polymorphism'],
    'Concurrency': ['Threads', 'Synchronization'],
    # Add more concepts as needed
}

for concept, prereqs in concepts.items():
    for prereq in prereqs:
        knowledge_graph.add_edge(concept, prereq)

# Define functions to get prerequisites
def get_all_prerequisites(concept):
    return list(nx.descendants(knowledge_graph, concept))

# Define the code analysis function
def analyze_code(code_snippet):
    # Generate code embedding
    code_embedding = model.encode(code_snippet, convert_to_tensor=True)

    # Prepare concept embeddings
    concept_names = list(knowledge_graph.nodes())
    concept_embeddings = model.encode(concept_names, convert_to_tensor=True)

    # Compute cosine similarities
    cosine_scores = util.cos_sim(code_embedding, concept_embeddings)[0]

    # Threshold for concept matching
    threshold = 0.4  # Adjust as needed
    matched_concepts = []
    for i, score in enumerate(cosine_scores):
        if score > threshold:
            matched_concepts.append((concept_names[i], float(score)))

    if matched_concepts:
        # Sort concepts by similarity score
        matched_concepts.sort(key=lambda x: x[1], reverse=True)
        # Get top concepts
        top_concepts = [concept for concept, score in matched_concepts]

        return {
            'concepts': top_concepts
        }
    else:
        return {
            'concepts': []
        }

# Highlight code syntax
def highlight_code(code_snippet):
    try:
        lexer = guess_lexer(code_snippet)
    except Exception:
        lexer = TextLexer()
    formatter = HtmlFormatter(style='friendly', full=True, noclasses=True)
    highlighted_code = highlight(code_snippet, lexer, formatter)
    return highlighted_code

# Learning resources mapping
resource_mapping = {
    # Main concepts
    'Binary Search': '[Binary Search Algorithm](https://www.geeksforgeeks.org/binary-search/)',
    'Quick Sort': '[Quick Sort Algorithm](https://www.tutorialspoint.com/data_structures_algorithms/quick_sort_algorithm.htm)',
    'Merge Sort': '[Merge Sort Algorithm](https://www.programiz.com/dsa/merge-sort)',
    'Dynamic Programming': '[Dynamic Programming](https://www.geeksforgeeks.org/dynamic-programming/)',
    'Linked List': '[Linked Lists](https://www.geeksforgeeks.org/data-structures/linked-list/)',
    'Hash Tables': '[Hash Tables](https://www.programiz.com/dsa/hash-table)',
    'Graphs': '[Graph Data Structure](https://www.geeksforgeeks.org/graph-data-structure-and-algorithms/)',
    'Trees': '[Tree Data Structure](https://www.programiz.com/dsa/trees)',
    'Object-Oriented Programming': '[OOP Concepts](https://www.javatpoint.com/python-oops-concepts)',
    'Concurrency': '[Concurrency in Programming](https://www.geeksforgeeks.org/concurrency-in-operating-system/)',
    # Prerequisites
    'Arrays': '[Arrays Tutorial](https://www.geeksforgeeks.org/arrays-in-c-cpp/)',
    'Search Algorithms': '[Search Algorithms](https://www.tutorialspoint.com/data_structures_algorithms/search_algorithms.htm)',
    'Sorting Algorithms': '[Sorting Algorithms](https://www.programiz.com/dsa/sorting-algorithm)',
    'Recursion': '[Understanding Recursion](https://www.freecodecamp.org/news/recursion-in-programming/)',
    'Pointers': '[Pointers in C/C++](https://www.geeksforgeeks.org/pointers-in-c-and-c-set-1-introduction-arithmetic-and-array/)',
    'Data Structures': '[Data Structures Course](https://www.coursera.org/learn/data-structures)',
    'Algorithms': '[Algorithms Course](https://www.edx.org/course/algorithm-design-and-analysis)',
    'Functions': '[Functions in Programming](https://www.programiz.com/python-programming/function)',
    'Mathematical Induction': '[Mathematical Induction](https://brilliant.org/wiki/mathematical-induction/)',
    'Classes': '[Classes and Objects](https://www.learncpp.com/cpp-tutorial/classes-and-class-members/)',
    'Inheritance': '[Inheritance in OOP](https://www.javatpoint.com/cpp-inheritance)',
    'Polymorphism': '[Polymorphism in OOP](https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm)',
    'Threads': '[Multithreading](https://www.geeksforgeeks.org/multithreading-in-cpp/)',
    'Synchronization': '[Thread Synchronization](https://www.ibm.com/docs/en/zos/2.1.0?topic=programming-thread-synchronization)',
    # Add more resources as needed
}

# Define the main app function
def CodeIQ_app(code_snippet, high_level_only):
    analysis = analyze_code(code_snippet)
    concepts = analysis['concepts']

    if not concepts:
        return "No programming concepts identified in the provided code snippet.", "", ""

    # Get resources
    resources = []
    if high_level_only:
        # Show main concepts as prerequisites
        for concept in concepts:
            resource = resource_mapping.get(concept, 'Resource not available.')
            resources.append(f"- **{concept}**: {resource}")
    else:
        # Show all underlying prerequisites
        prerequisites = set()
        for concept in concepts:
            prereqs = get_all_prerequisites(concept)
            prerequisites.update(prereqs)
        for prereq in prerequisites:
            resource = resource_mapping.get(prereq, 'Resource not available.')
            resources.append(f"- **{prereq}**: {resource}")

    # Generate highlighted code
    highlighted = highlight_code(code_snippet)

    # Prepare outputs
    concepts_output = ', '.join(concepts)
    prerequisites_output = '\n'.join(resources)

    return highlighted, concepts_output, prerequisites_output

# Create the Gradio interface
interface = gr.Interface(
    fn=CodeIQ_app,
    inputs=[
        gr.Textbox(lines=20, label="Code Snippet"),
        gr.Checkbox(label="Show only main concepts as prerequisites", value=True)
    ],
    outputs=[
        gr.HTML(label="Highlighted Code"),
        gr.Textbox(label="Identified Concepts"),
        gr.Markdown(label="Prerequisites and Resources")
    ],
    title="CodeIQ - Advanced Code Prerequisite Analyzer",
    description="""
    **Enter a code snippet to identify programming concepts and prerequisites.**

    CodeIQ analyzes your code, highlights the syntax, and provides you with the concepts used and the prerequisites needed to understand those concepts. Use the checkbox to select whether you want to see only the main concepts as prerequisites or all underlying prerequisites. You'll also receive curated resources to help you learn.
    """
)

# Launch the app
interface.launch(debug=True)