def code_similar(code1, code2):
    '\n    题目内容相似度用最小编辑距离来度量\n    选项相似度分为几种\n    1、完全相同：1\n    2、单选题：暂时只考虑序号和值都相等的，且共同变量超过一半:2\n    2、多选题/排序题：不考虑序号，共同变量超过一半即可：3\n    3、矩阵单选题：code_r 暂时只考虑完全匹配\n    4、其他情况为0\n\n    '
    code_distance_min = pd.DataFrame(index=code1.keys(), columns=['qnum', 'similar_content', 'similar_code'])
    for c1 in code1:
        disstance_str = pd.Series(index=code2.keys())
        for c2 in code2:
            if (code1[c1]['qtype'] == code2[c2]['qtype']):
                disstance_str[c2] = levenshtein(code1[c1]['content'], code2[c2]['content'])
        c2 = disstance_str.idxmin()
        if (('%s' % c2) == 'nan'):
            continue
        min_len = ((len(code1[c1]['content']) + len(code2[c2]['content'])) / 2)
        similar_content = ((100 - ((100 * disstance_str[c2]) / min_len)) if (min_len > 0) else 0)
        qtype = code2[c2]['qtype']
        if (qtype == '单选题'):
            t1 = code1[c1]['code']
            t2 = code2[c2]['code']
            inner_key = list((set(t1.keys()) & set(t2.keys())))
            tmp = all([(t1[c] == t2[c]) for c in inner_key])
            if (t1 == t2):
                similar_code = 1
            elif ((len(inner_key) >= (0.5 * len((set(t1.keys()) | set(t2.keys()))))) and tmp):
                similar_code = 2
            else:
                similar_code = 0
        elif (qtype in ['多选题', '排序题']):
            t1 = code1[c1]['code']
            t2 = code2[c2]['code']
            t1 = [t1[c] for c in code1[c1]['qlist']]
            t2 = [t2[c] for c in code2[c2]['qlist']]
            inner_key = (set(t1) & set(t2))
            if (t1 == t2):
                similar_code = 1
            elif (len((set(t1) & set(t2))) >= (0.5 * len((set(t1) | set(t2))))):
                similar_code = 3
            else:
                similar_code = 0
        elif (qtype in ['矩阵多选题']):
            t1 = code1[c1]['code_r']
            t2 = code2[c2]['code_r']
            t1 = [t1[c] for c in code1[c1]['qlist']]
            t2 = [t2[c] for c in code2[c2]['qlist']]
            inner_key = (set(t1) & set(t2))
            if (t1 == t2):
                similar_code = 1
            elif (len((set(t1) & set(t2))) >= (0.5 * len((set(t1) | set(t2))))):
                similar_code = 3
            else:
                similar_code = 0
        elif (qtype in ['填空题']):
            similar_code = 1
        else:
            similar_code = 0
        code_distance_min.loc[(c1, 'qnum')] = c2
        code_distance_min.loc[(c1, 'similar_content')] = similar_content
        code_distance_min.loc[(c1, 'similar_code')] = similar_code
    code_distance_min = code_distance_min.sort_values(['qnum', 'similar_content', 'similar_code'], ascending=[False, False, True])
    code_distance_min.loc[code_distance_min.duplicated(['qnum']), :] = np.nan
    code_distance_min = pd.DataFrame(code_distance_min, index=code1.keys())
    return code_distance_min
题目内容相似度用最小编辑距离来度量
选项相似度分为几种
1、完全相同：1
2、单选题：暂时只考虑序号和值都相等的，且共同变量超过一半:2
2、多选题/排序题：不考虑序号，共同变量超过一半即可：3
3、矩阵单选题：code_r 暂时只考虑完全匹配
4、其他情况为0
def code_similar(code1, code2):
    '\n    题目内容相似度用最小编辑距离来度量\n    选项相似度分为几种\n    1、完全相同：1\n    2、单选题：暂时只考虑序号和值都相等的，且共同变量超过一半:2\n    2、多选题/排序题：不考虑序号，共同变量超过一半即可：3\n    3、矩阵单选题：code_r 暂时只考虑完全匹配\n    4、其他情况为0\n\n    '
    code_distance_min = pd.DataFrame(index=code1.keys(), columns=['qnum', 'similar_content', 'similar_code'])
    for c1 in code1:
        disstance_str = pd.Series(index=code2.keys())
        for c2 in code2:
            if (code1[c1]['qtype'] == code2[c2]['qtype']):
                disstance_str[c2] = levenshtein(code1[c1]['content'], code2[c2]['content'])
        c2 = disstance_str.idxmin()
        if (('%s' % c2) == 'nan'):
            continue
        min_len = ((len(code1[c1]['content']) + len(code2[c2]['content'])) / 2)
        similar_content = ((100 - ((100 * disstance_str[c2]) / min_len)) if (min_len > 0) else 0)
        qtype = code2[c2]['qtype']
        if (qtype == '单选题'):
            t1 = code1[c1]['code']
            t2 = code2[c2]['code']
            inner_key = list((set(t1.keys()) & set(t2.keys())))
            tmp = all([(t1[c] == t2[c]) for c in inner_key])
            if (t1 == t2):
                similar_code = 1
            elif ((len(inner_key) >= (0.5 * len((set(t1.keys()) | set(t2.keys()))))) and tmp):
                similar_code = 2
            else:
                similar_code = 0
        elif (qtype in ['多选题', '排序题']):
            t1 = code1[c1]['code']
            t2 = code2[c2]['code']
            t1 = [t1[c] for c in code1[c1]['qlist']]
            t2 = [t2[c] for c in code2[c2]['qlist']]
            inner_key = (set(t1) & set(t2))
            if (t1 == t2):
                similar_code = 1
            elif (len((set(t1) & set(t2))) >= (0.5 * len((set(t1) | set(t2))))):
                similar_code = 3
            else:
                similar_code = 0
        elif (qtype in ['矩阵多选题']):
            t1 = code1[c1]['code_r']
            t2 = code2[c2]['code_r']
            t1 = [t1[c] for c in code1[c1]['qlist']]
            t2 = [t2[c] for c in code2[c2]['qlist']]
            inner_key = (set(t1) & set(t2))
            if (t1 == t2):
                similar_code = 1
            elif (len((set(t1) & set(t2))) >= (0.5 * len((set(t1) | set(t2))))):
                similar_code = 3
            else:
                similar_code = 0
        elif (qtype in ['填空题']):
            similar_code = 1
        else:
            similar_code = 0
        code_distance_min.loc[(c1, 'qnum')] = c2
        code_distance_min.loc[(c1, 'similar_content')] = similar_content
        code_distance_min.loc[(c1, 'similar_code')] = similar_code
    code_distance_min = code_distance_min.sort_values(['qnum', 'similar_content', 'similar_code'], ascending=[False, False, True])
    code_distance_min.loc[code_distance_min.duplicated(['qnum']), :] = np.nan
    code_distance_min = pd.DataFrame(code_distance_min, index=code1.keys())
    return code_distance_min