Skip to content

Instantly share code, notes, and snippets.

@catb0t
Forked from BenTheHokie/wordmap.csv
Created May 30, 2019 02:35
Show Gist options
  • Save catb0t/7d9f357213a2c491f9bf9bc5910061d4 to your computer and use it in GitHub Desktop.
Save catb0t/7d9f357213a2c491f9bf9bc5910061d4 to your computer and use it in GitHub Desktop.
Phonetically correct word scrambler
I EIGH
J DGE
U OU
S ZE
T TTE
TH CHTH
R EUR
EW IEU
U OU
P PPE
S CZ
O EAUX
H WH
A UA
R RRE
D BD
I EIGH
T TTE
F PH
L LLE
EW OUGH
A AA
R RH
OU OUGH
N GNE
D DE
TH FTH
E EO
W WH
O HO
R WR
L LLEE
D ADE
A EIGH
N GN
D ED
B BT
A AA
CK CQUE
I EIGH
N DNE
T PHTH
O OUS
M MME
Y IGH
M MN
OU OUGH
TH FTH
#!/usr/bin/python3
import random
class Scrambler:
@staticmethod
def getDict(fn:str = "wordmap.csv"):
d = {}
with open(fn) as f:
for row in f:
s = row.split(',')
if len(s) >= 2:
key = s[0].upper()
if( d.get(key) ):
d[key].append(s[1].upper().strip())
else:
d[s[0].upper()] = [s[1].upper().strip()] # assign the index before the comma to the element after
return d
def __init__(self, wordmap = None):
self.wordmap = wordmap
if self.wordmap == None:
self.wordmap = self.getDict()
self.maxLen = 1;
for k in self.wordmap.keys(): # get the max length of the keys
if len(k) > self.maxLen:
self.maxLen = len(k)
def scramble(self, s:str):
sarr = s.split(' ')
newarr = []
for word in sarr:
newword = ""
currIdx = 0
while(currIdx < len(word)):
for i in range(self.maxLen, 0, -1):
translation = self.wordmap.get( word[currIdx:currIdx+i].upper() )
if( translation ): # if there is a valid translation
newword += random.choice( translation )
currIdx += i
break
elif( i == 1 ): # if there's no translation for a single letter, do not translate it
newword += word[currIdx].upper()
currIdx += i
break
newarr.append(newword)
return ' '.join(newarr)
def main():
sc = Scrambler()
while(1):
s = input("Input a phrase: ")
print(sc.scramble(s))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment