File size: 1,545 Bytes
e16247a
 
 
 
 
 
 
d9d4257
e16247a
d9d4257
e16247a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import pandas as pd
import os 
import difflib

def map_(input_text):
    #输入文本
    # input_text = "TypeError: unsupported operand type(s) for ^: 'int' and 'float'"
    demo_code_folder = './'

    errorType = pd.read_excel('./error_type.xlsx')
    type = []
    for i in range(len(errorType)):
        information = {
            'num': int(errorType['number'][i]),
            'type': errorType['type'][i],
            'context': errorType['context'][i]
        }
        type.append(information)

    # 计算输入文本与预设错误类型文本信息的相似度
    best_match = None
    best_similarity = 0
    for info in type:
        similarity = difflib.SequenceMatcher(None, input_text, info['type']).ratio()
        if similarity > best_similarity:
            best_similarity = similarity
            best_match = info

    # 输出相似度最高的文本信息及示例代码 
    if best_match:
        # print()
        code_file_name = str(best_match['num']) + ".py"
        code_file_path = os.path.join(demo_code_folder, code_file_name)
        # 检查示例代码文件是否存在
        if os.path.exists(code_file_path):
            # 读取示例代码内容
            with open(code_file_path, "r") as file:
                code = file.read()
            return f"存在的错误:\n\n{best_match['context']}\n\n" + '-' * 50 + f"\n\n示例代码:\n```ss\n{code}\nss```"
        else:
            return "未找到对应的示例代码文件"
    else:
        return "未找到相似的错误类型"