Open supperthomas opened 1 year ago
import openai
import re
# 设置OpenAI的API密钥
openai.api_key = 'YOUR_API_KEY_HERE'
# 定义函数来调用chatgpt的API
def summarize_function_content(function_name, function_code):
# 调用chatgpt API以获取函数内容概括
response = openai.Completion.create(
engine="davinci-codex",
prompt=f"Summarize the function content of the C code snippet:\n\n{function_code}\n\nFunction Name: {function_name}\n\nSummary:",
max_tokens=100
)
# 解析API返回的概括
summary = response.choices[0].text.strip()
return summary
# 定义函数来对C语言代码进行Doxygen注释和函数内容概括
def generate_doxygen_comments(c_code):
# 使用正则表达式提取函数名和代码
functions = re.findall(r'(?:\w+\s)+(\w+)\s*\([^{}]*\)\s*{[^}]*}', c_code)
for function in functions:
# 调用summarize_function_content函数获取函数内容概括
function_code = re.search(f'{function}\\s*\\([^{}]*\\)\\s*{{([^}}]*)}}', c_code, re.MULTILINE | re.DOTALL).group(1).strip()
summary = summarize_function_content(function, function_code)
# 生成Doxygen注释
doxygen_comment = f"/**\n * @brief {summary}\n */"
# 在函数之前添加Doxygen注释
c_code = re.sub(f'{function}\\s*\\([^{}]*\\)\\s*{{', f'{doxygen_comment}\n\\g<0>', c_code)
return c_code
# 示例C语言代码
c_code = """
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
return 0;
}
"""
# 生成Doxygen注释和函数内容概括
c_code_with_comments = generate_doxygen_comments(c_code)
print(c_code_with_comments)
http://www.network-science.de/ascii/
http://www.network-science.de/ascii/