forked from mrda/junkcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphonetic.py
More file actions
executable file
·209 lines (204 loc) · 5.17 KB
/
phonetic.py
File metadata and controls
executable file
·209 lines (204 loc) · 5.17 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
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
#
# phonetic.py - Spell out the input string to the NATO phoentic alphabet,
# or do the inverse
#
# Copyright (C) 2007 Michael Davies (michael@the-davies.net)
#
# e.g. Encoding example:
# mrda@carbon:~/src/python$ ./phonetic.py vc107
# victor charlie one zero seven
#
# e.g. Decoding example
# mrda@carbon:~/src/python$ ./phonetic.py dec "victor charlie one zero seven"
# vc107
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
import re
import sys
import string
def encode(strings):
# nested string comprehension, I love python :-)
return " space ".join([" ".join([_enc(string.lower(ch)) for ch in list(str)]) for str in strings])
def _enc(ch):
if (ch) == 'a':
return 'alpha'
elif (ch) == 'b':
return 'bravo'
elif (ch) == 'c':
return 'charlie'
elif (ch) == 'd':
return 'delta'
elif (ch) == 'e':
return 'echo'
elif (ch) == 'f':
return 'foxtrot'
elif (ch) == 'g':
return 'golf'
elif (ch) == 'h':
return 'hotel'
elif (ch) == 'i':
return 'india'
elif (ch) == 'j':
return 'juliet'
elif (ch) == 'k':
return 'kilo'
elif (ch) == 'l':
return 'lima'
elif (ch) == 'm':
return 'mike'
elif (ch) == 'n':
return 'november'
elif (ch) == 'o':
return 'oscar'
elif (ch) == 'p':
return 'papa'
elif (ch) == 'q':
return 'quebec'
elif (ch) == 'r':
return 'romeo'
elif (ch) == 's':
return 'sierra'
elif (ch) == 't':
return 'tango'
elif (ch) == 'u':
return 'uniform'
elif (ch) == 'v':
return 'victor'
elif (ch) == 'w':
return 'whiskey'
elif (ch) == 'x':
return 'xray'
elif (ch) == 'y':
return 'yankee'
elif (ch) == 'z':
return 'zulu'
elif (ch) == '1':
return 'one'
elif (ch) == '2':
return 'two'
elif (ch) == '3':
return 'three'
elif (ch) == '4':
return 'four'
elif (ch) == '5':
return 'five'
elif (ch) == '6':
return 'six'
elif (ch) == '7':
return 'seven'
elif (ch) == '8':
return 'eight'
elif (ch) == '9':
return 'nine'
elif (ch) == '0':
return 'zero'
elif (ch) == ' ':
return 'space'
else:
return ch
def decode(strings):
# nested string comprehension, I love python :-)
return " ".join(["".join([_dec(atom) for atom in str.split()]) for str in strings])
def _dec(ch):
if (ch) == 'alpha':
return 'a'
elif (ch) == 'bravo':
return 'b'
elif (ch) == 'charlie':
return 'c'
elif (ch) == 'delta':
return 'd'
elif (ch) == 'echo':
return 'e'
elif (ch) == 'foxtrot':
return 'f'
elif (ch) == 'golf':
return 'g'
elif (ch) == 'hotel':
return 'h'
elif (ch) == 'india':
return 'i'
elif (ch) == 'juliet':
return 'j'
elif (ch) == 'kilo':
return 'k'
elif (ch) == 'lima':
return 'l'
elif (ch) == 'mike':
return 'm'
elif (ch) == 'november':
return 'n'
elif (ch) == 'oscar':
return 'o'
elif (ch) == 'papa':
return 'p'
elif (ch) == 'quebec':
return 'q'
elif (ch) == 'romeo':
return 'r'
elif (ch) == 'sierra':
return 's'
elif (ch) == 'tango':
return 't'
elif (ch) == 'uniform':
return 'u'
elif (ch) == 'victor':
return 'v'
elif (ch) == 'whiskey':
return 'w'
elif (ch) == 'xray':
return 'x'
elif (ch) == 'yankee':
return 'y'
elif (ch) == 'zulu':
return 'z'
elif (ch) == 'one':
return '1'
elif (ch) == 'two':
return '2'
elif (ch) == 'three':
return '3'
elif (ch) == 'four':
return '4'
elif (ch) == 'five':
return '5'
elif (ch) == 'six':
return '6'
elif (ch) == 'seven':
return '7'
elif (ch) == 'eight':
return '8'
elif (ch) == 'nine':
return '9'
elif (ch) == 'zero':
return '0'
elif (ch) == 'space':
return ' '
else:
return ch
if __name__ == "__main__":
if (len(sys.argv) == 1):
pass
elif (sys.argv[1] == "enc") or (sys.argv[1] == "encode"):
print encode(sys.argv[2:])
elif (sys.argv[1] == "dec") or (sys.argv[1] == "decode"):
print decode(sys.argv[2:])
elif (sys.argv[0] == "deimify.py"):
print decode(sys.argv[1:])
else:
print encode(sys.argv[1:])