|
|
import os
|
|
|
import json
|
|
|
import re
|
|
|
from typing import Iterator
|
|
|
from openai import OpenAI
|
|
|
|
|
|
|
|
|
class EnglishEssayAnalyzer:
|
|
|
def __init__(self):
|
|
|
self.client = OpenAI(
|
|
|
api_key=os.getenv("DEEPSEEK_API_KEY", "sk-01d13a39e09844038322108ecdbd1bbc"),
|
|
|
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
|
|
|
)
|
|
|
|
|
|
def _build_prompt(self, essay: str) -> str:
|
|
|
return f"""你是一位专业的英语教师,请分析以下英文作文:
|
|
|
{essay}
|
|
|
|
|
|
请按以下顺序指出问题:
|
|
|
1. 语法错误(标注行号)
|
|
|
2. 用词不当
|
|
|
3. 逻辑结构问题
|
|
|
4. 改进建议
|
|
|
|
|
|
用中文回答,保持专业但易懂的语气。"""
|
|
|
|
|
|
def analyze_stream(self, essay: str) -> Iterator[str]:
|
|
|
"""流式分析作文(新增关键方法)"""
|
|
|
try:
|
|
|
stream = self.client.chat.completions.create(
|
|
|
model="deepseek-r1",
|
|
|
messages=[{
|
|
|
"role": "user",
|
|
|
"content": self._build_prompt(essay)
|
|
|
}],
|
|
|
temperature=0.3,
|
|
|
stream=True
|
|
|
)
|
|
|
|
|
|
for chunk in stream:
|
|
|
if chunk.choices and chunk.choices[0].delta.content:
|
|
|
yield chunk.choices[0].delta.content
|
|
|
|
|
|
except Exception as e:
|
|
|
yield f"\n分析中断:{str(e)}"
|
|
|
|
|
|
def full_analysis(self, essay: str) -> dict:
|
|
|
"""完整分析并返回结构化结果"""
|
|
|
analysis = {
|
|
|
"grammar_errors": [],
|
|
|
"vocabulary_issues": [],
|
|
|
"structure_problems": [],
|
|
|
"suggestions": []
|
|
|
}
|
|
|
|
|
|
current_category = None
|
|
|
buffer = ""
|
|
|
category_pattern = re.compile(r'(\d+\.\s.*?)(:)') # 改进正则表达式
|
|
|
|
|
|
for chunk in self.analyze_stream(essay):
|
|
|
print(chunk, end='', flush=True)
|
|
|
buffer += chunk
|
|
|
|
|
|
# 改进的分类检测逻辑
|
|
|
match = category_pattern.search(buffer)
|
|
|
if match:
|
|
|
current_category = match.group(1).strip()
|
|
|
buffer = buffer[match.end():] # 移除已匹配的部分
|
|
|
|
|
|
# 根据分类初始化条目
|
|
|
if '语法' in current_category:
|
|
|
analysis['grammar_errors'].append('')
|
|
|
elif '用词' in current_category:
|
|
|
analysis['vocabulary_issues'].append('')
|
|
|
elif '逻辑' in current_category:
|
|
|
analysis['structure_problems'].append('')
|
|
|
elif '改进' in current_category:
|
|
|
analysis['suggestions'].append('')
|
|
|
|
|
|
# 内容填充逻辑(改进版)
|
|
|
if current_category:
|
|
|
target_list = None
|
|
|
if '语法' in current_category:
|
|
|
target_list = analysis['grammar_errors']
|
|
|
elif '用词' in current_category:
|
|
|
target_list = analysis['vocabulary_issues']
|
|
|
elif '逻辑' in current_category:
|
|
|
target_list = analysis['structure_problems']
|
|
|
elif '改进' in current_category:
|
|
|
target_list = analysis['suggestions']
|
|
|
|
|
|
if target_list and target_list:
|
|
|
target_list[-1] += chunk
|
|
|
|
|
|
# 后处理(增强版)
|
|
|
for key in analysis:
|
|
|
# 分割条目并清理空白
|
|
|
cleaned = []
|
|
|
for text in analysis[key]:
|
|
|
items = [t.strip() for t in re.split(r'\n(?=\d+\.)', text) if t.strip()]
|
|
|
cleaned.extend(items)
|
|
|
analysis[key] = cleaned
|
|
|
|
|
|
return analysis
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
# 示例用法
|
|
|
essay = """
|
|
|
Dear Peter, Knowing that you have won the first prize in The Chinese Chess NetworkChallengeCompetition, I feel very delighted. I'm writing to offer my warmest congratulations to you. From my perspective, you derseve what you gained What lead to your success is your diligence and go all out for the contest, making me admire you. On top of this, It's real encouragement to me to see your effors pay off. How I marvel at your perfect performance, which inspires my passion on Chinese Chess and other traditional skills, I would appreciate if you could share with me your experience on learn it. Looking forward to your early reply Your sincerely
|
|
|
"""
|
|
|
|
|
|
analyzer = EnglishEssayAnalyzer()
|
|
|
print("🔍 开始分析作文...\n")
|
|
|
result = analyzer.full_analysis(essay)
|
|
|
|
|
|
with open("analysis_report.json", "w", encoding="utf-8") as f:
|
|
|
json.dump(result, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
print("\n\n✅ 分析结果已保存至 analysis_report.json") |