import os import pandas as pd from fuzzywuzzy import process from openai import OpenAI from dotenv import load_dotenv # Load environment variables (OpenAI key) load_dotenv() openai_api_key = os.getenv("OPENAI_API_KEY") # Initialize OpenAI client client = OpenAI(api_key=openai_api_key) # Load word list word_df = pd.read_csv('spelling_words.csv', header=None) word_list = word_df[0].tolist() # Fuzzy search function def search_word(user_input, word_list, top_n=5): matches = process.extract(user_input, word_list, limit=top_n) return matches # Function for AI-powered responses (definitions, origins, etc.) def get_ai_response(query): response = client.chat.completions.create( model="gpt-4-turbo", messages=[ {"role": "system", "content": "You are an expert spelling bee coach. Provide clear definitions, word origins, usage examples, and helpful memory tips for kids."}, {"role": "user", "content": query} ], temperature=0.3, max_tokens=200 ) return response.choices[0].message.content.strip() # Main interaction loop if __name__ == "__main__": print("šŸŽ‰ Welcome to Spelling Bee Word Finder with AI! šŸŽ‰") while True: query = input("\nEnter a word or ask a question ('define ubiquitous', 'origin onomatopoeia') or type 'exit' to quit: ").strip() if query.lower() == "exit": print("Goodbye! Keep practicing 😊") break # Check if the query is asking for a definition or origin if query.lower().startswith(("define", "meaning", "origin", "use", "sentence", "explain")): print("\n🧠 AI Response:") print(get_ai_response(query)) else: # Default fuzzy search for spelling matches results = search_word(query, word_list) print("\nšŸ“Œ Top Matches (Fuzzy Search):") for match, score in results: print(f"{match} (Confidence: {score}%)")