def getAnswersFromDB(count): answers = Answers.query.filter(Answers.idQuestion == count + 1) resAnswers = [] for a in answers: resAnswers.append( Answer(idAnswer=a.idAnswer, idQuestion=count + 1, textAnswer=a.textAnswer, textAnswerIta=a.textAnswerIta)) return resAnswers
def allAnswers(self): labelled_answers = [] for qa in ALL_QA: if qa.question_id == self.question_id: answer = Answer.getAnswerById(qa.answer_id) correctNess = qa.correctNess labelled_answers.append({ "answer": answer, "correctNess": correctNess }) return labelled_answers
def get_comments(question_id): q = Question.objects.get(id=question_id) if q.type == '填空题': contents = [each.content for each in Answer.objects(question=q)] if not contents: raise KeyError else: comments = nlp.comments(contents) return json.dumps(comments) else: raise KeyError
def get_keywords(question_id): q = Question.objects.get(id=question_id) if q.type == '填空题': content = '' for each in Answer.objects(question=q): content += (each.content + ' ') if not content: raise KeyError else: keywords = nlp.extract_keywords(content, top_k=50) return json.dumps(keywords) else: raise KeyError
def analyse(question_id): q = Question.objects.get(id=question_id) answers = Answer.objects(question=q) total = len(answers) if not total: raise KeyError if q.type == '单项选择题' or q.type == '多项选择题': tmp = {} for each in answers: tmp.setdefault(each.content, 0) tmp[each.content] += 1.0 / total return json.dumps(tmp) else: pass
def get_sentiment(question_id): q = Question.objects.get(id=question_id) if q.type == '填空题': contents = [each.content for each in Answer.objects(question=q)] if not len(contents): raise KeyError else: results = nlp.sentiment(contents) final = 0.0 for item in results: final += item[0] final /= len(contents) return json.dumps(final) else: raise KeyError
Description: - One question may have any number of: - incorrect answers - correct answers - One answer may be reused to be included in: - any number of questions - So there is a many to many relation """ # Questions question_howManyDaysInYear = Question.insertQuestion( "How many days are in a year?") question_howManyDaysInWeek = Question.insertQuestion( "How many days are in a week?") # Answers answer_300 = Answer.insertAnswer("300") answer_400 = Answer.insertAnswer("400") answer_365 = Answer.insertAnswer("365") answer_7 = Answer.insertAnswer("7") answer_100 = Answer.insertAnswer("100") answer_not8 = Answer.insertAnswer("Not 8") answer_seven = Answer.insertAnswer("Seven") # QA for question_howManyDaysInYear QA.insertQA(question_howManyDaysInYear, answer_300, False) QA.insertQA(question_howManyDaysInYear, answer_400, False) QA.insertQA(question_howManyDaysInYear, answer_365, True) QA.insertQA(question_howManyDaysInYear, answer_7, False) # QA for question_howManyDaysInWeek QA.insertQA(question_howManyDaysInWeek, answer_7, True)