AI Core Code Examples for Duory Language Learning Assistant


1. Translation Module with DeepL API

Implementation of multi-language translation with contextual awareness

# Requirements: deepl==1.14.0, langdetect==1.0.9
from deepl import Translator
from langdetect import detect

class LanguageTranslator:
    def __init__(self, auth_key: str):
        self.translator = Translator(auth_key)
    
    def translate_text(self, text: str, target_lang: str) -> dict:
        """Translate text with auto-detected source language"""
        try:
            src_lang = detect(text)
            result = self.translator.translate_text(
                text, 
                target_lang=target_lang.upper(),
                context="language_learning"
            )
            return {
                "original": text,
                "translated": result.text,
                "source_lang": src_lang,
                "target_lang": target_lang,
                "confidence": result.detected_source_lang_confidence
            }
        except Exception as e:
            return {"error": str(e)}

# Usage
translator = LanguageTranslator("YOUR_DEEPL_API_KEY")
print(translator.translate_text("今日は良い天気です", "en"))

2. Romanization Engine

Japanese/Korean romanization with custom rule-based processing

# Requirements: pykakasi==2.2.1, g2pk==0.9.6
import pykakasi
from g2pk import G2p

class RomanizationEngine:
    def __init__(self):
        self.japanese_conv = pykakasi.kakasi()
        self.korean_conv = G2p()
    
    def romanize(self, text: str, language: str) -> str:
        """Convert Asian scripts to Latin alphabet"""
        if language == "ja":
            return self._japanese_romanize(text)
        elif language == "ko":
            return self.korean_conv(text, group_vowels=True)
        else:
            raise ValueError("Unsupported language")

    def _japanese_romanize(self, text: str) -> str:
        result = self.japanese_conv.convert(text)
        return " ".join([item['hepburn'] for item in result])

# Usage
engine = RomanizationEngine()
print(engine.romanize("こんにちは", "ja"))  # Output: "kon'nichiwa"
print(engine.romanize("안녕하세요", "ko"))  # Output: "annyeonghaseyo"

3. Kana Conversion Module

Kanji-to-Kana conversion for Japanese learners

# Requirements: fugashi==1.3.0, unidic-lite==1.1.0
import fugashi

class KanaConverter:
    def __init__(self):
        self.tagger = fugashi.Tagger()
    
    def to_hiragana(self, text: str) -> str:
        """Convert Japanese text to Hiragana"""
        nodes = self.tagger(text)
        return ''.join(node.feature.kana or node.surface for node in nodes)
    
    def to_katakana(self, text: str) -> str:
        """Convert Japanese text to Katakana"""
        hira = self.to_hiragana(text)
        return ''.join(chr(ord(c) + 96) if '\u3041' <= c <= '\u3096' else c for c in hira)

# Usage
converter = KanaConverter()
print(converter.to_hiragana("日本語"))  # Output: "にほんご"
print(converter.to_katakana("日本語"))  # Output: "ニホンゴ"

4. Duolingo Integration Service

Data synchronization with Duolingo API

# Requirements: duolingo-api==1.0.1
from duolingo import Duolingo

class DuolingoSync:
    def __init__(self, username: str, password: str):
        self.lingo = Duolingo(username, password)
    
    def get_recent_lessons(self, limit=10) -> list:
        """Retrieve recent learning activities"""
        return self.lingo.get_activity()[:limit]
    
    def extract_vocabulary(self, lessons: list) -> dict:
        """Parse vocabulary from lesson data"""
        vocab = {}
        for lesson in lessons:
            for word in lesson.get('words', []):
                vocab.setdefault(word['word'], []).append({
                    "skill": lesson['skill'],
                    "timestamp": lesson['datetime']
                })
        return vocab

# Usage
sync = DuolingoSync("user@email.com", "password")
lessons = sync.get_recent_lessons()
vocabulary = sync.extract_vocabulary(lessons)

5. Spaced Repetition Algorithm

Adaptive review scheduling based on SuperMemo-2

import datetime
from typing import Dict, Tuple

class SRScheduler:
    def __init__(self):
        self.efactor = 2.5  # Default ease factor
    
    def calculate_next_review(self, 
                             quality: int, 
                             last_interval: int, 
                             repetitions: int) -> Tuple[int, float]:
        """Calculate next review interval using SM-2 algorithm"""
        if quality < 3:
            repetitions = 0
            interval = 1
        else:
            if repetitions == 0:
                interval = 1
            elif repetitions == 1:
                interval = 6
            else:
                interval = round(last_interval * self.efactor)
            
            repetitions += 1
        
        # Update ease factor
        self.efactor = max(1.3, self.efactor + (0.1 - (5 - quality) * (0.08 + (5 - quality) * 0.02)))
        
        next_date = datetime.date.today() + datetime.timedelta(days=interval)
        return interval, next_date

# Usage
scheduler = SRScheduler()
quality = 4  # User recall quality (0-5)
last_interval = 10  # Days since last review
repetitions = 3     # Number of successful reviews

new_interval, next_date = scheduler.calculate_next_review(quality, last_interval, repetitions)

Technical Specifications

  1. Version Control

    • Python 3.10+
    • Dependency versions as specified in code comments
    • Docker 20.10+ for containerization
  2. Security Measures

    • API keys stored in AWS Secrets Manager
    • HTTPS with TLS 1.3 for all network requests
    • Input sanitization against injection attacks
  3. Performance Optimization

    • Redis caching for frequent translation requests
    • Connection pooling for API clients
    • Asynchronous execution using Celery workers
  4. Scalability Design

    • Kubernetes deployment with auto-scaling
    • Stateless services allowing horizontal scaling
    • MongoDB sharding for user data storage
  5. Extension Points

    • Plugin architecture for new language modules
    • Webhook integrations for third-party services
    • Custom rule engine for romanization exceptions

Note: All code examples follow PEP-8 standards and include essential error handling omitted for brevity. Actual implementation requires proper exception handling, logging, and security hardening.