|
|
import os |
|
|
import pandas as pd |
|
|
from fuzzywuzzy import process |
|
|
from openai import OpenAI |
|
|
from dotenv import load_dotenv |
|
|
|
|
|
|
|
|
load_dotenv() |
|
|
openai_api_key = os.getenv("OPENAI_API_KEY") |
|
|
|
|
|
|
|
|
client = OpenAI(api_key=openai_api_key) |
|
|
|
|
|
|
|
|
word_df = pd.read_csv('spelling_words.csv', header=None) |
|
|
word_list = word_df[0].tolist() |
|
|
|
|
|
|
|
|
def search_word(user_input, word_list, top_n=5): |
|
|
matches = process.extract(user_input, word_list, limit=top_n) |
|
|
return matches |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
if query.lower().startswith(("define", "meaning", "origin", "use", "sentence", "explain")): |
|
|
print("\nπ§ AI Response:") |
|
|
print(get_ai_response(query)) |
|
|
else: |
|
|
|
|
|
results = search_word(query, word_list) |
|
|
print("\nπ Top Matches (Fuzzy Search):") |
|
|
for match, score in results: |
|
|
print(f"{match} (Confidence: {score}%)") |
|
|
|