2019-10-29 20:05:36 +00:00
|
|
|
def get_word_pattern(word: str) -> str:
|
|
|
|
"""
|
2023-10-22 05:42:26 +00:00
|
|
|
Returns numerical pattern of character appearances in given word
|
|
|
|
>>> get_word_pattern("")
|
|
|
|
''
|
|
|
|
>>> get_word_pattern(" ")
|
|
|
|
'0'
|
2019-10-29 20:05:36 +00:00
|
|
|
>>> get_word_pattern("pattern")
|
|
|
|
'0.1.2.2.3.4.5'
|
|
|
|
>>> get_word_pattern("word pattern")
|
|
|
|
'0.1.2.3.4.5.6.7.7.8.2.9'
|
|
|
|
>>> get_word_pattern("get word pattern")
|
|
|
|
'0.1.2.3.4.5.6.7.3.8.9.2.2.1.6.10'
|
2023-10-22 05:42:26 +00:00
|
|
|
>>> get_word_pattern()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: get_word_pattern() missing 1 required positional argument: 'word'
|
|
|
|
>>> get_word_pattern(1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AttributeError: 'int' object has no attribute 'upper'
|
|
|
|
>>> get_word_pattern(1.1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AttributeError: 'float' object has no attribute 'upper'
|
|
|
|
>>> get_word_pattern([])
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AttributeError: 'list' object has no attribute 'upper'
|
2019-10-29 20:05:36 +00:00
|
|
|
"""
|
2016-08-11 13:43:33 +00:00
|
|
|
word = word.upper()
|
2019-10-29 20:05:36 +00:00
|
|
|
next_num = 0
|
|
|
|
letter_nums = {}
|
|
|
|
word_pattern = []
|
2016-08-11 13:43:33 +00:00
|
|
|
|
|
|
|
for letter in word:
|
2019-10-29 20:05:36 +00:00
|
|
|
if letter not in letter_nums:
|
|
|
|
letter_nums[letter] = str(next_num)
|
|
|
|
next_num += 1
|
|
|
|
word_pattern.append(letter_nums[letter])
|
|
|
|
return ".".join(word_pattern)
|
2019-10-05 05:14:13 +00:00
|
|
|
|
2016-08-11 13:43:33 +00:00
|
|
|
|
2019-10-29 20:05:36 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import pprint
|
|
|
|
import time
|
2016-08-11 13:43:33 +00:00
|
|
|
|
2019-10-29 20:05:36 +00:00
|
|
|
start_time = time.time()
|
|
|
|
with open("dictionary.txt") as in_file:
|
2022-10-12 22:54:20 +00:00
|
|
|
word_list = in_file.read().splitlines()
|
2016-08-11 13:43:33 +00:00
|
|
|
|
2021-08-19 12:08:20 +00:00
|
|
|
all_patterns: dict = {}
|
2022-10-12 22:54:20 +00:00
|
|
|
for word in word_list:
|
2019-10-29 20:05:36 +00:00
|
|
|
pattern = get_word_pattern(word)
|
|
|
|
if pattern in all_patterns:
|
|
|
|
all_patterns[pattern].append(word)
|
2016-08-11 13:43:33 +00:00
|
|
|
else:
|
2019-10-29 20:05:36 +00:00
|
|
|
all_patterns[pattern] = [word]
|
2016-08-11 13:43:33 +00:00
|
|
|
|
2019-10-29 20:05:36 +00:00
|
|
|
with open("word_patterns.txt", "w") as out_file:
|
|
|
|
out_file.write(pprint.pformat(all_patterns))
|
2016-10-08 17:30:05 +00:00
|
|
|
|
2022-10-12 22:54:20 +00:00
|
|
|
total_time = round(time.time() - start_time, 2)
|
|
|
|
print(f"Done! {len(all_patterns):,} word patterns found in {total_time} seconds.")
|
2019-10-29 20:05:36 +00:00
|
|
|
# Done! 9,581 word patterns found in 0.58 seconds.
|