Created
April 21, 2025 16:14
-
-
Save casperlehmann/9248b73c52483a7e05bcf072c9e4149f to your computer and use it in GitHub Desktop.
Playing with word filters
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
| #!/usr/bin/env python | |
| # Playing with word filters | |
| # pip install english-words | |
| from english_words import get_english_words_set | |
| web2 = get_english_words_set(['web2'], lower=True) | |
| gcide = get_english_words_set(['gcide'], lower=True) | |
| all_words = web2.union(gcide) | |
| # Filters | |
| has_len_x = lambda word, x: len(word) == x | |
| letter_in_a_to_f = lambda letter: letter in 'abcdef' | |
| all_in_a_to_f = lambda word: False not in [letter_in_a_to_f(_) for _ in word] | |
| all_except_last_in_a_to_f = lambda word: False not in [letter_in_a_to_f(_) for _ in word[:-1]] | |
| ends_in_l = lambda word: word.endswith('l') | |
| all_in_a_to_f_plus_l = lambda word: False not in [letter_in_a_to_f(_) for _ in word] | |
| has_len_8 = lambda word: has_len_x(word, 8) | |
| is_palindrome = lambda word: word == word[::-1] | |
| class WordList: | |
| def __init__(self, label, word_list): | |
| self.label = label | |
| self.word_list = word_list | |
| def show(self): | |
| print(f'\n# {self.label}') | |
| for i, word in enumerate(sorted(self.word_list)): | |
| print(word) | |
| if i % 100 == 99: | |
| if input(f'Listed {i} items. Continue? (y/n)> ') == 'n': | |
| break | |
| def apply(word_list, label, *filters): | |
| for filter in filters: | |
| word_list = {word for word in word_list if filter(word)} | |
| if label: | |
| print(f'{label:<50} {len(word_list):>6}') | |
| return WordList(label, word_list) | |
| apply_all = lambda label, *filters: apply(all_words, label, *filters) | |
| print (f'# Count all words: {len(all_words)}') | |
| print ('\n# Applying filters...') | |
| len_8 = apply_all('len(8)', has_len_8) | |
| all_a_f = apply_all("All letters in A-F", all_in_a_to_f) | |
| len_8_all_a_f = apply_all("len(8) + All letters in A-F", has_len_8, all_in_a_to_f) | |
| _ = apply_all("Word with A to F", all_in_a_to_f) | |
| a_f_ends_in_l = apply_all('Ending in L + preceded by letters in A-F', ends_in_l, all_except_last_in_a_to_f) | |
| palindromes = apply_all('Palindromes', is_palindrome) | |
| # all_a_f.show() | |
| len_8_all_a_f.show() | |
| a_f_ends_in_l.show() | |
| # palindromes.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment