-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_soundexlist.py
More file actions
33 lines (27 loc) · 1.02 KB
/
Copy pathcreate_soundexlist.py
File metadata and controls
33 lines (27 loc) · 1.02 KB
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
def create_soundexlist(wordlist):
""" Takes one string input which is a file location for a list of words.
Note: assumes one word per line.
Builds a dictionary of soundex codes in list and saves it to local directory.
Returns the soundex dictionary as a dictionary. """
# open and read file
file = open(wordlist, 'r')
contents = file.readlines()
file.close()
# initialise dictionary
soundex_dict = {}
# for each word, assess soundex code
for line in contents:
line = line.rstrip('\n')
code = soundex(line)
# enter word value into dictionary if soundex code key exists
if code in soundex_dict:
soundex_dict[code].append(line)
# else enter soundex key and word value
else:
soundex_dict[code] = [line]
# save dictionary to file
new_file = open("sdlist.txt", "w")
new_file.write(str(soundex_dict))
new_file.close()
# return dictionary created
return soundex_dict