Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ user-interface/package-lock.json
user-interface/.eslintcache

# IDE
.vscode
.vscode
backend/uploads
194 changes: 157 additions & 37 deletions backend/feature_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import re
import zipfile
import subprocess

import CppHeaderParser
from project_metadata import ProjectMetadata
from collections import Counter

def create_dir(path):
if not (os.path.isdir(path)):
Expand Down Expand Up @@ -57,6 +59,10 @@ def get_regex_counts(filename, regex_pattern):

def create_csv(trainDir, outfilename, students):
output = open(outfilename, "w")
projectMetadata = ProjectMetadata()

#list of keywords
keywords = ['sizeof', 'break', 'case', 'char', 'continue', 'do', 'default', 'while', 'double', 'int', 'struct', 'switch', 'else', 'long', 'return', 'for', 'void', 'if', 'float', 'static', 'typedef']

output.write(
"nr_crt,label,nr_clase,nr_errors,nr_inheritance,nr_virtual,nr_static,")
Expand All @@ -81,6 +87,12 @@ def create_csv(trainDir, outfilename, students):
class_number = len(headers)
sources_number = len(sources)

#append sources, headers and files count to metadata
projectMetadata.sourcesCount += sources_number
projectMetadata.headersCount += class_number
projectMetadata.filesCount += sources_number + class_number


if not (os.path.isfile(trainDir + std + "/codying_style.txt")):
command = ["cpplint", "--recursive", local_dir + "/sources/"]
output_filename = trainDir + std + "/codying_style.txt"
Expand Down Expand Up @@ -111,6 +123,12 @@ def create_csv(trainDir, outfilename, students):
struct_pattern = re.compile(r"W*(stuct)\W*")
function_pattern = re.compile(r"\W*(\(\))\W*")

keywords_pattern = {}

for keyword in keywords:
keywords_pattern[keyword] = re.compile("\W*(" + keyword + ")\W*")
projectMetadata.keyWordsList[keyword] = 0

inheritance_count = 0
virtual_count = 0
static_count = 0
Expand All @@ -127,45 +145,121 @@ def create_csv(trainDir, outfilename, students):
enum_count = 0
struct_count = 0
function_count = 0
constructor_count = 0
destructor_count = 0
constructor_param = 0
default_constructor = 0
pureVirtualMethodsCount = 0
overloadingFunctionsCount = 0
abstract_count = 0
multipleInheritanceCount = 0
copyConstructorCount = 0
moveConstructorCount = 0
publicMethodsCount = 0
privateMethodsCount = 0
protectedMethodsCount = 0
interfacesCount = 0


sources_size = 0
headers_size = 0
for source in sources:
sources_size += os.path.getsize(local_dir + "/sources/" + source)

sourceHeader = local_dir + "/sources/" + source
projectMetadata.sourcesLinesCount += sum(1 for line in open(sourceHeader, "rb"))
sources_size += os.path.getsize(sourceHeader)

#get numbers of keywords in source files
for key in keywords_pattern:
keywords_count = get_regex_counts(sourceHeader, keywords_pattern[key])
projectMetadata.keyWordsList[key] += keywords_count

for header in headers:
inheritance_count += get_regex_counts(
local_dir + "/headers/" + header, inheritance_pattern)
virtual_count += get_regex_counts(local_dir + "/headers/" + header,
virtual_pattern)
static_count += get_regex_counts(local_dir + "/headers/" + header,
static_pattern)
global_count += get_regex_counts(local_dir + "/headers/" + header,
global_pattern)
public_count += get_regex_counts(local_dir + "/headers/" + header,
public_pattern)
private_count += get_regex_counts(local_dir + "/headers/" + header,
private_pattern)
protected += get_regex_counts(local_dir + "/headers/" + header,
protected_pattern)
define_count += get_regex_counts(local_dir + "/headers/" + header,
define_pattern)
template_count += get_regex_counts(
local_dir + "/headers/" + header, template_pattern)
stl_count += get_regex_counts(local_dir + "/headers/" + header,
stl_pattern)
namespace_count += get_regex_counts(
local_dir + "/headers/" + header, namespace_pattern)
comments_count += get_regex_counts(
local_dir + "/headers/" + header, comments_pattern)
enum_count += get_regex_counts(local_dir + "/headers/" + header,
enum_pattern)
struct_count += get_regex_counts(local_dir + "/headers/" + header,
struct_pattern)

function_count += get_regex_counts(
local_dir + "/headers/" + header, function_pattern)
headers_size += os.path.getsize(local_dir + "/headers/" + header)
headerPath = local_dir + "/headers/" + header

#get number of contructors
cppHeader = CppHeaderParser.CppHeader(headerPath)

#get numbers of constructors, destructors and constructors with parameters
for classname in cppHeader.classes.keys():
pureVirtualMethodsPerClass = 0
#number of public Methods
publicMethodsCount += len(cppHeader.classes[classname]['methods']['public'])
privateMethodsCount += len(cppHeader.classes[classname]['methods']['private'])
protectedMethodsCount += len(cppHeader.classes[classname]['methods']['protected'])

for oneMethod in cppHeader.classes[classname]['methods']['public']:
if oneMethod['name'] == classname:
if oneMethod['constructor'] == True:
constructor_count += 1
if len(oneMethod['parameters']):
constructor_param += 1
else:
default_constructor += 1
if len(oneMethod['parameters']) == 1:
if ((classname + " &") == oneMethod['parameters'][0]['type']) or (("const " + classname + " &") == oneMethod['parameters'][0]['type']):
#print(oneMethod['parameters'][0]['type'])
copyConstructorCount += 1
if ((classname + " & &") == oneMethod['parameters'][0]['type']):
#print(oneMethod['parameters'][0]['type'])
moveConstructorCount += 1
if oneMethod['destructor'] == True:
destructor_count += 1
if oneMethod['operator'] != False:
projectMetadata.overloadedOperatorsList[oneMethod['operator']] = 'Found'

#get all pure virtual methods
pureVirtualMethods = cppHeader.classes[classname].get_all_pure_virtual_methods()
pureVirtualMethodsPerClass = len(pureVirtualMethods)
pureVirtualMethodsCount += pureVirtualMethodsPerClass

#number of interfaces
if len(cppHeader.classes[classname]['methods']) == pureVirtualMethodsPerClass:
interfacesCount += 1

#get number of overloaded functions
overloadingMethods = cppHeader.classes[classname].get_all_method_names()
dictionaryOverloading = Counter(overloadingMethods)
for key in dictionaryOverloading:
if dictionaryOverloading[key] > 2:
overloadingFunctionsCount += 1

#get number of abstract classes
abstract_count += cppHeader.classes[classname]['abstract']

#get number of class derived from multiple classes
#list of classes that this inherits
inheritsClassesList = cppHeader.classes[classname]['inherits']
#print(inheritsClassesList)
if len(inheritsClassesList) > 1:
multipleInheritanceCount += 1



projectMetadata.headersLinesCount += sum(1 for line in open(headerPath, "rb"))

inheritance_count += get_regex_counts(headerPath, inheritance_pattern)
virtual_count += get_regex_counts(headerPath, virtual_pattern)
static_count += get_regex_counts(headerPath, static_pattern)
global_count += get_regex_counts(headerPath, global_pattern)
public_count += get_regex_counts(headerPath, public_pattern)
private_count += get_regex_counts(headerPath, private_pattern)
protected += get_regex_counts(headerPath, protected_pattern)
define_count += get_regex_counts(headerPath, define_pattern)
template_count += get_regex_counts(headerPath, template_pattern)
stl_count += get_regex_counts(headerPath, stl_pattern)
namespace_count += get_regex_counts(headerPath, namespace_pattern)
comments_count += get_regex_counts(headerPath, comments_pattern)
enum_count += get_regex_counts(headerPath, enum_pattern)
struct_count += get_regex_counts(headerPath, struct_pattern)

#get numbers of keywords in header files
for key in keywords_pattern:
keywords_count = get_regex_counts(headerPath, keywords_pattern[key])
projectMetadata.keyWordsList[key] += keywords_count

function_count += get_regex_counts(headerPath, function_pattern)
headers_size += os.path.getsize(headerPath)


to_Write = []
to_Write.append(nrCrt)
Expand All @@ -191,6 +285,28 @@ def create_csv(trainDir, outfilename, students):
to_Write.append(headers_size)
to_Write.append(sources_size)

#append informations to metadata object
projectMetadata.classesCount += class_number
projectMetadata.derivedClassesCount += inheritance_count
projectMetadata.virtualFunctionsCount += virtual_count
projectMetadata.privateMethodsCount += privateMethodsCount
projectMetadata.publicMethodsCount += publicMethodsCount
projectMetadata.protectedMethodsCount += protectedMethodsCount
projectMetadata.stlElementsCount += stl_count
projectMetadata.templateClassesCount += template_count
projectMetadata.linesCount += projectMetadata.headersLinesCount + projectMetadata.sourcesLinesCount
projectMetadata.constructorsCount += constructor_count
projectMetadata.destructorsCount += destructor_count
projectMetadata.parametersConstructorCount += constructor_param
projectMetadata.defaultConstructorsCount += default_constructor
projectMetadata.pureVirtualFunctionsCount += pureVirtualMethodsCount
projectMetadata.overloadedFunctionsCount += overloadingFunctionsCount
projectMetadata.abstractClassesCount += abstract_count
projectMetadata.multipleDerivedClassesCount += multipleInheritanceCount
projectMetadata.copyConstructorsCount += copyConstructorCount
projectMetadata.moveConstructorsCount += moveConstructorCount
projectMetadata.interfacesCount += interfacesCount

for w in to_Write:
output.write(str(w) + ",")
output.write("\n")
Expand All @@ -199,6 +315,9 @@ def create_csv(trainDir, outfilename, students):

output.close()

#todo: replace this with a filled object
return projectMetadata


def extract_zip(zipPath, destPath, scope):
with zipfile.ZipFile(zipPath, "r") as zip_ref:
Expand Down Expand Up @@ -267,14 +386,15 @@ def retrain_data_one(path_to_new_label):
if (label == newLabel[0]):
newLabels.append(label)

create_csv(trainDir, "../data/featuresRetrained.csv", newLabels)
filesMetadata = create_csv(trainDir, "../data/featuresRetrained.csv", newLabels)
new_data = merge_csv("../data/featuresRetrained.csv",
"../data/features.csv")
os.remove("../data/featuresRetrained.csv")

new_data = (new_data[0].split(","))[1:-1]

return new_data
# returns new data and metadata about the uploaded project
return { "new_data" : new_data, "files_metadata" : filesMetadata }


def add_new_line_csv(csv_file, data):
Expand Down
81 changes: 81 additions & 0 deletions backend/project_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
class ProjectMetadata:
def __init__(self):
self.interfacesCount = 0
self.abstractClassesCount = 0
self.classesCount = 0
self.derivedClassesCount = 0
self.multipleDerivedClassesCount = 0

self.filesCount = 0
self.sourcesCount = 0
self.headersCount = 0
self.linesCount = 0
self.sourcesLinesCount = 0
self.headersLinesCount = 0
self.keyWordsList = {}
self.singletonClassesCount = 0 #unmaped

self.pureVirtualFunctionsCount = 0
self.virtualFunctionsCount = 0
self.overloadedFunctionsCount = 0
self.privateMethodsCount = 0
self.publicMethodsCount = 0
self.protectedMethodsCount = 0
self.overridingFunctionsCount = 0 #unmaped

self.overloadedOperatorsList = {}
self.constructorsCount = 0
self.defaultConstructorsCount = 0
self.parametersConstructorCount = 0
self.copyConstructorsCount = 0
self.moveConstructorsCount = 0
self.destructorsCount = 0

self.stlElementsCount = 0
self.templateClassesCount = 0
self.lambdaFunctionsCount = 0 #unmaped
self.identifiersLengthMean = 0 #unmaped
self.emptySpacesCount = 0 #unmaped
self.readmeWordsCount = 0 #unmpaed

def toDictionary(self):
return {
"interfacesCount" : self.interfacesCount,
"abstractClassesCount" : self.abstractClassesCount,
"classesCount" : self.classesCount,
"derivedClassesCount" : self.derivedClassesCount,
"multipleDerivedClassesCount" : self.multipleDerivedClassesCount,

"filesCount" : self.filesCount,
"sourcesCount" : self.sourcesCount,
"headersCount" : self.headersCount,
"linesCount" : self.linesCount,
"sourcesLinesCount" : self.sourcesLinesCount,
"headersLinesCount" : self.headersLinesCount,
"keyWordsList" : self.keyWordsList,
"singletonClassesCount" : self.singletonClassesCount,

"pureVirtualFunctionsCount" : self.pureVirtualFunctionsCount,
"virtualFunctionsCount" : self.virtualFunctionsCount,
"overloadedFunctionsCount" : self.overloadedFunctionsCount,
"privateMethodsCount" : self.privateMethodsCount,
"publicMethodsCount" : self.publicMethodsCount,
"protectedMethodsCount" : self.protectedMethodsCount,

"overloadedOperatorsList" : self.overloadedOperatorsList,
"overridingFunctionsCount" : self.overridingFunctionsCount,

"constructorsCount" : self.constructorsCount,
"defaultConstructorsCount" : self.defaultConstructorsCount,
"parametersConstructorCount" : self.parametersConstructorCount,
"copyConstructorsCount" : self.copyConstructorsCount,
"moveConstructorsCount" : self.moveConstructorsCount,
"destructorsCount" : self.destructorsCount,

"stlElementsCount" : self.stlElementsCount,
"templateClassesCount" : self.templateClassesCount,
"lambdaFunctionsCount" : self.lambdaFunctionsCount,
"identifiersLengthMean" : self.identifiersLengthMean,
"emptySpacesCount" : self.emptySpacesCount,
"readmeWordsCount" : self.readmeWordsCount
}
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Flask_Cors==3.0.10
Flask==1.1.2
pycryptodome==3.9.9
joblib==0.17.0
cpplint==1.5.4
cpplint==1.5.4
CppHeaderParser
6 changes: 3 additions & 3 deletions backend/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def predict_route():
zip_file.extractall(extraction_full_path)

# Get features
features = feature_extraction.retrain_data_one(extraction_full_path + "/")
features = preprocessor.transform_entry(features)
retrainResult = feature_extraction.retrain_data_one(extraction_full_path + "/")
features = preprocessor.transform_entry(retrainResult['new_data'])

# Predict the grade
grade = predictor.predict([features])[0]
Expand All @@ -71,7 +71,7 @@ def predict_route():
grades_df.to_csv(GRADES_CSV_FILENAME, index=False)

# Return a result
result = {"predicted_grade": grade}
result = {"predicted_grade": grade, "metadata" : retrainResult["files_metadata"].toDictionary() }
return jsonify(result)


Expand Down
4 changes: 3 additions & 1 deletion user-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
"@testing-library/react": "^11.2.5",
"@testing-library/user-event": "^12.6.3",
"axios": "^0.21.1",
"chart.js": "^3.7.0",
"react": "^17.0.1",
"react-bootstrap": "^1.4.3",
"react-chartjs-2": "^3.3.0",
"react-dom": "^17.0.1",
"react-icons": "^4.1.0",
"react-scripts": "4.0.1",
Expand Down Expand Up @@ -38,4 +40,4 @@
"last 1 safari version"
]
}
}
}
Loading