Files
d365scm/analyze_question_tips.py
2026-03-22 09:52:58 +08:00

117 lines
3.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import json
from openai import OpenAI
import time
# 配置API
client = OpenAI(
api_key="sk-2b675306a92d4fb389766291ab3f1ec1",
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)
def analyze_question(question):
"""分析题目并生成记忆要点"""
prompt = f"""分析这道D365考试题目生成简洁的记忆要点帮助快速记忆。
题目:
{question['stem']}
选项:
{chr(10).join([f"{opt['label']}. {opt['text']}" for opt in question['options']])}
正确答案:{question['answer']}
请用中文生成以下内容每项不超过100字
1. 关键词提示:题目中的关键英文单词或短语
2. 题目特点:这道题的独特之处
3. 记忆技巧:如何快速记住这道题的答案
4. 答案解析:为什么这个答案是对的
请用JSON格式返回
{{
"keywords": "关键词提示",
"features": "题目特点",
"memory_tips": "记忆技巧",
"explanation": "答案解析"
}}
"""
try:
response = client.chat.completions.create(
model="qwen-plus",
messages=[
{"role": "system", "content": "你是一个D365考试专家擅长总结题目要点和记忆技巧。"},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=500
)
result = response.choices[0].message.content.strip()
# 提取JSON
if '```json' in result:
result = result.split('```json')[1].split('```')[0].strip()
elif '```' in result:
result = result.split('```')[1].split('```')[0].strip()
return json.loads(result)
except Exception as e:
print(f"Error analyzing question {question['topic']}-{question['question_num']}: {e}")
return {
"keywords": "分析失败",
"features": "分析失败",
"memory_tips": "分析失败",
"explanation": "分析失败"
}
def main():
# 加载题目
with open('exam_data/questions_translated.json', 'r', encoding='utf-8') as f:
questions = json.load(f)
print(f"总共 {len(questions)} 道题目需要分析")
# 加载已分析的题目
try:
with open('exam_data/question_tips.json', 'r', encoding='utf-8') as f:
tips_data = json.load(f)
analyzed_count = len(tips_data)
except:
tips_data = {}
analyzed_count = 0
print(f"已分析 {analyzed_count} 道题目")
# 分析题目
for i, question in enumerate(questions):
key = f"{question['topic']}-{question['question_num']}"
# 跳过已分析的题目
if key in tips_data:
print(f"[{i+1}/{len(questions)}] 跳过已分析: {key}")
continue
print(f"[{i+1}/{len(questions)}] 分析题目: {key}")
tips = analyze_question(question)
tips_data[key] = tips
# 每分析10道题保存一次
if (i + 1) % 10 == 0:
with open('exam_data/question_tips.json', 'w', encoding='utf-8') as f:
json.dump(tips_data, f, ensure_ascii=False, indent=2)
print(f"已保存 {len(tips_data)} 道题目的分析结果")
# 避免API限流
time.sleep(0.5)
# 最终保存
with open('exam_data/question_tips.json', 'w', encoding='utf-8') as f:
json.dump(tips_data, f, ensure_ascii=False, indent=2)
print(f"\n完成!共分析 {len(tips_data)} 道题目")
if __name__ == "__main__":
main()