abc123 / hack /read_dict.py
vimalk78's picture
feat(crossword): generated crosswords with clues
486eff6
raw
history blame
2.84 kB
#!/usr/bin/env python3
"""
Simple Python program to read and process dict-words/dict.csv
"""
import csv
import os
def read_dict_csv(file_path):
"""Read the dictionary CSV file and return word->definition dictionary."""
word_dict = {}
try:
with open(file_path, 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
word_dict[row['word'].lower()] = row['definition']
except FileNotFoundError:
print(f"Error: File {file_path} not found")
return None
except Exception as e:
print(f"Error reading file: {e}")
return None
return word_dict
def search_word(word_dict, word):
"""Search for a word in the dictionary."""
word_lower = word.lower()
if word_lower in word_dict:
return word_dict[word_lower]
return None
def main():
# Path to the dictionary CSV file
dict_path = os.path.join(os.path.dirname(__file__), 'dict-words', 'dict.csv')
print(f"Reading dictionary from: {dict_path}")
# Read the dictionary data
word_dict = read_dict_csv(dict_path)
if word_dict:
print(f"Successfully loaded {len(word_dict)} words from dictionary")
# Display first 5 words as sample
print("\nFirst 5 words:")
for i, (word, definition) in enumerate(list(word_dict.items())[:5]):
print(f"{i+1}. {word}: {definition[:100]}...")
for word in ['suit']:
print(f"word: {word}, definition: \"{word_dict[word]}\"")
# Basic statistics
print(f"\nStatistics:")
print(f"Total words: {len(word_dict)}")
avg_def_length = sum(len(definition) for definition in word_dict.values()) / len(word_dict)
print(f"Average definition length: {avg_def_length:.1f} characters")
# Find longest and shortest words
longest_word = max(word_dict.keys(), key=len)
shortest_word = min(word_dict.keys(), key=len)
print(f"Longest word: {longest_word} ({len(longest_word)} chars)")
print(f"Shortest word: {shortest_word} ({len(shortest_word)} chars)")
# Interactive search
print("\n" + "="*50)
print("Interactive Dictionary Search (type 'quit' to exit)")
print("="*50)
while True:
search_term = input("\nEnter word to search: ").strip()
if search_term.lower() == 'quit':
break
definition = search_word(word_dict, search_term)
if definition:
print(f"\n{search_term.upper()}: {definition}")
else:
print(f"\n'{search_term}' not found in dictionary")
else:
print("Failed to load dictionary data")
if __name__ == "__main__":
main()