forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_patterns.py
39 lines (30 loc) · 961 Bytes
/
word_patterns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from __future__ import print_function
import pprint, time
def getWordPattern(word):
word = word.upper()
nextNum = 0
letterNums = {}
wordPattern = []
for letter in word:
if letter not in letterNums:
letterNums[letter] = str(nextNum)
nextNum += 1
wordPattern.append(letterNums[letter])
return '.'.join(wordPattern)
def main():
startTime = time.time()
allPatterns = {}
with open('Dictionary.txt') as fo:
wordList = fo.read().split('\n')
for word in wordList:
pattern = getWordPattern(word)
if pattern not in allPatterns:
allPatterns[pattern] = [word]
else:
allPatterns[pattern].append(word)
with open('Word Patterns.txt', 'w') as fo:
fo.write(pprint.pformat(allPatterns))
totalTime = round(time.time() - startTime, 2)
print(('Done! [', totalTime, 'seconds ]'))
if __name__ == '__main__':
main()