def show_all_revisions(): all_revisions = DBQuery().get_all_revisions() data_list = [] for revision in all_revisions: revision_id = revision.id feedback_id = revision.feedback_id feedback_order = revision.feedback_order created_user = revision.created_user article_id = revision.article_id article = DBQuery().get_article_by_id(article_id) feedback = DBQuery().get_feedback_by_id(feedback_id) article_authors = article.authors title = article.title data = { "revision_id": revision_id, "feedback_id": feedback_id, "feedback_order": feedback_order, "created_user": created_user, "article_content": feedback.content, "article_id": article_id, "article_authors": article_authors, "title": title } data_list.append(data) return render_template('show_all_revisions.html', data=data_list)
def experiment_article(feedback_id): verified_string = generate_verified_str(6) create_user = request.args.get('user', default="") order = request.args.get('order', default="") experiment_flow = request.args.get('flow', default="") sys.stderr.write("id: " + str(feedback_id)) feedback = DBQuery().get_feedback_by_id(feedback_id) article_id = feedback.article_id article = DBQuery().get_article_by_id(article_id) paragraphs = article.content.split("<BR>") list = [] for i, paragraph in enumerate(paragraphs): list.append((i, paragraph)) sorted(list) data = { "article_id": article_id, "title": article.title, "authors": article.authors, "paragraphs": list, "feedback_id": feedback_id, "order": order, "create_user": create_user, "verified_string": verified_string, "experiment_flow": experiment_flow, } return render_template('experiment_article.html', data=data)
def ensemble_feedback(feedback_id): verified_string = generate_verified_str(6) experiment_flow = request.args.get('flow', default="") create_user = request.args.get('user', default="") feedback = DBQuery().get_feedback_by_id(feedback_id) article_id = None article_content = "" feedback_content = "" if feedback: article_id = feedback.article_id content = feedback.content.strip() feedback_content = feedback.feedback_content content_list = content.split("\n") raw_content = "\n".join(content_list[1:]) article_content = "\n".join(content_list) data = { "article_id": article_id, "feedback_id": feedback_id, "create_user": create_user, "article_content": article_content, "raw_content": raw_content, "feedback_content": json.dumps(feedback_content), "verified_string": verified_string, "experiment_flow": experiment_flow, } return render_template('ensemble_feedback.html', data=data)
def add_article(): if request.method == 'POST': author = request.form['author'] title = request.form['title'] content = request.form['content'] list = content.strip().split("\n") list = [i.strip() for i in list if i] article_title = title.strip() article_content = "<BR>".join(list) article_authors = author.strip() article_source = "structfeed" article_year = datetime.utcnow().year article_id = DBQuery().add_article(article_title, article_content, article_authors, article_source, article_year) data = { "author": author, "title": title, "content": content, "article_id": article_id } return jsonify(success=1, data=data) else: return jsonify(success=0, data=[])
def init_comparison(): # initialize comparison db start = 65 end = 112 for i in range(start, end + 1): for j in range(i + 1, end + 1): pair_id = str(i) + "_" + str(j) DBQuery().add_comparison(pair_id, "", 0, 0, 0)
def show_revision(revision_id): revision = DBQuery().get_revision_by_id(int(revision_id)) article = DBQuery().get_article_by_id(revision.article_id) data = { "revision_id": revision.id, "article_id": revision.article_id, "feedback_id": revision.feedback_id, "feedback_order": revision.feedback_order, "revision_content": revision.revision_content, "created_user": revision.created_user, "article_title": article.title, "article_content": article.content, "article_authors": article.authors } return render_template('revision.html', data=data)
def comparison_task(pair_id, method, code, times): [p1, p2] = pair_id.split("_") article1 = DBQuery().get_evaluate_by_revision_id(p1) paragraphs1 = article1.revision_content.split("\n") article2 = DBQuery().get_evaluate_by_revision_id(p2) paragraphs2 = article2.revision_content.split("\n") list1 = [] for i, paragraph in enumerate(paragraphs1): list1.append((i, paragraph)) list2 = [] for i, paragraph in enumerate(paragraphs2): list2.append((i, paragraph)) sorted(list1) sorted(list2) data = { "text1": { "id": p1, "paragraphs": list1 }, "text2": { "id": p2, "paragraphs": list2 }, } if method == "diff": [diff1, diff2] = compare.compareText(article1.revision_content, article2.revision_content) return render_template('comparison_task.html', method=method, pair_id=pair_id, data=data, code=code, diff1=diff1, diff2=diff2, times=times) return render_template('comparison_task.html', method=method, pair_id=pair_id, data=data, code=code, times=times)
def experiment(feedback_id): create_user = request.args.get('user', default="") order = request.args.get('order', default="") experiment_flow = request.args.get('flow', default="") feedback = DBQuery().get_feedback_by_id(feedback_id) article_id = feedback.article_id article = DBQuery().get_article_by_id(article_id) article_title = article.title data = { "article_title": article_title, "feedback_id": feedback_id, "order": order, "experiment_flow": experiment_flow, "create_user": create_user, } return render_template('experiment.html', data=data)
def import_articles(directory, filename): '''Insert Articles to database.''' print "import articles from %s" % directory all_files = [ file for file in listdir(directory) if isfile(join(directory, file)) ] for file in all_files: if file == ".DS_Store": continue print "Read Article: %s/%s ..." % (directory, file) r = open("%s/%s" % (directory, file), "r") content = r.read().decode('utf-8') all_match = re_data.findall(content) data = dict(all_match) article_title = data['Title'].strip() article_authors = data['Authors'].strip() article_source = data['Source'].strip() article_year = data['Year'].strip() article_content = data['Introduction'].strip() article_paragraphs = article_content.split('<BR>') #check whether article is exist article = DBQuery().get_article_by_title_and_authors( article_title, article_authors) if article: print "file exist!" if not isfile(join('data-in-db', file)): shutil.move('%s/%s' % (directory, file), 'data-in-db') continue print "insert article to the database ..." list = [i.strip() for i in article_paragraphs if i] clear_content = "<BR>".join(list) article_id = DBQuery().add_article(article_title, clear_content, article_authors, article_source, int(article_year)) print "move article #%d to data-in-db folder ..." % article_id if isfile(join('data-in-db', file)): print "file has already existed in data-in-db folder..." else: shutil.move('%s/%s' % (directory, file), 'data-in-db')
def show_ensemble_all(): all_ensemble_feedback = DBQuery().get_all_feedbacks() data_list = [] for feedback in all_ensemble_feedback: feedback_id = feedback.id article_id = feedback.article_id article = DBQuery().get_article_by_id(article_id) article_authors = article.authors title = article.title data = { "feedback_id": feedback_id, "title": title, "article_id": article_id, "authors": article_authors } data_list.append(data) return render_template('show_all_feedbacks.html', data=data_list)
def import_feedback(directory, filename): '''Insert Feedback to database.''' print "import feedback from %s" % directory all_files = [file for file in listdir(directory) if file.endswith('.json')] print all_files for file in all_files: fname = file[:-5] # remove ".json" txt_file = "%s.txt" % fname article_authors, article_title = fname.split("-") print[article_title.decode('utf-8'), article_authors.decode('utf-8')] article = DBQuery().get_article_by_title_and_authors( article_title, article_authors.decode('utf-8')) print article if not article: continue article_id = article.id print "Artcile id: %d" % article_id print "Read Feedback: %s/%s ..." % (directory, file) print "Read Article: %s/%s ..." % (directory, txt_file) fp_json = open("%s/%s" % (directory, file), "r") print fp_json fp_txt = open("%s/%s" % (directory, txt_file), "r") feedback_content = json.load(fp_json) content = fp_txt.read().decode('utf-8') print feedback_content print content print "insert feedback to the database ..." feedback_id = DBQuery().add_feedback(article_id, content, feedback_content) print "move feedback to feedback-in-db folder ..." if isfile(join('feedback-in-db', file)): print "file has already existed in feedback-in-db folder..." else: shutil.move('%s/%s' % (directory, file), 'feedback-in-db') shutil.move('%s/%s' % (directory, txt_file), 'feedback-in-db')
def add_revision(): if request.method == 'POST': created_user = request.form['created_user'] article_id = int(request.form['article_id']) feedback_id = int(request.form['feedback_id']) feedback_content = json.loads(request.form['feedback_content']) revision_content = json.loads(request.form['revision_content']) duration_time = int(request.form['duration_time']) verified_string = request.form['verified_string'] feedback_order = request.form['feedback_order'] revision_id = DBQuery().add_revision(created_user, article_id, feedback_id, feedback_order, feedback_content, revision_content, duration_time) problem = "%d,%d" % (article_id, feedback_id) answer = "%d" % revision_id #add task DBQuery().add_task(created_user=created_user, task_type=TaskType.REVISION, problem=problem, answer=answer, verified_string=verified_string) data = { "created_user": created_user, "article_id": article_id, "feedback_id": feedback_id, "feedback_content": feedback_content, "duration_time": duration_time, "verified_string": verified_string, "feedback_order": feedback_order } return jsonify(success=1, data=data) else: return jsonify(success=0, data=[])
def show_article(article_id): article = DBQuery().get_article_by_id(article_id) paragraphs = article.content.split("<BR>") list = [] for i, paragraph in enumerate(paragraphs): list.append((i, paragraph)) sorted(list) data = { "id": article.id, "title": article.title, "authors": article.authors, "paragraphs": list, } return render_template('article.html', data=data)
def add_comparison(): if request.method == 'POST': pair_id = request.form['pair_id'] # articleId1_articleId2 created_user = request.form['created_user'] # verified code choosed_article = request.form['choosed_article'] # 1 or 2 comparison_id = DBQuery().update_comparison(pair_id, created_user, choosed_article) data = { "pair_id": pair_id, "created_user": created_user, "choosed_article": choosed_article, "comparison_id": comparison_id } return jsonify(success=1, data=data) else: return jsonify(success=0, data=[])
def show_all(): all_articles = DBQuery().get_article_count() article_authors = "anonymous" data_list = [] for article in all_articles: article_id = article.id title = article.title.encode("utf-8") if article.authors: article_authors = article.authors data = { "title": title, "article_id": article_id, "authors": article_authors } data_list.append(data) data_list.sort(key=lambda tup: tup["article_id"]) return render_template('show_all.html', data=data_list)
def pre_experiment(): # experiment flow pattern: # <feedback_id1>-<order1>|<feedback_id2>-<order2>|<feedback_id3>-<order3> experiment_flow = request.args.get('flow', default="") create_user = request.args.get('user', default=None) feedback_ids = [] orders = [] done = [] flow = experiment_flow.split("|") for element in flow: [feedback_id, order] = element.split("-") feedback_ids.append(feedback_id) orders.append(order) revision = DBQuery().get_revision_by_feedback_id_and_user( feedback_id, create_user) if revision is None: done.append(0) else: done.append(1) data = { "experiment_flow": experiment_flow, "create_user": create_user, "feedback_id1": feedback_ids[0], "order1": orders[0], "order1_done": done[0], "feedback_id2": feedback_ids[1], "order2": orders[1], "order2_done": done[1], "feedback_id3": feedback_ids[2], "order3": orders[2], "order3_done": done[2], } return render_template('pre_experiment.html', data=data)
def init_evaluate(): # initialize evaluate db start = 111 end = 112 for i in range(start, end + 1): DBQuery().add_evaluate_by_revision_id(i)
def show_all_articles(page=1): paginated_articles = DBQuery().get_article_paginate(page, per_page) return render_template('show_all_articles.html', paginated_articles=paginated_articles)