Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: ipp4_0_single_anagrams.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script demonstrates "Chapter 3: Project #4 - Single Anagrams" from the book "Impractical Python Projects" by Lee Vaughan.
- - This script allows users to find single word anagrams of a given word by searching a dictionary file.
- - It downloads a dictionary file from a specified URL, loads it into memory, and then searches for anagrams of the user-provided word within the dictionary.
- Requirements:
- - Python 3.x
- - The following modules:
- - sys
- - requests
- Functions:
- - download_dictionary(url, file_name):
- - Description:
- - Downloads a dictionary file from a URL.
- - Parameters:
- - url (str): The URL from which to download the dictionary file.
- - file_name (str): The name to save the downloaded file as.
- - Raises:
- - requests.RequestException: If an error occurs during the HTTP request.
- - load_dictionary(file):
- - Description:
- - Opens a text file and turns its contents into a list of lowercase strings.
- - Parameters:
- - file (str): The name of the file to open.
- - Returns:
- - list: A list of lowercase strings containing the words from the file.
- - find_anagrams(word, word_list):
- - Description:
- - Finds anagrams of a given word in a list of words.
- - Parameters:
- - word (str): The word to find anagrams for.
- - word_list (list): A list of words to search for anagrams.
- - Returns:
- - list: A list of anagrams found in the word list.
- Usage:
- - Ensure you have Python 3.x installed on your system.
- - Save the script to a file, for example, `anagrams.py`.
- - Run the script using the command: python anagrams.py.
- Example Output:
- Enter a word to find its anagrams: sample
- Anagrams of 'sample':
- maples
- Additional Notes:
- - The dictionary file is downloaded from the specified URL. Ensure an active internet connection for successful download.
- - Anagrams are case-insensitive.
- """
- import sys
- import requests
- def download_dictionary(url, file_name):
- """
- Download a dictionary file from a URL.
- Parameters:
- url (str): The URL from which to download the dictionary file.
- file_name (str): The name to save the downloaded file as.
- Raises:
- requests.RequestException: If an error occurs during the HTTP request.
- """
- try:
- response = requests.get(url)
- with open(file_name, 'wb') as f:
- f.write(response.content)
- except requests.RequestException as e:
- print("Error downloading dictionary from {}: {}".format(url, e))
- sys.exit(1)
- def load_dictionary(file):
- """
- Open a text file & turn contents into a list of lowercase strings.
- Parameters:
- file (str): The name of the file to open.
- Returns:
- list: A list of lowercase strings containing the words from the file.
- """
- try:
- with open(file, encoding='utf-8') as in_file:
- loaded_txt = in_file.read().strip().split('\n')
- loaded_txt = [x.lower() for x in loaded_txt]
- return loaded_txt
- except IOError as e:
- print("{}\nError opening {}. Terminating program.".format(e, file))
- sys.exit(1)
- def find_anagrams(word, word_list):
- """
- Find anagrams of a given word in a list of words.
- Parameters:
- word (str): The word to find anagrams for.
- word_list (list): A list of words to search for anagrams.
- Returns:
- list: A list of anagrams found in the word list.
- """
- word_sorted = sorted(word.lower())
- anagrams = [w for w in word_list if sorted(w) == word_sorted and w.lower() != word.lower()]
- return anagrams
- def main():
- dictionary_url = "https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt"
- dictionary_file = "dictionary.txt"
- download_dictionary(dictionary_url, dictionary_file)
- user_input = input("Enter a word to find its anagrams: ").strip()
- word_list = load_dictionary(dictionary_file)
- anagrams = find_anagrams(user_input, word_list)
- if anagrams:
- print("Anagrams of '{}':".format(user_input))
- for anagram in anagrams:
- print(anagram)
- else:
- print("No anagrams found in dictionary for '{}'.".format(user_input))
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement