def send_segment(request): ''' get segment :param request: :return: ''' if request.method == "POST": utils.update_db(request) dbname = request.POST.get("db", "") try: info = dict() info['seg'] = dict() info['origin'] = dict() info['source'] = dict() # info['checked'] = [] # 已经查看过的index filename = request.session.get(utils.SESSION_FILE, "") if filename == "": msg = request.POST.get("msg", "") msg_list = seg_from_disk(msg) else: msg_list = seg_from_file(filename) # 分词每次分MAX_TERM个,避免时间过长 data = msg_list[:utils.SEGS_PER_PAGE] if len( msg_list) > utils.SEGS_PER_PAGE else msg_list seg_list = update_segments(data, dbname) terms = [] # 文本的分词 for i, origin in enumerate(msg_list): info['origin'][i + 1] = origin #所有数据,用于计算总共多少页 i = 1 for seg_item in seg_list: # info['origin'][i] = seg_item[0] # 获取第一页的分词,显示来源 if i <= utils.SEGS_PER_PAGE: info['seg'][i] = dict() tmp = [] for j, seg in enumerate(seg_item[1]): seg = seg.decode('utf8') info['seg'][i][j + 1] = [s for s in seg] tmp.append(seg) terms.append(tmp) i += 1 info['source'] = get_seg_source( request.session.get(utils.SESSION_DB, ""), terms) info["page_count"] = utils.SEGS_PER_PAGE except Exception, e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] output = ",".join([str(e), fname, str(exc_tb.tb_lineno)]) error_logger.info( request.session.get("username", "") + " - 分词服务错误 - " + output) return HttpResponse(json.dumps(info), content_type='application/json')
def update_category(request): utils.update_db(request) dic = utils.update_nav_style({}, requests.session.get(utils.SESSION_DB, "")) dic["username"] = request.session.get(utils.SESSION_USER, "") return HttpResponse(json.dumps(dic), content_type='application/json')
def send_suggest(request): ''' 进入标注界面,获得分词,调用标注服务 :param request: :return: ''' if request.method == "POST": try: utils.update_db(request) new_sugs = request.POST.get("new_segs", "")[:-1] # 去掉末尾的; origin_msg = request.POST.get("origin", "") # 原文 if origin_msg: origin_msg = origin_msg.split(",") filename = request.session.get(utils.SESSION_FILE, "") info = dict() # 手动输入 if filename == "": # 诊断的分词对应标注和来源 info['sug'], info['source'], info['msg'] = get_sug_from_disk(new_sugs, request.session.get(utils.SESSION_DB, "")) # 从文件读取 else: edit_index = request.POST.get("edit_index", "") # 已编辑的index # seg_index_list = edit_index.split(",")[:-1] # 保存过的诊断index,从0开始 # edit_count = get_sug_from_file(filename, seg_index_list) # 返回编辑的分词个数 edit_count = 0 utils.get_database(request.session.get(utils.SESSION_DB, "")).update_file_checked_seg(filename, edit_count) # 诊断的分词对应标注 info['sug'], info['source'], info['msg'] = sort_sugs_by_category(origin_msg, "从文件 " + request.session.get( utils.SESSION_ORIGIN_FILE, ""), new_sugs, request.session.get(utils.SESSION_USER, ""), request.session.get(utils.SESSION_DB, "")) # 所有的标注对应颜色 info['all'] = {} all_sugs = utils.get_suggests_dic(request.session.get(utils.SESSION_DB, "")) for k, v in all_sugs.iteritems(): info['all'][k] = v info["page_count"] = utils.SUGS_PER_PAGE except Exception, e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] output = ",".join([str(e), fname, str(exc_tb.tb_lineno)]) error_logger.info(request.session.get("username", "") + " - 标注服务错误 - " + output) return HttpResponse(json.dumps(info), content_type='application/json')
def get_data(request): ''' 获得新增的标注数据 :param request: :return: ''' if request.method == "POST": utils.update_db(request) d = {} d = refresh_datafile_sug(d, request.session.get(utils.SESSION_DB, "")) return HttpResponse(json.dumps(d), content_type='application/json')
def update_suggests(request): ''' 在标注界面,切换术语集,更新标注和颜色 :param request: :return: ''' if request.method == "POST": utils.update_db(request) dic = get_all_suggest_color({}, request.session.get(utils.SESSION_DB, "")) dic = utils.update_nav_style(dic, request.session.get(utils.SESSION_DB, "")) return HttpResponse(json.dumps(dic), content_type='application/json')
def get_origin_data(request): ''' 获得所有数据库中已存的数据 :param request: :return: ''' if request.method == "POST": utils.update_db(request) data = init_origin_data({}, request.session.get(utils.SESSION_DB, "")) # 这里按sug排序 request.session[utils.SESSION_ALLDATA] = build_sug_dict(data["items"]) config = json.load(open("config.json")) data["page_count"] = config['basic']['ITEM_PER_PAGE_ORIGIN_DATA'] return HttpResponse(json.dumps(data), content_type='application/json')
def get_files(request): ''' 获得所有上传文件 :param request: :return: ''' if request.method == "POST": utils.update_db(request) files = utils.get_database(request.session.get(utils.SESSION_DB, "")).get_files() dic = get_file(files) config = json.load(open("config.json")) dic["page_count"] = config['basic']['ITEM_PER_PAGE_FILE'] return HttpResponse(json.dumps(dic), content_type='application/json')
def get_session(request): ''' 登录后,更新用户名session,获得用户权限 :param request: :return: ''' if request.method == "POST": utils.update_db(request) dic = {} dic['username'] = request.session.get(utils.SESSION_USER, "") dic['auth_log'] = log_authority( request.session.get(utils.SESSION_USER, "")) dic['auth_new_data'] = new_data_authority( request.session.get(utils.SESSION_USER, "")) dic['auth_origin_data'] = origin_data_authority( request.session.get(utils.SESSION_USER, "")) dic = utils.update_nav_style(dic, request.session.get(utils.SESSION_DB, "")) return HttpResponse(json.dumps(dic), content_type='application/json')
def get_log_info(request): ''' 获得所有日志数据 :param request: :return: ''' if request.method == "POST": utils.update_db(request) log_dic = {} log_dic["denglu"] = get_login_loginfo() log_dic["shuju"] = get_data_loginfo() log_dic["biaozhu"] = get_sug_loginfo() log_dic["wenjian"] = get_files_loginfo() # log_dic["error"] = get_error_loginfo() config = json.load(open("config.json")) log_dic["page_count"] = config['basic']['ITEM_PER_PAGE_LOG'] return HttpResponse(json.dumps(log_dic), content_type='application/json')
def get_all_suggests(request): ''' 所有标注传到标注编辑界面 :param request: :return: ''' utils.update_db(request) sug_category = {} sug_category["category"] = utils.get_database(request.session.get(utils.SESSION_DB, "")).get_categories() count = {} for s in sug_category["category"]: count[s] = 0 sug_data = utils.get_database(request.session.get(utils.SESSION_DB, "")).get_suggests() for line in sug_data: try: count[line["sug"]] += 1 except Exception, e: print line["sug"]