|
| 1 | +import shutil |
| 2 | + |
| 3 | +from info_extractor import InfoExtractor |
| 4 | +import os |
| 5 | +import pandas as pd |
| 6 | +from sklearn.feature_extraction.text import CountVectorizer |
| 7 | +from sklearn.linear_model import LogisticRegression |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +testAlgo = LogisticRegression(solver='lbfgs', multi_class='auto') |
| 11 | + |
| 12 | +trainResumePathDictionary = {} |
| 13 | +trainResumeSkillsDictionary = {} |
| 14 | +trainY = [] |
| 15 | +resumeBaseUrl = "training-data/" |
| 16 | +processingSet = ['FE', 'BE', 'DevOps'] |
| 17 | +dataFrameDictionary = {} |
| 18 | + |
| 19 | +try: |
| 20 | + for currentSet in processingSet: |
| 21 | + currentPath = resumeBaseUrl + currentSet |
| 22 | + trainResumePathDictionary[currentSet] = [os.path.join(currentPath, f) for f in os.listdir(currentPath) if os.path.isfile(os.path.join(currentPath, f))] |
| 23 | +except: |
| 24 | + print('Error') |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +resumeVectorizer = CountVectorizer() |
| 29 | + |
| 30 | +def prepareResumeNameAsIndex(resumesList): |
| 31 | + indexes = {} |
| 32 | + for i in range(len(resumesList)): |
| 33 | + indexes[i] = resumesList[i].split("/")[len(resumesList[i].split("/")) - 1] |
| 34 | + return indexes |
| 35 | + |
| 36 | + |
| 37 | +def prepareOutputClassesForTrainingSet(currentSet): |
| 38 | + if currentSet == 'FE': |
| 39 | + trainY.append(0) |
| 40 | + elif currentSet == 'BE': |
| 41 | + trainY.append(1) |
| 42 | + elif currentSet == 'QA': |
| 43 | + trainY.append(2) |
| 44 | + elif currentSet == 'DevOps': |
| 45 | + trainY.append(3) |
| 46 | + |
| 47 | + |
| 48 | +def extractTrainingText(resumes, currentSet): |
| 49 | + countFilesRead = 0 |
| 50 | + trainResumeSkillsDictionary[currentSet] = [] |
| 51 | + tempSplittedTextForDataFrame = [] |
| 52 | + tempSplittedTextContainerForDataFrame = [] |
| 53 | + currentResumeDataFrame = {} |
| 54 | + for currentResume in resumes: |
| 55 | + countFilesRead += 1 |
| 56 | + if countFilesRead % 100 == 0: |
| 57 | + print("Resumes Read for " + currentSet + " = " + str(countFilesRead)) |
| 58 | + tempSplittedTextForDataFrame = InfoExtractor.extractSkills(currentResume) |
| 59 | + tempSplittedTextContainerForDataFrame.append(tempSplittedTextForDataFrame) |
| 60 | + individualResumeSkills = " ".join(tempSplittedTextForDataFrame) |
| 61 | + trainResumeSkillsDictionary[currentSet].append(individualResumeSkills) |
| 62 | + prepareOutputClassesForTrainingSet(currentSet) |
| 63 | + currentResumeDataFrame = pd.DataFrame(tempSplittedTextContainerForDataFrame) |
| 64 | + tempSplittedTextContainerForDataFrame = [] |
| 65 | + tempSplittedTextForDataFrame = [] |
| 66 | + currentResumeDataFrame.rename(index=prepareResumeNameAsIndex(trainResumePathDictionary[currentSet]), inplace=True) |
| 67 | + return currentResumeDataFrame |
| 68 | + |
| 69 | + |
| 70 | +def trainDataSet(): |
| 71 | + for currentSet in processingSet: |
| 72 | + dataFrameDictionary[currentSet] = extractTrainingText(trainResumePathDictionary[currentSet], currentSet) |
| 73 | + print('----------Extraction completed for dataset: ' + currentSet + '------------') |
| 74 | + |
| 75 | + |
| 76 | +def fetchValuesForTraining(currentDataset): |
| 77 | + tempSkillsToTrainSet = [] |
| 78 | + for currentSet in processingSet: |
| 79 | + tempSkillsToTrainSet += currentDataset[currentSet] |
| 80 | + return tempSkillsToTrainSet |
| 81 | + |
| 82 | + |
| 83 | +def normalizeLanguageForMachine(): |
| 84 | + Resume_Vector = [] |
| 85 | + normalizedData = [] |
| 86 | + |
| 87 | + skillsToTrain = fetchValuesForTraining(trainResumeSkillsDictionary) |
| 88 | + resumeVectorizer.fit(skillsToTrain) |
| 89 | + |
| 90 | + for text in skillsToTrain: |
| 91 | + vector = resumeVectorizer.transform([text]) |
| 92 | + Resume_Vector.append(vector.toarray()) |
| 93 | + |
| 94 | + for x in Resume_Vector: |
| 95 | + normalizedData.append(x[0]) |
| 96 | + |
| 97 | + return normalizedData |
| 98 | + |
| 99 | + |
| 100 | +def classifyTestedResumes(testResumes, predictedResumes): |
| 101 | + resultDestinationBaseUrl = "result/resumes/" |
| 102 | + namesOnly = [] |
| 103 | + predictedNames = [] |
| 104 | + for i in range(len(testResumes)): |
| 105 | + namesOnly.append(testResumes[i].split("/")[len(testResumes[i].split("/")) - 1]) |
| 106 | + for i in range(len(predictedResumes)): |
| 107 | + currentName = namesOnly[i].split("\\")[len(testResumes[i].split("\\")) - 1] |
| 108 | + if predictedResumes[i] == 0: |
| 109 | + classifyResumesInFolders(testResumes[i], resultDestinationBaseUrl + 'FE/' + currentName) |
| 110 | + predictedNames.append("Front End Resume") |
| 111 | + elif predictedResumes[i] == 1: |
| 112 | + classifyResumesInFolders(testResumes[i], resultDestinationBaseUrl + 'BE/' + currentName) |
| 113 | + predictedNames.append("Back End Resume") |
| 114 | + elif predictedResumes[i] == 2: |
| 115 | + classifyResumesInFolders(testResumes[i], resultDestinationBaseUrl + 'QA/' + currentName) |
| 116 | + predictedNames.append("QA Resume") |
| 117 | + elif predictedResumes[i] == 3: |
| 118 | + classifyResumesInFolders(testResumes[i], resultDestinationBaseUrl + 'DevOps/' + currentName) |
| 119 | + predictedNames.append("DevOps Resume") |
| 120 | + return namesOnly, predictedNames |
| 121 | + |
| 122 | + |
| 123 | +def testAndClassifyResumes(): |
| 124 | + resumePathTest = "uploads" |
| 125 | + testResumes = [os.path.join(resumePathTest, f) for f in os.listdir(resumePathTest) if |
| 126 | + os.path.isfile(os.path.join(resumePathTest, f))] |
| 127 | + skillsToTrainTest = [] |
| 128 | + testResume = "" |
| 129 | + for testResume in testResumes: |
| 130 | + testSkills = InfoExtractor.extractSkills(testResume) |
| 131 | + skillsToTrainTest.append(" ".join(testSkills)) |
| 132 | + newArrayToPredict = resumeVectorizer.transform(skillsToTrainTest).toarray() |
| 133 | + predictedResumes = testAlgo.predict(newArrayToPredict) |
| 134 | + return classifyTestedResumes(testResumes, predictedResumes) |
| 135 | + |
| 136 | + |
| 137 | +def trainMachineLearningAlgorithm(normalizedDataForProcessing, trainY): |
| 138 | + trainX = np.array(normalizedDataForProcessing) |
| 139 | + trainY = np.array(trainY) |
| 140 | + trainY = trainY.reshape(-1, 1) |
| 141 | + testAlgo.fit(trainX, trainY) |
| 142 | + print(trainX.shape) |
| 143 | + print(trainY.shape) |
| 144 | + |
| 145 | + |
| 146 | +# "src/data/test/resumes/export_dataframe.csv" |
| 147 | +def getTrainingDataFromCSV(file): |
| 148 | + trainingSetFromCSV = pd.read_csv(file) |
| 149 | + trainYFromFile = np.array(trainingSetFromCSV['outputClass']).reshape(-1, 1) |
| 150 | + trainXFromFile = np.array(trainingSetFromCSV.drop(columns=['outputClass']).values.tolist()) |
| 151 | + print(trainYFromFile.shape) |
| 152 | + print(trainXFromFile.shape) |
| 153 | + return trainXFromFile, trainYFromFile, trainingSetFromCSV |
| 154 | + |
| 155 | + |
| 156 | +def normalizeDataAndWriteToFile(file): |
| 157 | + normalizedDataForProcessing = normalizeLanguageForMachine() |
| 158 | + TransformedResumesData = pd.DataFrame(normalizedDataForProcessing) |
| 159 | + TransformedResumesData = TransformedResumesData.join(pd.DataFrame({'outputClass': trainY})) |
| 160 | + print(TransformedResumesData.shape) |
| 161 | + TransformedResumesData.rename(index=prepareResumeNameAsIndex(fetchValuesForTraining(trainResumePathDictionary)), |
| 162 | + inplace=True) |
| 163 | + # TransformedResumesData.columns = resumeVectorizer.get_feature_names() |
| 164 | + print(TransformedResumesData.shape) |
| 165 | + export_csv = TransformedResumesData.to_csv(file, index=None, header=True) |
| 166 | + return normalizedDataForProcessing |
| 167 | + |
| 168 | +def classifyResumesInFolders(source, destination): |
| 169 | + if not os.path.exists(destination.rsplit('/', 1)[0]): |
| 170 | + os.makedirs(destination.rsplit('/', 1)[0]) |
| 171 | + dest = shutil.copyfile(source, destination) |
| 172 | + |
| 173 | + |
| 174 | +trainDataSet() |
| 175 | + |
| 176 | +normalizedDataForProcessing = normalizeDataAndWriteToFile('training-data/training_data_for_resumes.csv') |
| 177 | + |
| 178 | +trainMachineLearningAlgorithm(normalizedDataForProcessing, trainY) |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
0 commit comments