File size: 7,981 Bytes
1df77cc | 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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | """
Teaching Mode - Interactive learning with mistake detection
"""
from typing import Dict, List, Optional
from .memory import MemorySystem, EnglishMistakeDetector, ConversationStyle
class TeachingMode:
"""Interactive teaching with mistake detection and friendly conversation"""
def __init__(self, user_id: str = "default", style: str = "friend"):
self.user_id = user_id
self.memory = MemorySystem(user_id)
self.mistake_detector = EnglishMistakeDetector()
self.conversation_style = ConversationStyle(style)
self.current_topic = None
self.lesson_progress = {}
def start_lesson(self, topic: str) -> Dict:
"""Start a new lesson"""
self.current_topic = topic
self.lesson_progress = {
'topic': topic,
'started': True,
'mistakes': 0,
'corrections': 0,
'examples_practiced': 0
}
greeting = self.conversation_style.get_response('greeting')
topic_intros = {
'english': f"{greeting}\n\nLet's practice English together! I'll help you with grammar, vocabulary, and conversation. Just write anything in English and I'll help you improve! ๐",
'grammar': f"{greeting}\n\nLet's work on English grammar! I'll teach you tenses, articles, prepositions, and more. Try writing some sentences! โ๏ธ",
'vocabulary': f"{greeting}\n\nLet's build your vocabulary! I'll teach you new words and how to use them. Try using new words in sentences! ๐",
'conversation': f"{greeting}\n\nLet's practice conversation! Just talk to me like a friend, and I'll help you sound more natural. ๐ฌ",
'hindi': f"{greeting}\n\nLet's practice Hindi! Main aapki madad karunga Hindi seekhne mein. Hindi mein kuch likhiye! ๐ฎ๐ณ",
'math': f"{greeting}\n\nLet's solve math problems together! Show me what you're working on, and I'll help you step by step. ๐ข"
}
intro = topic_intros.get(topic.lower(), f"{greeting}\n\nLet's learn {topic} together! I'm here to help you. What would you like to start with?")
return {
'response': intro,
'topic': topic,
'status': 'started'
}
def process_input(self, user_input: str) -> Dict:
"""Process user input and provide feedback"""
response = {
'original_input': user_input,
'mistakes_found': [],
'corrections': [],
'feedback': '',
'encouragement': '',
'next_step': ''
}
# Detect mistakes if it's English practice
if self.current_topic and self.current_topic.lower() in ['english', 'grammar', 'conversation', 'vocabulary']:
mistakes = self.mistake_detector.detect_mistakes(user_input)
if mistakes:
response['mistakes_found'] = mistakes
# Create corrections
for mistake in mistakes:
correction_msg = self.conversation_style.format_correction(
mistake['original'],
mistake['correction'],
self.mistake_detector.get_explanation(mistake)
)
response['corrections'].append(correction_msg)
# Record mistake in memory
self.memory.add_mistake(
mistake['original'],
mistake['correction'],
mistake['type'],
self.current_topic
)
response['feedback'] = f"I found {len(mistakes)} mistake(s). Let me help you fix them! ๐ช"
else:
response['feedback'] = self.conversation_style.get_response('encouragement')
response['next_step'] = "Your sentence is perfect! Try another one or ask me anything."
# Store conversation in memory
full_response = response['feedback']
if response['corrections']:
full_response += "\n\n" + "\n".join(response['corrections'])
self.memory.add_conversation(user_input, full_response, self.current_topic or '')
return response
def get_learning_summary(self) -> Dict:
"""Get user's learning summary"""
stats = self.memory.get_user_stats()
weak_areas = self.memory.get_weak_areas()
learned = self.memory.get_learned_topics()
common_mistakes = self.memory.get_common_mistakes(top_n=3)
summary = f"""
๐ **Your Learning Progress**
โ
Topics Learned: {len(learned)}
๐ฏ Weak Areas: {len(weak_areas)}
โ๏ธ Corrections Made: {stats['corrections_made']}
๐ฌ Conversations: {stats['total_conversations']}
๐ Progress: {stats['learning_progress']:.1f}%
"""
if weak_areas:
summary += f"๐ Focus Areas: {', '.join(weak_areas[:3])}\n"
if common_mistakes:
summary += "\n๐ Common Mistakes to Work On:\n"
for mistake in common_mistakes:
summary += f" โข '{mistake['mistake']}' (appeared {mistake['count']} times)\n"
if learned:
summary += f"\n๐ Great Job! You've learned: {', '.join(learned[:5])}"
return {
'summary': summary,
'stats': stats,
'weak_areas': weak_areas,
'learned_topics': learned
}
def set_style(self, style: str):
"""Change conversation style"""
self.conversation_style = ConversationStyle(style)
self.memory.set_preference('conversation_style', style)
return f"Sure! I'll talk to you as your {style} now! ๐"
def check_progress(self, topic: str = None) -> Dict:
"""Check user's progress in a topic"""
topic = topic or self.current_topic
if not topic:
return {'error': 'No topic selected. Start a lesson first!'}
improvement = self.memory.check_improvement(topic)
mistakes = self.memory.get_mistake_history(topic)
return {
'topic': topic,
'total_mistakes': len(mistakes),
'improvement': improvement,
'message': improvement['message']
}
def get_personalized_lesson(self) -> Dict:
"""Get a personalized lesson based on weak areas"""
weak_areas = self.memory.get_weak_areas()
if not weak_areas:
return {
'response': "You're doing great! No specific weak areas found. Let's practice something new! What topic interests you?",
'suggestion': 'new_topic'
}
# Pick the most common weak area
focus_area = weak_areas[0]
lesson_suggestions = {
'tense': "I noticed you sometimes struggle with tenses. Let's practice past tense! Can you tell me what you did yesterday?",
'article': "Let's work on articles (a, an, the). Try writing 3 sentences about your day, and I'll help with articles!",
'preposition': "Prepositions can be tricky! Let's practice common ones: in, on, at. Tell me where things are in your room.",
'subject-verb agreement': "Let's practice matching subjects with verbs. Try: 'He/She/It ___' and fill in the blank!",
'indian_english': "Let's work on standard English expressions. I'll give you common phrases and we'll practice the natural way to say them!"
}
suggestion = lesson_suggestions.get(focus_area, f"Let's work on {focus_area}. Ready to practice?")
return {
'response': suggestion,
'focus_area': focus_area,
'suggestion': 'personalized_practice'
}
|