File size: 1,289 Bytes
a5fb347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3deb082
a5fb347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from graphCodeBert import GraphCodeBert
from keras.models import load_model, Model
import numpy as np, json

class Predict:

    def __generate_code_embedding(self,code_snippet):
        embedding = np.array(GraphCodeBert().generate_individual_embedding(code_snippet)).reshape((1,768))
        return embedding
    def __calculate_loss(self,code_embedding,model_name):
        model:Model = load_model(f'results/{model_name}.hdf5')
        return model.evaluate(code_embedding,code_embedding)


    def predict(self,code_snippet):
        model_name="autoencoder_25"

        code_embedding = self.__generate_code_embedding(code_snippet)
        print("Input code snippet shape: ",code_embedding.shape)
        loss = self.__calculate_loss(code_embedding,model_name)
        print("Reconstruction Loss: ",loss)

        with open('./results/metrics.json',"r") as fp:
            metric_json = json.loads(fp.read())
        
        threshold = metric_json["Threshold"]

        return "Not a candidate for refactoring" if loss>threshold else "Is a candidate for refactoring", threshold, loss




if __name__=="__main__":
    Predict().predict("""	public void sleep(){
		int s1 = 1;
		int s2 = 2;
		int s3 = 3;
		int s4 = 4;
		int s5 = 5;
		int s6 = 6;
		int s7 = 7;
		int s8 = 8;

	}""")