abc123 / hack /demo_wordnet_generator.py
vimalk78's picture
feat(crossword): generated crosswords with clues
486eff6
#!/usr/bin/env python3
"""
Demo: WordNet-Based Crossword Generator
Shows the WordNet clue generation system without full thematic integration.
"""
import sys
from pathlib import Path
# Add hack directory to path
sys.path.insert(0, str(Path(__file__).parent))
def demo_wordnet_clues():
"""Demo WordNet clue generation."""
print("๐ŸŽฏ WordNet Clue Generation Demo")
print("=" * 50)
try:
from wordnet_clue_generator import WordNetClueGenerator
# Initialize generator
print("๐Ÿ”„ Initializing WordNet generator...")
generator = WordNetClueGenerator()
if generator.initialize():
print("โœ… WordNet generator ready!")
else:
print("โŒ Failed to initialize WordNet generator")
return False
# Test word-topic combinations
test_cases = [
("elephant", "animals"),
("piano", "music"),
("database", "technology"),
("mountain", "geography"),
("soccer", "sports"),
("pizza", "food")
]
print(f"\n๐Ÿงช Testing {len(test_cases)} word-topic combinations")
print("=" * 50)
for word, topic in test_cases:
print(f"\n๐Ÿ“ Word: '{word.upper()}' + Topic: '{topic}'")
print("-" * 40)
# Test different clue styles
styles = ['definition', 'synonym', 'hypernym', 'category']
for style in styles:
try:
clue = generator.generate_clue(word, topic, clue_style=style)
status = "โœ…" if clue and len(clue) > 5 else "โŒ"
clue_text = clue if clue else "(no clue generated)"
print(f" {status} {style:12}: {clue_text}")
except Exception as e:
print(f" โŒ {style:12}: Error - {e}")
return True
except Exception as e:
print(f"โŒ Demo error: {e}")
import traceback
traceback.print_exc()
return False
def demo_wordnet_info():
"""Demo WordNet information retrieval."""
print("\n๐Ÿ“š WordNet Information Demo")
print("=" * 50)
try:
from wordnet_clue_generator import WordNetClueGenerator
generator = WordNetClueGenerator()
if not generator.initialize():
print("โŒ Generator not available")
return
test_words = ["elephant", "computer", "beautiful"]
for word in test_words:
print(f"\n๐Ÿ” WordNet info for: '{word}'")
print("-" * 30)
info = generator.get_clue_info(word)
if 'error' in info:
print(f" โŒ {info['error']}")
else:
print(f" Synsets found: {info['synsets_count']}")
for i, synset in enumerate(info['synsets'][:2], 1): # Show top 2
print(f"\n {i}. {synset['name']} ({synset['pos']})")
print(f" ๐Ÿ“– Definition: {synset['definition']}")
if synset['synonyms']:
synonyms = [s for s in synset['synonyms'][:3] if s.lower() != word.lower()]
if synonyms:
print(f" ๐Ÿ”— Synonyms: {', '.join(synonyms)}")
if synset['hypernyms']:
print(f" ๐Ÿ“‚ Categories: {', '.join(synset['hypernyms'])}")
except Exception as e:
print(f"โŒ Info demo error: {e}")
def quick_interactive_demo():
"""Quick interactive demo for testing individual words."""
print("\n๐ŸŽฎ Quick Interactive Demo")
print("=" * 50)
print("Test individual words with WordNet clue generation")
print("Enter a word, or 'quit' to exit")
try:
from wordnet_clue_generator import WordNetClueGenerator
generator = WordNetClueGenerator()
if not generator.initialize():
print("โŒ Generator not available")
return
while True:
user_input = input("\n๐ŸŽฏ Enter word (or 'quit'): ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
break
if not user_input:
continue
word = user_input.lower()
print(f"\n๐Ÿ“ Generating clues for: '{word.upper()}'")
print("-" * 35)
# Test all styles
styles = ['definition', 'synonym', 'hypernym', 'category', 'descriptive']
for style in styles:
clue = generator.generate_clue(word, "", clue_style=style)
status = "โœ…" if clue and len(clue) > 3 else "โŒ"
clue_text = clue if clue else "(none)"
print(f" {status} {style:12}: {clue_text}")
except KeyboardInterrupt:
print("\n\n๐Ÿ‘‹ Exiting interactive demo")
except Exception as e:
print(f"โŒ Interactive demo error: {e}")
def main():
"""Run WordNet generator demos."""
print("๐Ÿš€ WordNet Crossword Generator Demo")
print("=" * 60)
print("Testing NLTK WordNet-based clue generation")
print("No API dependencies - runs completely offline!")
# Run demos
clue_demo_ok = demo_wordnet_clues()
if clue_demo_ok:
demo_wordnet_info()
print("\n" + "=" * 60)
choice = input("๐ŸŽฎ Run interactive demo? (y/n): ").strip().lower()
if choice in ['y', 'yes']:
quick_interactive_demo()
print("\nโœ… WordNet demo complete!")
print("\n๐Ÿ’ก This system provides:")
print(" ๐Ÿ” Offline clue generation using WordNet definitions")
print(" ๐Ÿ“š Multiple clue styles (definition, synonym, category, etc.)")
print(" ๐ŸŽฏ Topic-aware clue generation")
print(" โšก Fast generation (no API calls)")
print(" ๐Ÿ“Š Integration ready with thematic word generator")
if clue_demo_ok:
print(f"\n๐Ÿš€ Ready for integration with thematic word discovery!")
print(f" Run: python wordnet_clue_generator.py")
if __name__ == "__main__":
main()