You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
4.3 KiB

5 months ago
import os
import json
5 months ago
import re
5 months ago
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]:
5 months ago
"""流式分析作文(新增关键方法)"""
5 months ago
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:
5 months ago
yield chunk.choices[0].delta.content
5 months ago
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
5 months ago
buffer = ""
for chunk in self.analyze_stream(essay): # 现在可以正确调用
5 months ago
print(chunk, end='', flush=True)
5 months ago
buffer += chunk
# 分类检测逻辑
if re.match(r'^\d+\.\s', buffer):
if '' in buffer:
parts = buffer.split('', 1)
current_category = parts[0].strip()
buffer = parts[1]
# 初始化当前分类
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:
if '语法' in current_category:
analysis['grammar_errors'][-1] += chunk
elif '用词' in current_category:
analysis['vocabulary_issues'][-1] += chunk
elif '逻辑' in current_category:
analysis['structure_problems'][-1] += chunk
elif '改进' in current_category:
analysis['suggestions'][-1] += chunk
# 后处理
for key in analysis:
analysis[key] = [text.strip() for text in analysis[key] if text.strip()]
5 months ago
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")