Skip to content

leosmsilvx/code-authorship-identifier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Authorship Identifier

Java project for identifying whether C/C++ source code was written by a human or by LLMs AI.

The project workflow has three steps:

  1. Extract static code attributes from source files.
  2. Train a multilayer perceptron network using sigmoid activation function and backpropagation.
  3. Validate all files in the validate folder using the trained model.

Project Structure

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

Requirements

1. Extract Code Attributes

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.

Arguments

  • <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.

Argument Rules

  • If only <output_file.csv> is provided, the program uses trainingCodebase as the input folder.
  • If <output_file.csv> is only a file name, the CSV is saved inside trainingData.
  • If <input_folder> is human or ai, the program automatically looks inside trainingCodebase.
  • 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 file column, followed by the 50 extracted attributes in a fixed order.

2. Train the MLP

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 0 indicate HUMAN;
  • values closer to 1 indicate AI.

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.

Arguments

  • <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.

Argument Rules

  • <human_csv> and <ai_csv> are required.
  • If <human_csv> or <ai_csv> is only a file name, the program looks for it inside trainingData.
  • 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 as models/model_mlp.txt.
  • If <model_file.txt> is only a file name, it is saved inside models.
  • If fewer than two arguments are provided, the program prints the expected argument format and stops.

3. Validate Files

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 MEANS or MEDIAS;
  • the number of hidden neurons from the size of W1, B1, and W2.

Validation uses the same CodeAttributeExtractor class used during training data generation, ensuring that both training and prediction use the exact same feature extraction logic.

Arguments

  • <model_file.txt>: optional model file to load.
  • <validate_folder>: optional folder containing the source files to classify.

Argument Rules

  • If no argument is provided, the program loads models/model_mlp.txt and validates the validate folder.
  • If <model_file.txt> is provided as a simple file name, the program looks for it inside models.
  • 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 the validate folder.
  • If <validate_folder> is provided as a simple folder name, the program first checks that folder directly and can also resolve it inside validate.
  • 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.

4. Extracted Attributes

The project uses 50 attributes for each source file.

Structural Attributes

  1. Number of classes
  2. Number of functions
  3. Number of methods
  4. Number of imports/includes
  5. Number of function calls
  6. Number of returns
  7. Number of assignments
  8. Number of variables
  9. Number of function parameters
  10. Number of decorators/attributes
  11. Number of lambdas
  12. Number of templates
  13. Number of try/catch blocks
  14. Number of throws
  15. Number of asserts

Control-Flow Attributes

  1. Number of if statements
  2. Number of else statements
  3. Number of else if statements
  4. Number of for loops
  5. Number of while loops
  6. Number of break statements
  7. Number of continue statements
  8. Number of boolean operators
  9. Number of comparisons
  10. Number of arithmetic operators

Complexity Attributes

  1. Average cyclomatic complexity
  2. Maximum cyclomatic complexity
  3. Maximum block depth
  4. Average block depth
  5. Average function size
  6. Maximum function size
  7. Average number of statements per function
  8. Average number of branches per function

Size and Density Attributes

  1. Total number of lines
  2. Number of blank lines
  3. Number of comment lines
  4. Comment-to-line ratio
  5. Average line length
  6. Maximum line length
  7. Average number of characters per function

Identifier and Style Attributes

  1. Number of unique identifiers
  2. Total number of identifiers
  3. Unique identifier ratio
  4. Average identifier length
  5. Maximum identifier length
  6. Number of snake_case identifiers
  7. Number of camelCase identifiers
  8. snake_case ratio
  9. camelCase ratio
  10. Identifier entropy

All attributes are extracted by CodeAttributeExtractor, which is shared by ExtractCodeAttributes and ValidateFile.

About

Identifies whether C/C++ source code was written by a human or AI using static code attributes and a MLP classifier.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages