def play_quizzes(): try: try: body = request.get_json() previous_questions_id = body['previous_questions'] quiz_category = body['quiz_category'] quiz_category_id = quiz_category['id'] except: raise BadRequest questions = util.get_questions(category_id=quiz_category_id, formatted=False) unanswered_questions = [question for question in questions if question.id not in previous_questions_id] if not unanswered_questions: rand_question = None else: rand_question = random.choice(unanswered_questions).format() return jsonify({ "success": True, "question": rand_question }) except BadRequest: abort(400) except Exception: abort(500)
def get_paginated_questions(): try: categories = util.get_categories(formatted=True) current_category = request.args.get('cateogry') questions = util.get_questions(formatted=False) page = request.args.get('page', 1, type=int) start = QUESTIONS_PER_PAGE * (page - 1) end = start + QUESTIONS_PER_PAGE questions_on_selected_page = [question.format() for question in questions[start: end]] if not questions_on_selected_page: raise ResourceNotFound return jsonify({ "success": True, "questions": questions_on_selected_page, "total_questions": len(questions), "categories": categories, "current_category": current_category }) except ResourceNotFound: abort(404) except Exception: abort(500)
def index(): questions = util.get_questions() question_types = util.get_question_types(questions) answers = util.get_answers() answers_for_each_type = util.num_of_answers_for_each_type(questions, answers) return render_template('index.html', question_types=question_types, questions=questions, answers=answers, answers_for_each_type=answers_for_each_type)
def question_and_answers(question_id): questions = util.get_questions() question = util.get_data_by_id(questions, question_id) answers = util.get_answers() answers_for_question = util.get_answers_for_question(question_id, answers) return render_template('question_and_answers.html', question=question, answers_for_question=answers_for_question, question_id=question_id)
def add_question_post(): question_keys = util.get_question_keys() new_question = dict.fromkeys(question_keys, None) questions = util.get_questions() question_data = dict(request.form) question_data['id'] = util.get_new_id(questions) question_data['submission_time'] = util.get_current_datetime_string() question_data['view_number'] = 0 question_data['vote_number'] = 0 new_question.update(question_data) util.add_to_data(questions, new_question) return redirect(url_for('question_and_answers', question_id=new_question['id']))
def get_questions_by_category(category_id): try: questions = util.get_questions(category_id=category_id, formatted=True) if not questions: raise ResourceNotFound return jsonify({ "success": True, "questions": questions, "total_questions": len(questions), "current_category": category_id }) except ResourceNotFound: abort(404) except Exception: abort(500)
def answer_rerank(data_path, ent_path, rel_path, output_dir, index_reach, index_degrees, mid2wiki, is_heuristics, ent_hits, rel_hits): id2questions, id2goldmids = get_questions(data_path) id2mids = get_mids(ent_path, ent_hits) id2rels = get_rels(rel_path, rel_hits) file_base_name = os.path.basename(data_path) fout = open(os.path.join(output_dir, file_base_name), 'w') id2answers = defaultdict(list) found, notfound_both, notfound_mid, notfound_rel = 0, 0, 0, 0 retrieved, retrieved_top1, retrieved_top2, retrieved_top3 = 0, 0, 0, 0 lineids_found1 = [] lineids_found2 = [] lineids_found3 = [] # for every lineid for line_id in id2goldmids: if line_id not in id2mids and line_id not in id2rels: notfound_both += 1 continue elif line_id not in id2mids: notfound_mid += 1 continue elif line_id not in id2rels: notfound_rel += 1 continue found += 1 question, truth_rel = id2questions[line_id] truth_rel = www2fb(truth_rel) truth_mid = id2goldmids[line_id] mids = id2mids[line_id] rels = id2rels[line_id] if is_heuristics: for (mid, mid_name, mid_type, mid_score) in mids: for (rel, rel_label, rel_log_score) in rels: # if this (mid, rel) exists in FB if rel in index_reach[mid]: rel_score = math.exp(float(rel_log_score)) comb_score = (float(mid_score)**0.6) * (rel_score**0.1) id2answers[line_id].append( (mid, rel, mid_name, mid_type, mid_score, rel_score, comb_score, int(mid2wiki[mid]), int(index_degrees[mid][0]))) # I cannot use retrieved here because I use contain different name_type # if mid ==truth_mid and rel == truth_rel: # retrieved += 1 id2answers[line_id].sort(key=lambda t: (t[6], t[3], t[7], t[8]), reverse=True) else: id2answers[line_id] = [(mids[0][0], rels[0][0])] # write to file fout.write("{}".format(line_id)) if is_heuristics: for answer in id2answers[line_id]: mid, rel, mid_name, mid_type, mid_score, rel_score, comb_score, _, _ = answer fout.write(" %%%% {}\t{}\t{}\t{}\t{}".format( mid, rel, mid_name, mid_score, rel_score, comb_score)) else: for answer in id2answers[line_id]: mid, rel = answer fout.write(" %%%% {}\t{}".format(mid, rel)) fout.write('\n') if is_heuristics: if len(id2answers[line_id]) >= 1 and id2answers[line_id][0][ 1] == truth_rel: # id2answers[line_id][0][0] == truth_mid and retrieved_top1 += 1 retrieved_top2 += 1 retrieved_top3 += 1 lineids_found1.append(line_id) elif len(id2answers[line_id]) >= 2 and id2answers[line_id][1][0] == truth_mid \ and id2answers[line_id][1][1] == truth_rel: retrieved_top2 += 1 retrieved_top3 += 1 lineids_found2.append(line_id) elif len(id2answers[line_id]) >= 3 and id2answers[line_id][2][0] == truth_mid \ and id2answers[line_id][2][1] == truth_rel: retrieved_top3 += 1 lineids_found3.append(line_id) else: if len(id2answers[line_id]) >= 1 and id2answers[line_id][0][0] == truth_mid \ and id2answers[line_id][0][1] == truth_rel: retrieved_top1 += 1 retrieved_top2 += 1 retrieved_top3 += 1 lineids_found1.append(line_id) print() print("found: {}".format(found / len(id2goldmids) * 100.0)) print("retrieved at top 1: {}".format(retrieved_top1 / len(id2goldmids) * 100.0)) print("retrieved at top 2: {}".format(retrieved_top2 / len(id2goldmids) * 100.0)) print("retrieved at top 3: {}".format(retrieved_top3 / len(id2goldmids) * 100.0)) # print("retrieved at inf: {}".format(retrieved / len(id2goldmids) * 100.0)) fout.close() return id2answers
def delete_question(question_id): questions = util.get_questions() util.remove_from_data(questions, question_id) return redirect(url_for('index'))
def list_of_questions(category): questions = util.get_questions() questions_of_category = util.get_questions_of_category(questions, category) return render_template('list_of_questions.html', questions_of_category=questions_of_category, category=category)