Spaces:
Running
Running
Update parser.py
Browse files
parser.py
CHANGED
@@ -33,7 +33,7 @@ def get_category(node, parent=None):
|
|
33 |
if parent and isinstance(parent, ast.Return):
|
34 |
return 'returned_variable'
|
35 |
else:
|
36 |
-
return 'other'
|
37 |
|
38 |
def create_vector(category, level, location, total_lines, parent_path):
|
39 |
"""Create a 6D vector optimized for role similarity, integrating variable roles into category_id."""
|
@@ -43,7 +43,7 @@ def create_vector(category, level, location, total_lines, parent_path):
|
|
43 |
'other': 11, 'elif': 12, 'else': 13, 'except': 14, 'finally': 15, 'return': 16,
|
44 |
'assigned_variable': 17, 'input_variable': 18, 'returned_variable': 19
|
45 |
}
|
46 |
-
category_id = category_map.get(category, 0)
|
47 |
start_line, end_line = location
|
48 |
span = (end_line - start_line + 1) / total_lines
|
49 |
center_pos = ((start_line + end_line) / 2) / total_lines
|
@@ -75,7 +75,10 @@ def parse_node(node, lines, prev_end, level=0, total_lines=None, parent_path=Non
|
|
75 |
if any(line in processed_lines for line in range(start_line, end_line + 1)):
|
76 |
return parts
|
77 |
|
78 |
-
category
|
|
|
|
|
|
|
79 |
counters[category] += 1
|
80 |
node_id = f"{category.capitalize()}[{counters[category]}]"
|
81 |
|
@@ -118,14 +121,17 @@ def parse_node(node, lines, prev_end, level=0, total_lines=None, parent_path=Non
|
|
118 |
for arg in node.args.args:
|
119 |
var_start = start_line # Assume args are on the same line as function def for simplicity
|
120 |
if var_start not in processed_lines:
|
121 |
-
|
122 |
-
|
|
|
|
|
|
|
123 |
parts.append({
|
124 |
-
'category':
|
125 |
'source': f" {arg.arg},", # Indented as part of function
|
126 |
'location': (var_start, var_start),
|
127 |
'level': level + 1,
|
128 |
-
'vector': create_vector(
|
129 |
'parent_path': f"{current_path[0]} -> {var_node_id}",
|
130 |
'node_id': var_node_id
|
131 |
})
|
|
|
33 |
if parent and isinstance(parent, ast.Return):
|
34 |
return 'returned_variable'
|
35 |
else:
|
36 |
+
return 'other' # Default to 'other' for unrecognized nodes
|
37 |
|
38 |
def create_vector(category, level, location, total_lines, parent_path):
|
39 |
"""Create a 6D vector optimized for role similarity, integrating variable roles into category_id."""
|
|
|
43 |
'other': 11, 'elif': 12, 'else': 13, 'except': 14, 'finally': 15, 'return': 16,
|
44 |
'assigned_variable': 17, 'input_variable': 18, 'returned_variable': 19
|
45 |
}
|
46 |
+
category_id = category_map.get(category, 0) # Default to 0 for unknown categories
|
47 |
start_line, end_line = location
|
48 |
span = (end_line - start_line + 1) / total_lines
|
49 |
center_pos = ((start_line + end_line) / 2) / total_lines
|
|
|
75 |
if any(line in processed_lines for line in range(start_line, end_line + 1)):
|
76 |
return parts
|
77 |
|
78 |
+
# Get category, default to 'other' if None
|
79 |
+
category = get_category(node, parent_path[-1] if parent_path else None) or 'other'
|
80 |
+
if category not in counters:
|
81 |
+
category = 'other' # Ensure category exists in counters
|
82 |
counters[category] += 1
|
83 |
node_id = f"{category.capitalize()}[{counters[category]}]"
|
84 |
|
|
|
121 |
for arg in node.args.args:
|
122 |
var_start = start_line # Assume args are on the same line as function def for simplicity
|
123 |
if var_start not in processed_lines:
|
124 |
+
arg_category = get_category(arg, node) or 'input_variable'
|
125 |
+
if arg_category not in counters:
|
126 |
+
arg_category = 'input_variable'
|
127 |
+
counters[arg_category] += 1
|
128 |
+
var_node_id = f"InputVariable[{counters[arg_category]}]"
|
129 |
parts.append({
|
130 |
+
'category': arg_category,
|
131 |
'source': f" {arg.arg},", # Indented as part of function
|
132 |
'location': (var_start, var_start),
|
133 |
'level': level + 1,
|
134 |
+
'vector': create_vector(arg_category, level + 1, (var_start, var_start), total_lines, current_path),
|
135 |
'parent_path': f"{current_path[0]} -> {var_node_id}",
|
136 |
'node_id': var_node_id
|
137 |
})
|