Xinfan Chen
2026-03-27 ba9e6828a4623e25cabaa1964a96aa26e56c049f
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import json
import os
import re
import Levenshtein
from LLMCaller import *
 
def read_json(input_path):
    with open(input_path, 'r', encoding='utf-8',errors='ignore') as f:
        r = toString(json.load(f))
        r = r.replace("�",".")
        return json.loads(r)
 
 
def write_json(output_path, output_data):
    with open(output_path, 'w', encoding='utf-8') as f:
        json.dump(output_data, f, ensure_ascii=False)
 
 
def read_prompt(input_path):
    with open(input_path, 'r', encoding='utf-8') as f:
        return f.read()
    
    
def log_prompt(prompt_log_path, input):
    if not isinstance(input, str):
        input = toString(input)
    with open(prompt_log_path, "a", encoding="utf-8") as log_file:
        log_file.write(f"{input}\n")
        log_file.write("#######################################################\n")
 
 
def cretae_new_path(path, filetype):
    if not os.path.exists(path):
        os.makedirs(path)
    files = os.listdir(path)
    if len(files) == 0:
        return os.path.join(path, f'0.{filetype}')
    else:
        max = 0
        for file in files:
            max = max if max>=int(os.path.splitext(file)[0]) else int(os.path.splitext(file)[0])
        return os.path.join(path, str(max+1) + f'.{filetype}')
    
 
def find_latest_file(path):
    files = os.listdir(path)
    max = 0
    f = ""
    for file in files:
        if max < int(os.path.splitext(file)[0]):
            max = int(os.path.splitext(file)[0])
            f = file
 
    return os.path.join(path, f)
 
 
def toString(input):
    return json.dumps(input, ensure_ascii=False, separators=(",", ":"))          
       
        
def prompt_format(prompt, params):
    text = prompt
    for key, value in params.items():
        if isinstance(value, (dict, list)):
            value = toString(value)
        if isinstance(value, (int, float)):
            value = str(value)
        text = text.replace(key, value)
    return text
    
    
def GPTResponse2JSON(response):
    json_string = response
    prompt = f"Modify the following string so that it can be correctly parsed by the json.loads() method:\n{json_string}\n\nYou should just return the modified string."
    if "```json" in json_string:
        json_string = json_string.replace("```","")
        json_string = json_string.replace("json","")
        json_string = json_string.strip()
    try:
        result = json.loads(json_string)
    except:
        result = json.loads(clean_text(GPTCall(prompt)))
 
    return result
 
 
def clean_text(text):
    # Only keep json content
    pattern = r"```json(.*?)```"
    match = re.search(pattern, text, re.DOTALL)
    if match:
        text =  match.group(1)
    
    # Remove some unexpected characters    
    punctuation_map = {
        ",": ",",
        "。": ".",
        "!": "!",
        "?": "?",
        ":": ":",
        ";": ";",
        "“": "\"",
        "”": "\"",
        "‘": "\'",
        "’": "\'",
        "(": "(",
        ")": ")",
        "【": "[",
        "】": "]",
        "——": "-",
        "…": "...",
        "–": "-",
        "—": "-",
        "�": "."
    }
    pattern = r'[^a-zA-Z0-9\s\.,!?;:\'"\-({})\[\]]'
    for chinese, english in punctuation_map.items():
        text = text.replace(chinese, english)
    text = re.sub(pattern, '', text)
    text = text.strip()
    
    return text
 
 
def get_number(text):
    pattern = r'[^0-9]'
    text = re.sub(pattern, '', text)
    return int(text)
 
 
def contains_digit(string):
    return bool(re.search(r'\d', string))
 
 
def translate_digit(string):
    prompt = f"Convert the numbers in the following sentence into correct English expressions:\n\n{string}\n\nYour answer should only contain the following JSON content:\n" + '{"Converted-sentence": "..."}'
    result = GPTResponse2JSON(GPTCall(prompt))
    return list(result.values())[0]
 
 
def calculate_similarity(str1, str2):
    distance = Levenshtein.distance(str1.lower(), str2.lower())
    max_len = max(len(str1), len(str2))
    similarity = 1 - distance / max_len
    return similarity
 
 
def return_most_similar(string, string_list):
    max_similarity = 0
    tgt = 0
    for id,item in enumerate(string_list):
        current_similarity = calculate_similarity(string, item)
        if current_similarity > max_similarity:
            max_similarity = current_similarity
            tgt = id
    
    return string_list[tgt]
 
 
def GetValueFromDictArray(dictarray, key1, key2, value1):
    for item in dictarray:
        if item[key1] == value1:
            return item[key2]