src.ch03 package¶
Submodules¶
src.ch03.c1_anagram_generator module¶
Generate phrase anagrams from a word or phrase.
-
src.ch03.c1_anagram_generator.
anagram_generator
(word: str) → list[source]¶ Generate phrase anagrams.
Make phrase anagrams from a given word or phrase.
-
src.ch03.c1_anagram_generator.
extend_anagram_dict
(word_list: list, dictionary: dict)[source]¶ Extend an anagram dictionary.
Adds words from given word list to a given anagram dictionary.
-
src.ch03.c1_anagram_generator.
find_anagram_phrases
(phrases: list, word: str, anagram_dict: dict, phrase: list) → None[source]¶ Find anagram phrases.
Recursively finds anagram phrases of word by removing unusable words from the anagram_dict, finding remaining anagrams given the phrase, then adding any found anagram phrases to phrases.
-
src.ch03.c1_anagram_generator.
find_anagrams
(word: str, anagram_dict: dict) → list[source]¶ Find anagrams in word.
Find all anagrams in a given word (or phrase) using anagram dictionary.
- Parameters
word (str) – Word to find anagrams of.
anagram_dict – Dictionary from
get_anagram_dict()
.
- Returns
-
src.ch03.c1_anagram_generator.
get_anagram_dict
(word_list: list) → dict[source]¶ Get an anagram dictionary from word_list.
Get the ID of each word in word list and add it to a dictionary with the ID as the key.
- Parameters
word_list (list) – List of words to make into anagram dictionary.
- Returns
defaultdict
oflist
with an ID (int
) as the key and words whose product of letters equal that ID as values.
-
src.ch03.c1_anagram_generator.
get_id
(word: str) → int[source]¶ Get ID number of word.
Assign a unique prime number to each letter in
ascii_lowercase
. The product of each letter in word is its ID number.
-
src.ch03.c1_anagram_generator.
get_primes
(length: int = 26, min_prime: int = 2, max_prime: int = 101) → list[source]¶ Get list of primes.
Given a length, minimum, and maximum prime number, return a list of prime numbers.
- Parameters
- Returns
list
of length prime numbers with min_prime as the smallest prime number and max_prime as the largest prime number in the list.
-
src.ch03.c1_anagram_generator.
multi_get_anagram_dict
(word_list: list) → dict[source]¶ Multithreaded get anagram dictionary.
Uses
os.cpu_count()
andthreading.Thread
to use all CPUs to make an anagram dictionary with the intent of being more efficient thanget_anagram_dict()
.- Parameters
word_list (list) – List of words to make into anagram dictionary.
- Returns
defaultdict
oflist
with an ID (int
) as the key and words whose product of letters equal that ID as values.
Warning
Avoids race conditions by heavily relying on CPython’s Global Interpreter Lock. More info about Thread Objects.
-
src.ch03.c1_anagram_generator.
remove_unusable_words
(anagram_dict: dict, usable_letters: list) → dict[source]¶ Remove unusable words from anagram dictionary.
Creates new anagram dictionary by including only IDs that can be IN usable_letters.
- Parameters
- Returns
defaultdict
oflist
with an ID (int
) as the key and words whose product of letters equal that ID as values.
-
src.ch03.c1_anagram_generator.
split
(a_list: list, parts: int) → list[source]¶ Split a list into parts.
Split given list into given number of parts.
- Parameters
- Returns
List of lists with a_list split into parts.
Example
>>> import src.ch03.c1_anagram_generator.split as split >>> some_list = ['this', 'is', 'a', 'list'] >>> split_list = split(some_list, 2) >>> print(split_list) [['this', 'is'], ['a', 'list']]
src.ch03.p1_digram_counter module¶
Counts the occurrence of all possible digrams of a word in a dictionary.
-
src.ch03.p1_digram_counter.
count_digrams
(digrams: set, dict_list: list) → dict[source]¶ Count digrams in word dictionary.
Count frequency of each digram in the set in a word dictionary list.
-
src.ch03.p1_digram_counter.
digram_counter
(word: str, dict_file: str = '/usr/share/dict/american-english') → dict[source]¶ Wrap get_digrams, count_digrams, and read_from_file.
Send word through
get_digrams()
to get a set of digrams which is then passed throughcount_digrams()
along with the list made by passing dict_file throughread_from_file()
.- Parameters
word (str) – Word to get digrams of.
dict_file (str) – Path of dictionary file to get a frequency analysis of each digram. Defaults to
DICTIONARY_FILE_PATH
.
- Returns
Counter
with digrams as keys and their counts as values.
Module contents¶
Chapter 3.
-
src.ch03.
GET_DIGRAMS_ERROR
¶ String with
TypeError
forget_digrams()
.- Type
-
src.ch03.
COUNT_DIGRAMS_ERROR
¶ String with
TypeError
forcount_digrams()
.- Type