Created
November 27, 2021 18:42
-
-
Save JosephTLyons/6f764ebc65da0633270ad35b6755f5cb to your computer and use it in GitHub Desktop.
A script that categorizes words by a "shrink" ranking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import random | |
def get_shrink_rank(word): | |
if " " in word: | |
print(word) | |
raise Exception("Word must be a single word") | |
if not word.isalpha(): | |
print(word) | |
raise Exception("Word must only contain letters") | |
return len(word) - len(set(word)) | |
with open("/Users/josephlyons/Downloads/english-words-master/words_alpha.txt", "r") as file: | |
words = [word.strip().lower() for word in file] | |
print(f"{len(words)} words ranked") | |
print() | |
shrink_rank_to_words_with_shrink_rank_dictionary = defaultdict(list) | |
for word in words: | |
shrink_rank_to_words_with_shrink_rank_dictionary[get_shrink_rank(word)].append(word) | |
sorted_shrink_ranks = sorted(shrink_rank_to_words_with_shrink_rank_dictionary.keys()) | |
for shrink_rank in sorted_shrink_ranks: | |
words_with_shrink_rank = shrink_rank_to_words_with_shrink_rank_dictionary[shrink_rank] | |
average_length_of_word_with_rank = sum(len(word) for word in words_with_shrink_rank) / len(words_with_shrink_rank) | |
average_length_of_word_with_rank_rounded = round(average_length_of_word_with_rank, 2) | |
random_word_with_shrink_rank = random.choice(words_with_shrink_rank) | |
shrink_rank_string = f"Shrink Rank: {shrink_rank}" | |
words_count_string = f"Words: {len(words_with_shrink_rank)}" | |
average_word_length_string = f"Average Word Length: {average_length_of_word_with_rank_rounded}" | |
example_string = f"Example: \"{random_word_with_shrink_rank}\"" | |
output_strings = [shrink_rank_string, words_count_string, average_word_length_string, example_string] | |
output_string = " | ".join(output_strings) | |
print(output_string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment