コード例 #1
0
def add_category(request):
    '''
    添加标注类型
    :param request:
    :return:
    '''
    if request.method == "POST":
        new_ctg = request.POST.get("newctg", "")
        utils.get_database(request.session.get(utils.SESSION_DB, "")).update_categories(new_ctg)

        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:
            count[line["sug"]] += 1

        sug_category["counts"] = count

        utils.log_category_info(request.session.get(utils.SESSION_USER, ""), "添加标注类型",
                                request.session.get(utils.SESSION_DB, ""), new_ctg)

        # 更新服务
        requests.post(utils.update_sug_url(request.session.get(utils.SESSION_DB, "")),
                      data=request.session.get(utils.SESSION_DB, ""), headers=utils.headers)

        return HttpResponse(json.dumps(sug_category), content_type='application/json')
コード例 #2
0
def suggest(request):
    if not utils.check_user_session(request):
        return HttpResponseRedirect("/login/")
    # 进入标注界面,更新服务,按所选类型切换标注术语集
    requests.post(utils.update_sug_url(request.session.get(utils.SESSION_DB, "")),
                  data=request.session.get(utils.SESSION_DB, ""), headers=utils.headers)
    return render_to_response("suggest.html")
コード例 #3
0
def delete_selected_sugs(sugs, db):
    '''
    数据库删除分词标注,更新服务数据
    :param sugs:
    :param db: 术语集名称
    :return:
    '''
    utils.get_database(db).delete_suggests(sugs)
    requests.post(utils.update_sug_url(db), data=db, headers=utils.headers)
コード例 #4
0
def delete_sug_category(request):
    '''
    删除某个标注和该标注下的分词
    :param request:
    :return:
    '''
    if request.method == "POST":
        category = request.POST.get("category", "")
        utils.get_database(request.session.get(utils.SESSION_DB, "")).delete_category(category)
        utils.get_database(request.session.get(utils.SESSION_DB, "")).delete_items_by_sug(category)
        # 更新服务
        requests.post(utils.update_sug_url(request.session.get(utils.SESSION_DB, "")),
                      data=request.session.get(utils.SESSION_DB, ""), headers=utils.headers)

        utils.log_category_info(request.session.get(utils.SESSION_USER, ""), "删除标注类型",
                                request.session.get(utils.SESSION_DB, ""), category)

        return HttpResponse("", content_type='application/test')
コード例 #5
0
def add_segs_sugs(request):
    '''
    从新增数据添加分词标注
    :param request: msgs:{sug:"人工-中心词,髋关节-部位"}
    :return:
    '''
    if request.method == "POST":
        data = eval(request.POST.get("msg", ""))

        res = add_sugs(data["sug"], request.session.get(utils.SESSION_DB, ""))
        add_segs(request.session.get(utils.SESSION_DB, ""))

        logger = logging.getLogger(utils.SUGGEST_LOG)

        for sug, old_sug in res.iteritems():
            if old_sug == "":
                op = "确认标注"
            else:
                op = "更新标注"
                old_sug += "=>"
            utils.log_data_info(logger,
                                request.session.get(utils.SESSION_USER,
                                                    ""), "手动",
                                request.session.get(utils.SESSION_DB, ""), op,
                                old_sug + sug)

        d = refresh_datafile_sug({}, request.session.get(utils.SESSION_DB, ""))

        # 更新服务
        requests.post(utils.update_seg_url(
            request.session.get(utils.SESSION_DB, "")),
                      data="",
                      headers=utils.headers)
        requests.post(utils.update_sug_url(
            request.session.get(utils.SESSION_DB, "")),
                      data="",
                      headers=utils.headers)

        return HttpResponse(json.dumps(d), content_type='application/json')
コード例 #6
0
def update_segs_sugs(request):
    '''
    修改原始数据
    :param request: source:origin/new
                    msgs:dict,[seg:sug]
    :return:
    '''
    if request.method == "POST":
        msgs = eval(request.POST.get("msg", ""))  # {"高血压":"中心词"}
        logger = logging.getLogger(utils.SUGGEST_LOG)
        for seg, sug in msgs.iteritems():
            old_sug = utils.get_database(
                request.session.get(utils.SESSION_DB,
                                    "")).get_suggest_from_seg(seg)[0]["sug"]

            utils.log_data_info(logger,
                                request.session.get(utils.SESSION_USER,
                                                    ""), "手动",
                                request.session.get(utils.SESSION_DB,
                                                    ""), "更新原始数据",
                                seg + ":" + old_sug + "=>" + seg + ":" + sug)

        # 先获得更新的词原先的标注,用于写入日志,再更新数据库
        utils.get_database(request.session.get(
            utils.SESSION_DB, "")).update_single_sug_category(msgs)

        requests.post(utils.update_sug_url(
            request.session.get(utils.SESSION_DB, "")),
                      data="",
                      headers=utils.headers)
        requests.post(utils.update_seg_url(
            request.session.get(utils.SESSION_DB, "")),
                      data="",
                      headers=utils.headers)

        data = init_origin_data({}, request.session.get(utils.SESSION_DB, ""))

        return HttpResponse(json.dumps(data), content_type='application/json')
コード例 #7
0
def upload_data_file(request):
    '''
    上传分词/标注数据,txt/csv格式
    分词-标注数据库已有,跳过
    分词-标注数据库没有,写入
    分词-标注和数据库的不一样,提示是否覆盖
    ?只有分词,没有标注:未知
    错误数据(格式不对,标注类型不对),不能写入
    :param request:
    :return:
    '''
    # 上传文件信息写入log
    # upload_filename = request.FILES.get("myfile", None)
    # name = upload_filename.name
    # utils.logger_file_info(request.session.get(utils.SESSION_USER, ""), "上传分词标注数据",
    #                        request.session.get(utils.SESSION_DB, ""), name)

    checked = request.POST.get("checked", "")  # 同一分词,不同标注,是否覆盖原始数据
    if checked == '0':
        checked = False
    else:
        checked = True

    all_categories = utils.get_database(
        request.session.get(utils.SESSION_DB, "")).get_categories()

    i = 0
    # 上传标注
    for line in open("tmp.csv").readlines():
        line = line.strip()
        if len(line) > 1:
            if len(line.split("\t")) != 2:
                pass
            else:
                seg, sug = line.split("\t")
                if sug in all_categories:  # 添加的标注需属于已有标注
                    # 添加数据

                    utils.get_database(
                        request.session.get(utils.SESSION_DB,
                                            "")).insert_suggests(
                                                {
                                                    dbinfo.SUG_SEG: seg,
                                                    dbinfo.SUG_SUG: sug,
                                                    dbinfo.SEG_SOURCE: "",
                                                    dbinfo.SUG_SOURCE: "",
                                                    dbinfo.SUG_STATE: "已存",
                                                    dbinfo.SUG_COUNT: 1
                                                },
                                                cover=checked)

                    utils.log_sug_info(
                        request.session.get(utils.SESSION_USER, ""), "从数据文件",
                        request.session.get(utils.SESSION_DB, ""), "添加标注",
                        seg + ":" + sug)

                    i += 1

    requests.post(utils.update_seg_url(
        request.session.get(utils.SESSION_DB, "")),
                  data="",
                  headers=utils.headers)
    requests.post(utils.update_sug_url(
        request.session.get(utils.SESSION_DB, "")),
                  data="",
                  headers=utils.headers)

    # 更新数据
    data = init_origin_data({}, request.session.get(utils.SESSION_DB, ""))
    # 这里按sug排序
    request.session[utils.SESSION_ALLDATA] = build_sug_dict(data["items"])

    return HttpResponse(json.dumps(data), content_type='application/json')
コード例 #8
0
def delete_segs_sugs(request):
    '''
    新增数据的删除
    :param request: msg:{sug:"多根-其他,双坐骨-部位,..."} {0:{seg:,sug:},1:{seg:,sug:}}
    :return:
    '''
    if request.method == "POST":
        msgs = eval(request.POST.get("msg", ""))
        dic = {}
        # 删除新增数据
        if "sug" in msgs.keys():
            sugs = msgs["sug"]
            for s in sugs.split(","):
                if s:
                    idx = s.rfind("-")  # 最后一个"-"是分隔分词与标注,分词有可能带有"-"
                    dic['seg'], dic['sug'] = s[:idx], s[idx + 1:len(s)]

                    utils.get_database(
                        request.session.get(utils.SESSION_DB,
                                            "")).delete_suggests(dic)

                    logger = logging.getLogger(utils.SUGGEST_LOG)
                    utils.log_data_info(
                        logger, request.session.get(utils.SESSION_USER,
                                                    ""), "手动",
                        request.session.get(utils.SESSION_DB, ""), "删除新增数据",
                        dic['seg'] + ":" + dic['sug'])
        # 删除原始数据
        else:
            for k, v in msgs.iteritems():
                utils.get_database(request.session.get(utils.SESSION_DB,
                                                       "")).delete_suggests(v)

                logger = logging.getLogger(utils.SUGGEST_LOG)
                utils.log_data_info(
                    logger, request.session.get(utils.SESSION_USER, ""), "手动",
                    request.session.get(utils.SESSION_DB, ""), "删除原始数据",
                    v["seg"] + ":" + v["sug"])

        data = init_origin_data({}, request.session.get(utils.SESSION_DB, ""))
        data = refresh_datafile_sug(data,
                                    request.session.get(utils.SESSION_DB, ""))
        # 这里按sug排序
        request.session[utils.SESSION_ALLDATA] = build_sug_dict(data["items"])

        # 更新服务
        requests.post(utils.update_seg_url(
            request.session.get(utils.SESSION_DB, "")),
                      data="",
                      headers=utils.headers)
        requests.post(utils.update_sug_url(
            request.session.get(utils.SESSION_DB, "")),
                      data="",
                      headers=utils.headers)

        if request.session.get(utils.SESSION_DB, "") == "zhenduan":
            url = utils.seg_service_url_zd
        elif request.session.get(utils.SESSION_DB, "") == "shoushu":
            url = utils.seg_service_url_ss

        requests.post(url,
                      data=request.session.get(utils.SESSION_DB, ""),
                      headers=utils.headers)

        if request.session.get(utils.SESSION_DB, "") == "zhenduan":
            url = utils.sug_service_url_zd
        elif request.session.get(utils.SESSION_DB, "") == "shoushu":
            url = utils.sug_service_url_ss

        requests.post(url,
                      data=request.session.get(utils.SESSION_DB, ""),
                      headers=utils.headers)

        return HttpResponse(json.dumps(data), content_type='application/json')