|
| 1 | +# %% |
| 2 | +import os |
| 3 | +from string import punctuation |
| 4 | + |
| 5 | +import joblib |
| 6 | +import numpy as np |
| 7 | +from nltk.corpus import stopwords |
| 8 | + |
| 9 | +from daswow.CellFeatures import CellFeatures |
| 10 | +from daswow.model_download import download_models_from_github_release |
| 11 | + |
| 12 | +SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 13 | +MODELS_PATH = os.path.join(SCRIPT_DIR, "models") |
| 14 | + |
| 15 | + |
| 16 | +class Preprocessing: |
| 17 | + # init. set dataframe to be processed |
| 18 | + def __init__(self, df): |
| 19 | + self.df = df |
| 20 | + self.features = ["text"] |
| 21 | + self.stopWords = set(stopwords.words("english")) |
| 22 | + |
| 23 | + def remove_stopwords(self, words): |
| 24 | + wordsFiltered = [w for w in words if w not in self.stopWords] |
| 25 | + return wordsFiltered |
| 26 | + |
| 27 | + def set_column(self, col, newcol): |
| 28 | + self.df[newcol] = self.df[col].apply(self.combine_lists_to_text) |
| 29 | + return self.df |
| 30 | + |
| 31 | + def custom_text_preprocessing(self, s): |
| 32 | + favourite_punc = [".", "#", "_"] |
| 33 | + if s: |
| 34 | + for char in punctuation: |
| 35 | + if char not in favourite_punc: |
| 36 | + s = s.replace(char, " ") |
| 37 | + s = " ".join( |
| 38 | + [ |
| 39 | + "" if word.replace(".", "").isdigit() else word |
| 40 | + for word in s.split(" ") |
| 41 | + ] |
| 42 | + ) |
| 43 | + # s = " ".join(['$' if '$' in word and word.replace('$','').isnumeric() else word for word in s.split(' ')]) |
| 44 | + s = " ".join(self.remove_stopwords(s.lower().split(" "))) |
| 45 | + s = " ".join([word.strip() for word in s.split(" ") if len(word) > 1]) |
| 46 | + # s = " ".join([word for word in s if word not in throw_words]) |
| 47 | + return s |
| 48 | + |
| 49 | + def combine_lists_to_text(self, obj): |
| 50 | + text = "" |
| 51 | + if obj: |
| 52 | + try: |
| 53 | + if isinstance(obj, list): |
| 54 | + for element in obj: |
| 55 | + if isinstance(element, list): |
| 56 | + for e in element: |
| 57 | + text = text + " " + str(e) |
| 58 | + else: |
| 59 | + text = text + " " + str(element) |
| 60 | + elif isinstance(obj, str): |
| 61 | + text = text + " " + obj |
| 62 | + except: |
| 63 | + print("expecting string or list, found %s" % type(obj)) |
| 64 | + |
| 65 | + text = text.strip().lower() |
| 66 | + return text |
| 67 | + |
| 68 | + def set_lexical(self, features): |
| 69 | + new_text = [] |
| 70 | + for idx, row in self.df.iterrows(): |
| 71 | + l = [] |
| 72 | + for each in features: |
| 73 | + if isinstance(row[each], list): |
| 74 | + l = l + row[each] |
| 75 | + else: |
| 76 | + l = l + [row[each]] |
| 77 | + new_text.append(l) |
| 78 | + self.df["new_text"] = new_text |
| 79 | + return self.df |
| 80 | + |
| 81 | + def process(self): |
| 82 | + self.df = self.set_lexical(self.features) |
| 83 | + self.df["new_text"] = self.df["text"].apply(self.combine_lists_to_text) |
| 84 | + self.df["new_text"] = self.df["new_text"].apply(self.custom_text_preprocessing) |
| 85 | + return self.df |
| 86 | + |
| 87 | + |
| 88 | +class DASWOWInference: |
| 89 | + def __init__(self, nb_path, models_path=MODELS_PATH): |
| 90 | + cf = CellFeatures() |
| 91 | + self.df = cf.get_cell_features_nb(nb_path) |
| 92 | + |
| 93 | + download_models_from_github_release() |
| 94 | + |
| 95 | + self.preprocesser = Preprocessing(self.df) |
| 96 | + self.model = joblib.load(f"{models_path}/rf_code_scaled.pkl") |
| 97 | + self.tfidf = joblib.load(f"{models_path}/tfidf_vectorizer.pkl") |
| 98 | + self.selector = joblib.load(f"{models_path}/selector.pkl") |
| 99 | + self.ss = joblib.load(f"{models_path}/scaler.pkl") |
| 100 | + self.stopWords = set(stopwords.words("english")) |
| 101 | + self.stat_features = [ |
| 102 | + "linesofcomment", |
| 103 | + "linesofcode", |
| 104 | + "variable_count", |
| 105 | + "function_count", |
| 106 | + ] |
| 107 | + self.labels = [ |
| 108 | + "helper_functions", |
| 109 | + "load_data", |
| 110 | + "data_preprocessing", |
| 111 | + "data_exploration", |
| 112 | + "modelling", |
| 113 | + "evaluation", |
| 114 | + "prediction", |
| 115 | + "result_visualization", |
| 116 | + "save_results", |
| 117 | + "comment_only", |
| 118 | + ] |
| 119 | + |
| 120 | + def remove_stopwords(self, words): |
| 121 | + wordsFiltered = [w for w in words if w not in self.stopWords] |
| 122 | + return wordsFiltered |
| 123 | + |
| 124 | + def preprocess(self): |
| 125 | + self.df = self.preprocesser.process() |
| 126 | + return True |
| 127 | + |
| 128 | + def vectorize(self): |
| 129 | + text = self.tfidf.transform(self.df["new_text"]) |
| 130 | + return text |
| 131 | + |
| 132 | + def select_features(self, text): |
| 133 | + text = self.selector.transform(text) |
| 134 | + return text |
| 135 | + |
| 136 | + def set_statistical_features(self, text): |
| 137 | + X_copy = text.toarray() |
| 138 | + |
| 139 | + for each in self.stat_features: |
| 140 | + X_copy = np.c_[X_copy, self.df[each].values] |
| 141 | + |
| 142 | + return X_copy |
| 143 | + |
| 144 | + def scale_features(self, text): |
| 145 | + text = self.ss.transform(text) |
| 146 | + return text |
| 147 | + |
| 148 | + def predict(self): |
| 149 | + self.preprocess() |
| 150 | + cells_features = self.vectorize() |
| 151 | + cells_features = self.select_features(cells_features) |
| 152 | + cells_features = self.set_statistical_features(cells_features) |
| 153 | + cells_features = self.scale_features(cells_features) |
| 154 | + prediction = self.model.predict(cells_features) |
| 155 | + # convert prediction to labels |
| 156 | + prediction = [ |
| 157 | + [self.labels[i] for i, p in enumerate(pred) if p == 1] |
| 158 | + for pred in prediction |
| 159 | + ] |
| 160 | + return prediction |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + nb_path = "/mnt/Projects/PhD/Research/Student-Thesis/7_Akshita/daswow-data-science-code-analysis/.scrapy/user_study_notebooks/user_study_notebooks/cyclegan-with-data-augmentation.ipynb" |
| 165 | + infer = DASWOWInference( |
| 166 | + nb_path=nb_path, |
| 167 | + ) |
| 168 | + |
| 169 | + infer.predict() |
0 commit comments