File size: 1,996 Bytes
31069c2 |
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 |
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}%)")
|