This project implements a readability analysis framework for Java code snippets, combining software metrics with machine learning classification.
The tool analyzes Java code snippets (.jsnp files) and extracts several well-known software metrics that correlate with readability.
It then builds a labeled dataset using human readability scores and applies a machine learning classifier to predict readability.
The following readability metrics are implemented:
-
Number of Lines (LOC)
Counts the total number of lines in a snippet (including comments and blanks). -
Halstead Volume
- Derived from the number of distinct and total operators (
n1,N1) and operands (n2,N2). - Formula:
[ V = (N1 + N2) \times \log_2(n1 + n2) ]
- Derived from the number of distinct and total operators (
-
Cyclomatic Complexity
- Starts at 1, and increments for each control flow element:
if,for,while,do-while,foreach,switchcases,catchblocks, ternary?:, and logical connectors (&&,||).
- Starts at 1, and increments for each control flow element:
-
Token Entropy
- Uses Shannon entropy over token distributions (including whitespace and special symbols).
- Captures predictability vs. diversity of tokens.
-
Preprocessing (Dataset Creation)
- Extracts the selected feature metrics from
.jsnpfiles. - Reads a ground truth CSV containing human readability ratings.
- Converts ratings into binary labels:
- Average score ≥ 3.6 →
Y(Readable) - Average score < 3.6 →
N(Not Readable)
- Average score ≥ 3.6 →
- Saves the output as a CSV dataset.
- Extracts the selected feature metrics from
-
Classification
- Loads the dataset with Weka.
- Trains a Logistic Regression classifier.
- Evaluates via 10-fold cross-validation.
- Reports Accuracy, Area Under ROC, and F-Score.
The project is packaged as a runnable JAR with subcommands (using Picocli).
java -jar Readability-Analysis-1.0.jar preprocess \
--source ./snippets \
--ground-truth ./truth.csv \
--target ./dataset.csv \
lines h_volume token_entropy cyclomatic_complexity