-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundex.py
67 lines (51 loc) · 1.52 KB
/
soundex.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
from fst import FST
import string, sys
from fsmutils import compose
def letters_to_numbers():
"""
Returns an FST that converts letters to numbers as specified by
the soundex algorithm
"""
# Let's define our first FST
f1 = FST('soundex-generate')
# Indicate that 'start' is the initial state
f1.add_state('start')
f1.add_state('next')
f1.initial_state = 'start'
# Set all the final states
f1.set_final('next')
return f1
def truncate_to_three_digits():
"""
Create an FST that will truncate a soundex string to three digits
"""
# Ok so now let's do the second FST, the one that will truncate
# the number of digits to 3
f2 = FST('soundex-truncate')
# Indicate initial and final states
f2.add_state('1')
f2.initial_state = '1'
f2.set_final('1')
return f2
def add_zero_padding():
# Now, the third fst - the zero-padding fst
f3 = FST('soundex-padzero')
f3.add_state('1')
f3.add_state('2')
f3.initial_state = '1'
f3.set_final('2')
return f3
def soundex_convert(name_string):
"""Combine the three FSTs above and use it to convert a name into a Soundex"""
f1 = letters_to_numbers()
f2 = truncate_to_three_digits()
f3 = add_zero_padding()
pass
if __name__ == '__main__':
user_input = input().strip()
if user_input:
print("%s -> %s" % (user_input, soundex_convert(list(user_input))))