File size: 2,844 Bytes
486eff6 |
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#!/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()
|