def nonTechnicalKeywordSeelector(names,project): name_list = names.split(',') print("name list") print(name_list) db_list = [] unique_filtered_word_nontech = set() for val in name_list: lower_case_value = val.lower() unique_filtered_word_nontech.add(lower_case_value) print(unique_filtered_word_nontech) print("error in yesterday") for value in unique_filtered_word_nontech: db_availability = ConnectionToNeo4j.getMatchingTopicsNonTech(value) print("error in now") if db_availability == True: db_list.append(value) print(db_list) print("it is printed") if len(db_list) > 0: db_string_list = ','.join(db_list) print(db_string_list) print("error in that") return db_string_list else: db = "CV" project_tech_list = ConnectionToNeo4j.cvProjectTech(db,project) print("error in this") return project_tech_list
def ValidatingTechnical(userAnswer, dbAnswer ,qno): sentence1 = userAnswer sentence2 = dbAnswer grammarMarks = WeightOfTheAnswer.process_content(sentence1) if len(sentence1.split()) == 0: return ['None', ''] else: stopWords = set(stopwords.words('english')) words1 = word_tokenize(sentence1) words2 = word_tokenize(sentence2) wordsFiltered1 = [] wordsFiltered2 = [] newSentence = "" for w in words1: if w not in stopWords: wordsFiltered1.append(w) newSentence = newSentence + " " + w for w in words2: if w not in stopWords: wordsFiltered2.append(w) marks = 0 final_word = "" for word2 in wordsFiltered2: for word1 in wordsFiltered1: if word2 == word1: marks+= 1 for word in wordsFiltered2: final_word = final_word+" "+word wordcountofdbanswer = len(final_word.split()) print(marks) print(wordcountofdbanswer) finalmark = (marks/wordcountofdbanswer)+grammarMarks finalmark = "%.2f" % finalmark # finalmark = SequenceMatcher(None, wordsFiltered1, wordsFiltered2).ratio() ConnectionToNeo4j.sessionMarksStoring(vari.userId, vari.sessionId, qno, finalmark) return_Value = [finalmark,newSentence] return return_Value
def ValidatingNonTechnical(answer,qno): answerwordcount = len(answer.split()) stopWords = set(stopwords.words('english')) words1 = word_tokenize(answer) wordsFiltered1 = [] newSentence = "" for w in words1: if w not in stopWords: wordsFiltered1.append(w) newSentence = newSentence + " " + w if answerwordcount == 0: ConnectionToNeo4j.sessionMarksStoring(vari.userId, vari.sessionId, qno, '0') return ['None', ''] if answerwordcount < 20: ConnectionToNeo4j.sessionMarksStoring(vari.userId, vari.sessionId, qno, '0.5') return [0.5, newSentence] if answerwordcount > 20: ConnectionToNeo4j.sessionMarksStoring(vari.userId, vari.sessionId, qno, '1') return [1, newSentence]
def adding_diff_level_val_list(userId, user_diff, db_diff, random_table, diff_level): level_list = ConnectionToNeo4j.getdiffLevelList(userId, user_diff, db_diff, random_table, diff_level) level_list_str = str(level_list) print("hello i got the list") print(level_list) diff_list = level_list.split(',') print("hello in the diff level selector") diff_list = list(map(int, diff_list)) print(diff_list) return (diff_list)
def validation(subsection, typer, treetype,qno): r = sr.Recognizer() audio = "../Audio/"+MostRecentAudioFileAccess.MostRecentAudioClip() cmd = 'ffmpeg -i ' + audio + ' -f segment -segment_time 13 -c copy ../Audio/answer_creation_purposes/out%03d.wav' os.system(cmd) marks = "No" longstext = "" path, dirs, files = next(os.walk("../Audio/answer_creation_purposes")) file_count = len(files) range_value = file_count print('Loading..') for x in range(0, range_value): audio = '../Audio/answer_creation_purposes/out00' + str(x) + '.wav' with sr.AudioFile(str(audio)) as source: audio = r.record(source) try: text = r.recognize_google(audio) if x == 0: longstext = text else: longstext = longstext + " " + text except Exception as e: print(e) filelist = glob.glob("../Audio/answer_creation_purposes/*.wav") for file in filelist: os.remove(file) print(longstext) if typer == "technical": valuefromdb = ConnectionToNeo4j.getValueFromdb(subsection, treetype) marks = AnswerValidating.ValidatingTechnical(longstext, valuefromdb,qno) if typer == "nontechnical": marks = AnswerValidating.ValidatingNonTechnical(longstext,qno) return marks
def keywordSelector(db,filtered_words_string,param,diff_level): db2 ="user_difficulty" db3 = "difficulty" db4 = "nested_difficulty" user_id = vari.userId print("diff_level") print(diff_level) print("diff_level") filtered_words = filtered_words_string.split(" ") print(filtered_words) print("filtered_words") global topic_list topic_list = [] global nested_keyword_list nested_keyword_list = [] global nested_difficulty_list nested_difficulty_list = [] global nes_keywords # print(filtered_words) unique_filtered_words = set() for val in filtered_words: lower_case_val = val.lower() unique_filtered_words.add(lower_case_val) print(unique_filtered_words) print("unique_filtered_words") for value in unique_filtered_words: if param == "2": print(db) print(value) topic_availability = ConnectionToNeo4j.getMatchingTopics(db,value) print(topic_availability) if topic_availability == True: topic_list.append(value) elif param == "1": topic_availability = ConnectionToNeo4j.getMatchingTopicsNonTech(value) print(topic_availability) if topic_availability == True: topic_list.append(value) print(topic_list) print("topic_list") # print(topic_list) if param == "1": topic_list = ",".join(topic_list) return topic_list if len(topic_list) > 0 and param=="2": for itt in range (len(topic_list)): random_keyword = random.choice(topic_list) nes_keywords=ConnectionToNeo4j.getMatchingNestedTopicId(db,random_keyword) print(random_keyword) print("random_keyword") nested_keyword_list = (nes_keywords.split(',', )) print(nested_keyword_list) print(nested_keyword_list) print("random_keyword") nes_diff_level = ConnectionToNeo4j.getNestedDiffLevelList(user_id,db2,db3,db4,db,diff_level) print("nes_diff_level") print(nes_diff_level) print("nes_diff_level") nested_difficulty_list = (nes_diff_level.split(',', )) print("nested_difficulty_list") print(nested_difficulty_list) print("nested_difficulty_list") selected_list = set(nested_difficulty_list) & set(nested_keyword_list) print("selected_list") print(selected_list) print("selected_list") selected_list = list(selected_list) print("selected_list") print(selected_list) print("selected_list") if not selected_list: return 0 random_nes_que = random.choice(selected_list) print("random_nes_que") print(random_nes_que) print("random_nes_que") topic = ConnectionToNeo4j.getMatchingNestedTopic(db,random_keyword,random_nes_que) print("topic") print(topic) print("topic") return topic else: return 0
def question_gen(): #difficulty level generation global changed_know_list changed_know_list = [] db2 = "user" db3 = "session" global diff_level diff_level = "easy" session_db = "session" userId = vari.userId sessionId = vari.sessionId question_number = NonTechnicalQuestions.question_number global qprinted qprinted = 0 global taking_list taking_list = [] global prev1_ans_result prev1_ans_result = 0.2 global prev2_ans_result prev2_ans_result = 0.2 global prev1_que_count prev1_que_count = 5 global prev2_que_count prev2_que_count = 6 global user_diff user_diff = "user_difficulty" global db_diff db_diff = "difficulty" q_list = [] lang = 'en' tech_keywords = NonTechnicalQuestions.technology_list print(tech_keywords) print("hey") global nested_question_ccount nested_question_ccount = 2 splitted_table_list = (tech_keywords.split(',', )) print(splitted_table_list) print("list is printed") splitted_table_list_length = len(splitted_table_list) stable_splitted_table_list_length = len(splitted_table_list) print("length") print(splitted_table_list_length) split_list_length = stable_splitted_table_list_length itteration_val = int(11 / split_list_length) itteration_value = math.floor(itteration_val) # get the nested value count after filling technologies rem_nested_count = 11 - (split_list_length * itteration_value) nested_question_ccount = nested_question_ccount + rem_nested_count print(nested_question_ccount) print("this is theeeeeeeeeeeeeeerem_nested_count") print("itt") print(itteration_value) while splitted_table_list_length >= 1: random_table = random.choice(splitted_table_list) print(random_table) splitted_table_list_length = splitted_table_list_length - 1 print("length") print(splitted_table_list_length) splitted_table_list.remove(random_table) print(splitted_table_list) technical_node_count = ConnectionToNeo4j.getTechNodeCount(random_table) print(technical_node_count) q_list = [] for id in range(1, technical_node_count + 1): q_list.append(id) print(q_list) for itt in range(itteration_value): print(itt) print("my itteration") print(itt) # difficulty level selection if prev1_ans_result >= 0.5 and prev2_ans_result >= 0.5: diff_level = DifficultyLevelSelector.increase_difficulty_level( diff_level) elif prev1_ans_result < 0.5 and prev2_ans_result < 0.5: diff_level = DifficultyLevelSelector.decrease_difficulty_level( diff_level) print(diff_level) # get the list of nodes according to the difficulty level taking_list = DifficultyLevelSelector.adding_diff_level_val_list( userId, user_diff, db_diff, random_table, diff_level) print(taking_list) print("hi i am the taking list") # comparing two lists to get the nodes that are in the q_list changed_know_list = set(q_list) & set(taking_list) print("i know it is hereeeeeeeeeeeeeeeeeeeeeeeeeeeeee") print(changed_know_list) changed_know_list = list(changed_know_list) if not changed_know_list: changed_know_list = q_list random_que = random.choice(changed_know_list) print(random_que) random_que_string = str(random_que) print(random_que_string) technical_question = ConnectionToNeo4j.technical_question_keyword( random_table, random_que_string) print("qu") print(technical_question) changed_know_list = [] q_list.remove(random_que) print(q_list) print(changed_know_list) actual_question = TechnicalQuestionCreators.gen_Question( technical_question) parser = GingerIt() TextToSpeechConverter.text_to_speech(actual_question, lang) qprinted = qprinted + 1 print("qprint") print(qprinted) print("qprint") question_number = question_number + 1 prev1_que_count = prev1_que_count + 1 prev2_que_count = prev2_que_count + 1 answer_validity = test.test() while (answer_validity == "null"): answer_validity = test.test() if itt > 1 and nested_question_ccount > 0: filtered_words_string = "hdbhfhb dsds " nested = NestedQuestionCreator.keywordSelector( random_table, filtered_words_string, "2", diff_level) if nested != 0: print("nested keyword value") print(nested) actual_question = TechnicalQuestionCreators.gen_Question( nested) TextToSpeechConverter.text_to_speech(actual_question, lang) print(actual_question) qprinted = qprinted + 1 print("qprint") print(qprinted) print("qprint") question_number = question_number + 1 prev1_que_count = prev1_que_count + 1 prev2_que_count = prev2_que_count + 1 nested_question_ccount = nested_question_ccount - 1 print(nested) answer_validity = test.test() while (answer_validity == "null"): answer_validity = test.test() else: print("when ignores") print("true") else: print("false") if itt == itteration_value - 1 and splitted_table_list_length == 1 and nested_question_ccount > 0: itteration_value = itteration_value + nested_question_ccount nested_question_ccount == 0 p1_qno = str(prev1_que_count) p1_send_question = "question" + p1_qno print(p1_qno) p2_qno = str(prev2_que_count) p2_send_question = "question" + p2_qno print(p1_qno) print("this is prev one") print(prev1_que_count) print("this is prev") print(prev2_que_count) print("nooooooooooooooooooooooooooo") if prev1_que_count == 7: prev1_ans_result = 0.2 prev2_ans_result = ConnectionToNeo4j.getQuestionMarks( db2, db3, userId, sessionId, p2_send_question) prev2_ans_result = float(prev2_ans_result) else: prev1_ans_result = ConnectionToNeo4j.getQuestionMarks( db2, db3, userId, sessionId, p1_send_question) prev2_ans_result = ConnectionToNeo4j.getQuestionMarks( db2, db3, userId, sessionId, p2_send_question) prev1_ans_result = float(prev1_ans_result) prev2_ans_result = float(prev2_ans_result) print("this is prev marksssssssssssss") print(prev1_ans_result) print(p1_send_question) print(question_number) print("this is prev marksssssssssssss") print("this is prev marksssssssssssss") print(prev2_ans_result) print(p2_send_question) print(question_number) print("this is prev marksssssssssssss") # techs = NonTechnicalQuestions.technology_list # print(techs) # lang = 'en' # q_list = [] # for id in range(1,5): # q_list.append(str(id)) # # print(q_list) # # for question_no in range(4): # time.sleep(5) # random_que = random.choice(q_list) # technical_question=ConnectionToNeo4j.ontologyQuestionGen(random_que) # q_list.remove(random_que) # actual_question = "What is "+technical_question+"?" # TextToSpeechConverter.text_to_speech(actual_question, lang) # print(actual_question) # # question_gen()
def rewardForQuestion(languageName, nodeId, difficultyLevel): # languageName = "python" # nodeId = 10 # difficultyLevel = "medium" userid = vari.userId facial = 10 #have to remove voice = 10 #have to remove answer = 20 #have to remove total = (facial + voice + answer) print("Total = ", total) total = int(total) if (0 < total <= 20): state = 1 print("State = ", state) elif (21 < total <= 40): state = 2 print("State = ", state) elif (41 < total <= 60): state = 3 print("State = ", state) elif (61 < total <= 80): state = 4 print("State = ", state) else: state = 5 print("State = ", state) # ----------------------------------------------------------------------------------- # Get the latest updated q-table from ontology - only for shown print("@@@@@This part for get from ontology@@@@@@@@@@@@@@@@@@@@@@@@@") print("It is updated correctly \n", ConnectionToNeo4j.createQtable1(languageName)) print(type(ConnectionToNeo4j.createQtable1(languageName))) print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@") # data = R print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&") # Get the String array matrix from ontology - split I = np.array(ConnectionToNeo4j.createQtable1(languageName)).tolist() Z = I.split(" ") print("Z-spit krapu 1 \n", Z) print(type(Z)) # to remove the [] number = " ".join(Z) print("this remove[] \n", number) print(type(number)) print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&") # H = [float(i) if '.' in i else int(i) for i in number] # H = np.asfarray(number,float) # print("This str - float") # print(type(H)) # Re-change it into 5,5 array # qTableCreated = H.reshape(5,5) # print(type(qTableCreated)) # print(qTableCreated) # change it into matrix # R = np.matrix(number) # print(type(R)) # print(R) R = np.matrix([[64.0, 64.0, 64.0, 64.0, 64.0], [64.0, 64.0, 64.0, 64.0, 64.0], [64.0, 64.0, 64.0, 64.0, 64.0], [64.0, 64.0, 64.0, 64.0, 64.0], [64.0, 64.0, 64.0, 64.0, 64.0]]) # try: # I = float(ConnectionToNeo4j.createQtable1(languageName)) # R = np.matrix(I) # except ValueError: # print("That is not a valid number of miles") # matrix = open(fname).read() # matrix = [item.split('\n') for item in matrix.split('\n')] # R = matrix.reshape((matrix.shape[0], 5)) # print("Get from text file-latest updated \n", R) print("----------------------------------------------") # Q matrix Q = np.matrix(np.zeros([5, 5])) if state == 1: R[0, 4] = total elif state == 2: R[1, 3] = total elif state == 3: R[2, 2] = total elif state == 4: R[3, 1] = total else: R[4, 0] = total # Gamma (learning parameter). gamma = 0.8 # Initial state if state == 5: initial_state = 4 else: initial_state = state # initial_state = state # This function returns all available actions in the state given as an argument def available_actions(state): current_state_row = R[state, ] av_act = np.where(current_state_row >= 0)[1] return av_act # Get available actions in the current state available_act = available_actions(initial_state) # This function chooses at random which action to be performed within the range def sample_next_action(available_actions_range): next_action = int(np.random.choice(available_act, 1)) return next_action # Sample next action to be performed action = sample_next_action(available_act) # This function updates the Q matrix def update(current_state, action, gamma): max_index = np.where(Q[action, ] == np.max(Q[action, ]))[1] if max_index.shape[0] > 1: max_index = int(np.random.choice(max_index, size=1)) else: max_index = int(max_index) max_value = Q[action, max_index] # Q learning formula Q[current_state, action] = R[current_state, action] + gamma * max_value # Update Q matrix update(initial_state, action, gamma) # ------------------------------------------------------------------------------- # Create the reward value # iterarte the process for i in range(100): current_state = np.random.randint(0, int(Q.shape[0])) available_act = available_actions(current_state) action = sample_next_action(available_act) update(current_state, action, gamma) print("-----------------------") print("New updated one \n", Q) print("-----------------------") # ---------------------------------------------------------------------------------------------------------------- # Save the Q-metrix in text file print(" Maximum value:") print(np.max(Q)) T = Q * 100 / np.max(Q) print("^^^^^^^^^^^^^^^^^^^^^^^^^^") print(T) np.savetxt('../Database/text.txt', T, fmt='%f') print("-----New - 6-----------------------------------------") # ------------------------------------------------------- # send to ontology qTableCreated = str(T) ConnectionToNeo4j.sendQtable(languageName, qTableCreated) # ------------------------------------------------------------------------------- print("------New - 7----------------------------------------") # convert to probability value if state == 3: convertProb = "{0:.0f}%".format((np.max(Q) / 10) - 11) # convertProb = getProb - 10.0 print("Precentage of difficulty - ", convertProb) else: convertProb = "{0:.0f}%".format(np.max(Q) / 10) print("Precentage of difficulty - ", convertProb) # --send to precentage value to ontology-------------- print(type(convertProb)) convertProb2 = int(convertProb.strip("%")) print(convertProb2) # --identify the state-------------- print("------New - 8----------------------------------------") if convertProb2 <= 15: rewardState = "hard" elif convertProb2 <= 30: rewardState = "medium" else: rewardState = "easy" print(rewardState) print(type(rewardState)) # --update the ontology--------------------------- print( "-------New - 9- update the existing list--------------------------------------" ) print( ConnectionToNeo4j.getDifficultyList(userid, languageName, difficultyLevel)) getDiffList = str( ConnectionToNeo4j.getDifficultyList(userid, languageName, difficultyLevel)) print("get existing list", getDiffList) print(type(getDiffList)) print("########exiting list 1 gattaa \n") getDiffList2 = getDiffList.split(',') getDiffList2.remove(str(nodeId)) getDiffList3 = list(map(int, getDiffList2)) print("this removed node and int it", getDiffList3) print(type(getDiffList3)) str_getDiffList3 = ','.join(str(e) for e in getDiffList3) print("This is converted str", str_getDiffList3) print(type(str_getDiffList3)) print("#######now get the new list to update exiting one \n") # update the existing category with new value ConnectionToNeo4j.sendExistingDifficultyList(userid, languageName, difficultyLevel, str_getDiffList3) print( "-------New - 10- update the new list--------------------------------------" ) # get the new category list getNewList = ConnectionToNeo4j.getNewRewardList(userid, languageName, rewardState) print(ConnectionToNeo4j.getNewRewardList(userid, languageName, rewardState)) print(type(getNewList)) print(type(nodeId)) str_nodeId = str(nodeId) print(type(str_nodeId)) getnewList = getNewList.split(',') print("1", getnewList) getnewList.append(str_nodeId) print("append new nod = ", getnewList) # getDiffList4 = [int(i) for i in appendNewNode] # print("this append and transfer int ", getDiffList4) # print(type(getDiffList4)) str_getDiffList4 = ','.join(str(e) for e in getnewList) print("This is converted str", str_getDiffList4) print(type(str_getDiffList4)) print("Now it appended \n", str_getDiffList4) # send to the new list to the new category ConnectionToNeo4j.sendNewDifficultyList(userid, languageName, rewardState, str_getDiffList4) print(type(str_getDiffList4))
def generate_cv_questions(): db = "CV" db2 = "project" db3 = "project_d" # node_Count = ConnectionToNeo4j.getNodeCount(db) lang = 'en' q_list = [] pro_list = [] count = 1 session = 0 answer_validity = 0 global question_number question_number = 0 while count <= 3: session = session + 1 print("session") print(session) session_no_string = str(session) session_node_count = ConnectionToNeo4j.session_Node_Count( db, session_no_string) print("this is ") print(session_node_count) node_id = ConnectionToNeo4j.get_node_id(db, session_no_string) for id in range(node_id, session_node_count + node_id): q_list.append(str(id)) print(q_list) print("node_count") print(session_node_count) for question_no in range(session_node_count): print("question number") print(question_no) random_que = random.choice(q_list) print("random que") print(random_que) non_technical_question = ConnectionToNeo4j.cvQuestionGen( db, random_que) q_list.remove(random_que) print(q_list) print(non_technical_question) actual_question = QuestionCreator.gen_Question( non_technical_question) parser = GingerIt() grammer_corrected_question_list = parser.parse(actual_question) grammer_corrected_question = grammer_corrected_question_list.get( "result") TextToSpeechConverter.text_to_speech(grammer_corrected_question, lang) question_number = question_number + 1 print(question_number) print("hiiiiiiiiiiiiiiiiii printing count") if random_que == "5": pro = ConnectionToNeo4j.getProjects(db, "5") print(pro) for id in range(1, pro + 1): pro_list.append(str(id)) print(pro_list) random_proj_que = random.choice(pro_list) modify_random_proj_que = "p" + random_proj_que print(modify_random_proj_que) project_question = ConnectionToNeo4j.cvQuestionProjectGen( db2, db3, modify_random_proj_que, userid) actual_project_question = QuestionCreator.gen_Question( project_question) parser = GingerIt() grammer_corrected_project_question_list = parser.parse( actual_project_question) grammer_corrected_pr0ject_question = grammer_corrected_project_question_list.get( "result") TextToSpeechConverter.text_to_speech( grammer_corrected_pr0ject_question, lang) question_number = question_number + 1 print(question_number) print("hiiiiiiiiiiiiiiiiii printing count") global technology_list tech = test.kes() tech = NestedQuestionCreator.keywordSelector("", tech, "1", "") print(tech) print("tech printed") technology_list = NestedQuestionCreator.nonTechnicalKeywordSeelector( tech, modify_random_proj_que) print("hello tech") print(technology_list) print("check validity") print("after a while") answer_validity = test.test() while (answer_validity == "None"): answer_validity = test.test() q_list = [] count = count + 1