Java project for identifying whether C/C++ source code was written by a human or by LLMs AI.
The project workflow has three steps:
- Extract static code attributes from source files.
- Train a multilayer perceptron network using sigmoid activation function and backpropagation.
- Validate all files in the
validatefolder using the trained model.
src/
CodeAttributeExtractor.java # Shared extraction logic for the 50 attributes
ExtractCodeAttributes.java # Generates training CSV files in trainingData
MultiLayerPerceptron.java # Trains the MLP and saves the model in models
ValidateFile.java # Validates all files in the validate folder
trainingCodebase/
human/ # Human-written source code used for training
ai/ # AI-written source code used for training
trainingData/ # Generated training CSV files
models/ # Trained models saved as .txt files
validate/ # Files to classify
- Java JDK 26, or a compatible JDK for this project.
ExtractCodeAttributes walks through an input folder, filters C/C++ files (.c, .h, .cpp, .hpp, .cc, .cxx, .hh, .hxx), and generates a CSV with 50 attributes per file.
<output_file.csv>: name or path of the CSV file that will receive the extracted attributes.<input_folder>: optional folder containing the source files to analyze.
- If only
<output_file.csv>is provided, the program usestrainingCodebaseas the input folder. - If
<output_file.csv>is only a file name, the CSV is saved insidetrainingData. - If
<input_folder>ishumanorai, the program automatically looks insidetrainingCodebase. - If
<input_folder>is a relative or absolute path, that folder is used directly. - If no argument is provided, the program prints the expected argument format and stops.
- The generated CSV starts with a
filecolumn, followed by the 50 extracted attributes in a fixed order.
MultiLayerPerceptron loads two CSV files: one for the HUMAN class and one for the AI class.
- Class
0=HUMAN - Class
1=AI
The network is a multilayer perceptron implemented in Java. It uses an input layer with one neuron for each extracted attribute, one hidden layer, and a single output neuron.
Each neuron uses the sigmoid activation function:
sigmoid(x) = 1 / (1 + e^(-x))
The output is interpreted as a probability-like score:
- values closer to
0indicateHUMAN; - values closer to
1indicateAI.
Training is performed with backpropagation. For each sample, the network performs a forward pass, calculates the output error, propagates that error back through the hidden layer, and updates weights and biases using the learning rate. Before training, the input attributes are normalized using the mean and standard deviation calculated from the training split. The final model stores the normalization data, all weights, and all biases in a text file.
<human_csv>: CSV file containing features extracted from human-written code.<ai_csv>: CSV file containing features extracted from AI-written code.<model_file.txt>: optional output file for the trained model.
<human_csv>and<ai_csv>are required.- If
<human_csv>or<ai_csv>is only a file name, the program looks for it insidetrainingData. - If
<human_csv>or<ai_csv>is a relative or absolute path, that file is used directly. - If
<model_file.txt>is not provided, the model is saved asmodels/model_mlp.txt. - If
<model_file.txt>is only a file name, it is saved insidemodels. - If fewer than two arguments are provided, the program prints the expected argument format and stops.
ValidateFile validates every C/C++ file found in the validate folder.
The program reads the .txt model file and automatically infers:
- the number of inputs from the size of
MEANSorMEDIAS; - the number of hidden neurons from the size of
W1,B1, andW2.
Validation uses the same CodeAttributeExtractor class used during training data generation, ensuring that both training and prediction use the exact same feature extraction logic.
<model_file.txt>: optional model file to load.<validate_folder>: optional folder containing the source files to classify.
- If no argument is provided, the program loads
models/model_mlp.txtand validates thevalidatefolder. - If
<model_file.txt>is provided as a simple file name, the program looks for it insidemodels. - If
<model_file.txt>is provided as a relative or absolute path, that model file is used directly. - If
<validate_folder>is not provided, the program validates thevalidatefolder. - If
<validate_folder>is provided as a simple folder name, the program first checks that folder directly and can also resolve it insidevalidate. - If
<validate_folder>is provided as a relative or absolute path, that folder is used directly. - If more than two arguments are provided, the program prints the expected argument format and stops.
- If the model file does not exist, validation stops.
- If the validation folder does not exist or contains no C/C++ files, validation stops.
The project uses 50 attributes for each source file.
- Number of classes
- Number of functions
- Number of methods
- Number of imports/includes
- Number of function calls
- Number of returns
- Number of assignments
- Number of variables
- Number of function parameters
- Number of decorators/attributes
- Number of lambdas
- Number of templates
- Number of try/catch blocks
- Number of throws
- Number of asserts
- Number of
ifstatements - Number of
elsestatements - Number of
else ifstatements - Number of
forloops - Number of
whileloops - Number of
breakstatements - Number of
continuestatements - Number of boolean operators
- Number of comparisons
- Number of arithmetic operators
- Average cyclomatic complexity
- Maximum cyclomatic complexity
- Maximum block depth
- Average block depth
- Average function size
- Maximum function size
- Average number of statements per function
- Average number of branches per function
- Total number of lines
- Number of blank lines
- Number of comment lines
- Comment-to-line ratio
- Average line length
- Maximum line length
- Average number of characters per function
- Number of unique identifiers
- Total number of identifiers
- Unique identifier ratio
- Average identifier length
- Maximum identifier length
- Number of
snake_caseidentifiers - Number of
camelCaseidentifiers snake_caseratiocamelCaseratio- Identifier entropy
All attributes are extracted by CodeAttributeExtractor, which is shared by ExtractCodeAttributes and ValidateFile.