File size: 9,233 Bytes
486eff6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
"""
Integration adapter for Unified Thematic Word Generator

This service provides a bridge between the new unified word generator
and the existing crossword backend, enabling the backend to use the
comprehensive WordFreq vocabulary instead of the limited model vocabulary.
"""

import os
import sys
import logging
from typing import List, Dict, Any, Optional
from pathlib import Path

logger = logging.getLogger(__name__)

class UnifiedWordService:
    """
    Service adapter for integrating UnifiedThematicWordGenerator with the crossword backend.
    
    Provides the same interface as VectorSearchService but uses the comprehensive
    WordFreq vocabulary instead of model-limited vocabulary.
    """
    
    def __init__(self, vocab_size_limit: Optional[int] = None):
        """Initialize the unified word service.
        
        Args:
            vocab_size_limit: Maximum vocabulary size (None for default 100K)
        """
        self.generator = None
        self.vocab_size_limit = vocab_size_limit or int(os.getenv("MAX_VOCABULARY_SIZE", "100000"))
        self.is_initialized = False
        
        # Import the generator from hack directory
        self._import_generator()
    
    def _import_generator(self):
        """Import the UnifiedThematicWordGenerator from hack directory."""
        try:
            # Add hack directory to path
            hack_dir = Path(__file__).parent.parent.parent.parent.parent / "hack"
            if hack_dir.exists():
                sys.path.insert(0, str(hack_dir))
                logger.info(f"πŸ“ Added hack directory to path: {hack_dir}")
            
            # Import the generator
            from thematic_word_generator import UnifiedThematicWordGenerator
            
            # Initialize with appropriate cache directory
            cache_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'cache', 'unified_generator')
            
            self.generator = UnifiedThematicWordGenerator(
                cache_dir=cache_dir,
                vocab_size_limit=self.vocab_size_limit
            )
            
            logger.info(f"βœ… Imported UnifiedThematicWordGenerator with vocab limit: {self.vocab_size_limit:,}")
            
        except ImportError as e:
            logger.error(f"❌ Failed to import UnifiedThematicWordGenerator: {e}")
            logger.error("   Make sure the hack directory contains thematic_word_generator.py")
            self.generator = None
        except Exception as e:
            logger.error(f"❌ Error setting up UnifiedThematicWordGenerator: {e}")
            self.generator = None
    
    async def initialize(self):
        """Initialize the unified word service."""
        if not self.generator:
            logger.error("❌ Cannot initialize: generator not available")
            return False
        
        try:
            logger.info("πŸš€ Initializing Unified Word Service...")
            start_time = time.time()
            
            # Initialize the generator (async compatible)
            await self.generator.initialize_async()
            
            self.is_initialized = True
            init_time = time.time() - start_time
            
            logger.info(f"βœ… Unified Word Service initialized in {init_time:.2f}s")
            logger.info(f"πŸ“Š Vocabulary size: {self.generator.get_vocabulary_size():,} words")
            logger.info(f"🎯 Tier distribution: {self.generator.get_tier_distribution()}")
            
            return True
            
        except Exception as e:
            logger.error(f"❌ Failed to initialize Unified Word Service: {e}")
            self.is_initialized = False
            return False
    
    async def find_similar_words(
        self, 
        topic: str, 
        difficulty: str = "medium", 
        max_words: int = 15
    ) -> List[Dict[str, Any]]:
        """
        Find similar words using the unified generator.
        
        Compatible with VectorSearchService interface.
        
        Args:
            topic: Topic to find words for
            difficulty: Difficulty level (easy/medium/hard)
            max_words: Maximum number of words to return
            
        Returns:
            List of word dictionaries: [{"word": str, "clue": str}, ...]
        """
        if not self.is_initialized or not self.generator:
            logger.error("❌ Service not initialized or generator not available")
            return []
        
        try:
            # Use the generator's backend-compatible method
            results = await self.generator.find_similar_words(topic, difficulty, max_words)
            
            logger.info(f"🎯 Generated {len(results)} words for '{topic}' (difficulty: {difficulty})")
            return results
            
        except Exception as e:
            logger.error(f"❌ Error finding similar words for '{topic}': {e}")
            return []
    
    async def _get_cached_fallback(self, topic: str, difficulty: str, max_words: int) -> List[Dict[str, Any]]:
        """
        Fallback method for compatibility with existing backend code.
        
        Since our unified generator already has comprehensive vocabulary,
        this just calls find_similar_words with relaxed criteria.
        """
        if not self.is_initialized or not self.generator:
            return []
        
        try:
            # Try with lower similarity threshold as "fallback"
            results = self.generator.generate_thematic_words(
                topic, 
                num_words=max_words,
                min_similarity=0.2  # Lower threshold for fallback
            )
            
            # Format for backend compatibility
            backend_words = []
            for word, similarity, tier in results:
                if self.generator._matches_backend_difficulty(word, difficulty):
                    backend_word = {
                        "word": word.upper(),
                        "clue": self.generator._generate_simple_clue(word, topic),
                        "similarity": similarity,
                        "tier": tier
                    }
                    backend_words.append(backend_word)
            
            logger.info(f"πŸ“¦ Fallback generated {len(backend_words)} words for '{topic}'")
            return backend_words[:max_words]
            
        except Exception as e:
            logger.error(f"❌ Error in cached fallback for '{topic}': {e}")
            return []
    
    def get_vocabulary_size(self) -> int:
        """Get the vocabulary size."""
        if self.generator:
            return self.generator.get_vocabulary_size()
        return 0
    
    def get_tier_info(self) -> Dict[str, Any]:
        """Get frequency tier information."""
        if not self.generator:
            return {}
            
        return {
            "tier_distribution": self.generator.get_tier_distribution(),
            "tier_descriptions": getattr(self.generator, 'tier_descriptions', {}),
            "vocabulary_size": self.generator.get_vocabulary_size()
        }


# Import time for initialization timing
import time

# Factory function for easy backend integration
async def create_unified_word_service(vocab_size_limit: Optional[int] = None) -> Optional[UnifiedWordService]:
    """
    Factory function to create and initialize a UnifiedWordService.
    
    Args:
        vocab_size_limit: Maximum vocabulary size (None for default)
        
    Returns:
        Initialized UnifiedWordService or None if initialization failed
    """
    try:
        service = UnifiedWordService(vocab_size_limit)
        
        if await service.initialize():
            return service
        else:
            logger.error("❌ Failed to initialize UnifiedWordService")
            return None
            
    except Exception as e:
        logger.error(f"❌ Error creating UnifiedWordService: {e}")
        return None


# Example usage for testing
async def main():
    """Test the unified word service."""
    print("πŸ§ͺ Testing Unified Word Service")
    print("=" * 50)
    
    # Create and initialize service
    service = await create_unified_word_service(vocab_size_limit=50000)  # Smaller vocab for testing
    
    if not service:
        print("❌ Failed to create service")
        return
    
    # Test word generation
    test_topics = ["animal", "science", "technology"]
    
    for topic in test_topics:
        print(f"\n🎯 Testing topic: '{topic}'")
        print("-" * 30)
        
        for difficulty in ["easy", "medium", "hard"]:
            words = await service.find_similar_words(topic, difficulty, max_words=5)
            
            print(f"  {difficulty.capitalize()}: {len(words)} words")
            for word_data in words:
                word = word_data['word']
                tier = word_data.get('tier', 'unknown')
                print(f"    {word:<12} ({tier})")
    
    print(f"\nπŸ“Š Service Info:")
    print(f"   Vocabulary size: {service.get_vocabulary_size():,}")
    print(f"   Tier info: {service.get_tier_info()}")
    
    print("\nβœ… Test completed!")


if __name__ == "__main__":
    import asyncio
    asyncio.run(main())