Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def encode_solution(msg,lookup):
- def encode(msg,lookup):
- def find_index(v,table):
- for key,value in table.items():
- if v.lower() in value.lower():
- return key
- return v
- for v in msg:
- yield find_index(v,lookup)
- return "".join(encode(msg,lookup))
- def create_engrish_filter(library=[]):
- def engrish_filter(word):
- vowels,conso='aeiou','bcdfghjklmnpqrstvwxyz'
- def contains_vowels(word):
- for vowel in vowels+'y':
- if vowel in word:
- return True
- return False
- def q_u(word):
- l=word.find("q")
- if l<0 or l+1>=len(word):return True
- if word[l+1] != 'u':return False
- def user_validate(word):
- o=''#input("Does '{}' look like a word to you? y/n>".format(word))
- return o=='y' or o=='Y'
- def matches_pronunciation_rules(word):
- #Source: http://stackoverflow.com/questions/13072774/
- v_state=[0, 9, 8, 1, 1, 13, 8, 1, 9, 9, 13, 8, 13, 9]
- c_state=[0, 2, 3, 4, 0, 6, 7, 4, 12, 5, 11, 7, 10, 12]
- a_state=[0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1]
- state=1
- for c in word:
- if c in vowels:
- state=v_state[state]
- elif c in conso:
- state=c_state[state]
- if state==0:return False
- return a_state[state]!=0
- if contains_vowels(word):
- if not q_u(word):return False
- if matches_pronunciation_rules(word):
- if word in library:
- return True
- elif user_validate(word):
- return True
- return word in library
- return engrish_filter
- def chunk_decode(code,lookup,filter=None):
- def get_chunks(msg,tbl):
- while len(msg)>0:
- i,e=0,0
- while msg[i] in tbl:i+=1
- while len(msg) > i+e and msg[i+e] not in tbl:e+=1
- sep,txt,msg=msg[i:i+e],msg[:i],msg[i+e:]
- yield txt,sep
- def possible_chunks(chunks,tbl):
- for chunk in chunks:
- def branching(txt,tbl):
- v,t=txt[0],txt[1:]
- for p in tbl[v]:
- p=p.lower()
- if len(t)>0:
- for ap in branching(t,tbl):
- yield p + ap
- else:
- yield p
- yield list(branching(chunk[0],tbl)),chunk[1]
- def apply_filter(target,func):
- valid={}
- for words,sep in target:
- good=[]
- for word in words:
- if word not in valid:
- valid[word]=func(word)
- if valid[word]:
- good.append(word)
- yield good,sep
- chunks=get_chunks(code,lookup)
- chunks=list(possible_chunks(chunks,lookup))
- if filter is not None:
- chunks=list(apply_filter(chunks,filter))
- def build_whole(l):
- a,b = l[0],l[1:] if len(l)>1 else None
- chunks,sep = a
- for chunk in chunks:
- if b is not None:
- for bchunk in build_whole(b):
- yield chunk + sep + bchunk
- else:
- yield chunk + sep
- for val in build_whole(list(chunks)):
- yield val
- phone_table = {
- "2":"ABC",
- "3":"DEF",
- "4":"GHI",
- "5":"JKL",
- "6":"MNO",
- "7":"PQRS",
- "8":"TUV",
- "9":"WXYZ"
- }
- if __name__=="__main__":
- from sys import argv
- msg = " ".join(argv[1:])
- def load_words(*fs):
- def sub(f):
- with open(f) as fi:
- return list(map(lambda s:s.strip().lower(),fi.readlines()))
- return []
- r=[]
- for f in fs:r.extend(sub(f))
- return r
- sol,words = encode_solution(msg,phone_table),load_words("words.txt","names.txt")
- print(sol)
- for p in chunk_decode(sol,phone_table,create_engrish_filter(words)):
- print(p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement