pszemraj commited on
Commit
a960c7e
1 Parent(s): fb16dad

Upload calculate_code_readability.py

Browse files
Files changed (1) hide show
  1. calculate_code_readability.py +73 -0
calculate_code_readability.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from itertools import chain
3
+
4
+
5
+ def calculate_readability(code_string:str) -> float:
6
+ code = code_string.splitlines()
7
+
8
+ # Heuristic 1: Line length
9
+ max_line_length = 80
10
+ long_lines = sum(1 for line in code if len(line) > max_line_length)
11
+ long_line_ratio = long_lines / len(code)
12
+
13
+ # Heuristic 2: Identifier length
14
+ min_identifier_length = 2
15
+ max_identifier_length = 20
16
+ identifiers = re.findall(r"\b[a-zA-Z_][a-zA-Z0-9_]*\b", " ".join(code))
17
+ short_identifiers = sum(1 for id in identifiers if len(id) < min_identifier_length)
18
+ long_identifiers = sum(1 for id in identifiers if len(id) > max_identifier_length)
19
+ bad_identifier_ratio = (
20
+ (short_identifiers + long_identifiers) / len(identifiers) if identifiers else 0
21
+ )
22
+
23
+ # Heuristic 3: Comment density
24
+ target_comment_density = 0.15
25
+ comment_lines = sum(1 for line in code if re.search(r"//|/\*|\*/|#", line))
26
+ comment_density = abs(comment_lines / len(code) - target_comment_density)
27
+
28
+ # Heuristic 4: Cyclomatic Complexity (normalized by the number of functions)
29
+ control_structures = re.findall(
30
+ r"\b(if|else|for|while|switch|case|default|continue|break)\b",
31
+ " ".join(code),
32
+ re.I,
33
+ )
34
+ functions = re.findall(r"\b(def|function|func|sub)\b", " ".join(code), re.I)
35
+ cyclomatic_complexity = (len(control_structures) + 1) / (len(functions) + 1)
36
+
37
+ # Heuristic 5: Indentation consistency
38
+ indentation_levels = [
39
+ len(re.match(r"^[\s\t]*", line).group()) for line in code if line.strip() != ""
40
+ ]
41
+ inconsistent_indentation = sum(
42
+ 1
43
+ for i in range(1, len(indentation_levels))
44
+ if indentation_levels[i] - indentation_levels[i - 1] not in {0, 1, -1}
45
+ )
46
+ indentation_inconsistency_ratio = (
47
+ inconsistent_indentation / (len(indentation_levels) - 1)
48
+ if len(indentation_levels) > 1
49
+ else 0
50
+ )
51
+ # Normalize heuristic scores
52
+ normalized_scores = {
53
+ "long_line_ratio": 1 - min(long_line_ratio, 1),
54
+ "bad_identifier_ratio": 1 - min(bad_identifier_ratio, 1),
55
+ "comment_density_deviation": 1 - min(comment_density, 1),
56
+ "normalized_cyclomatic_complexity": 1 / (1 + cyclomatic_complexity),
57
+ "indentation_inconsistency_ratio": 1 - min(indentation_inconsistency_ratio, 1),
58
+ }
59
+
60
+ # Calculate the aggregate score as the average of the normalized scores
61
+ aggregate_score = sum(normalized_scores.values()) / len(normalized_scores)
62
+
63
+ return aggregate_score
64
+
65
+
66
+ # Example usage:
67
+ code_example = """def calculate_readability(code):
68
+ # This function calculates readability
69
+ avg_line_length = sum(len(line) for line in code) / len(code)
70
+ return avg_line_length"""
71
+
72
+ readability_score = calculate_readability(code_example)
73
+ print(readability_score)