Advertisement
Tyler_Elric

phonecode.py

Apr 4th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.19 KB | None | 0 0
  1. def encode_solution(msg,lookup):
  2.     def encode(msg,lookup):
  3.         def find_index(v,table):
  4.             for key,value in table.items():
  5.                 if v.lower() in value.lower():
  6.                     return key
  7.             return v
  8.         for v in msg:
  9.             yield find_index(v,lookup)
  10.     return "".join(encode(msg,lookup))
  11.  
  12. def create_engrish_filter(library=[]):
  13.     def engrish_filter(word):
  14.         vowels,conso='aeiou','bcdfghjklmnpqrstvwxyz'
  15.         def contains_vowels(word):
  16.             for vowel in vowels+'y':
  17.                 if vowel in word:
  18.                     return True
  19.             return False
  20.         def q_u(word):
  21.             l=word.find("q")
  22.             if l<0 or l+1>=len(word):return True
  23.             if word[l+1] != 'u':return False
  24.         def user_validate(word):
  25.             o=''#input("Does '{}' look like a word to you? y/n>".format(word))
  26.             return o=='y' or o=='Y'
  27.         def matches_pronunciation_rules(word):
  28.             #Source: http://stackoverflow.com/questions/13072774/
  29.             v_state=[0, 9, 8, 1, 1, 13, 8, 1,  9, 9, 13, 8, 13,  9]
  30.             c_state=[0, 2, 3, 4, 0,  6, 7, 4, 12, 5, 11, 7, 10, 12]
  31.             a_state=[0, 1, 0, 0, 0,  1, 0, 0,  1, 1,  1, 0,  1,  1]
  32.             state=1
  33.             for c in word:
  34.                 if c in vowels:
  35.                     state=v_state[state]
  36.                 elif c in conso:
  37.                     state=c_state[state]
  38.                 if state==0:return False
  39.             return a_state[state]!=0
  40.         if contains_vowels(word):
  41.             if not q_u(word):return False
  42.             if matches_pronunciation_rules(word):
  43.                 if word in library:
  44.                     return True
  45.                 elif user_validate(word):
  46.                     return True
  47.         return word in library
  48.     return engrish_filter
  49.  
  50. def chunk_decode(code,lookup,filter=None):
  51.     def get_chunks(msg,tbl):
  52.         while len(msg)>0:
  53.             i,e=0,0
  54.             while msg[i] in tbl:i+=1
  55.             while len(msg) > i+e and msg[i+e] not in tbl:e+=1
  56.             sep,txt,msg=msg[i:i+e],msg[:i],msg[i+e:]
  57.             yield txt,sep
  58.     def possible_chunks(chunks,tbl):
  59.         for chunk in chunks:
  60.             def branching(txt,tbl):
  61.                 v,t=txt[0],txt[1:]
  62.                 for p in tbl[v]:
  63.                     p=p.lower()
  64.                     if len(t)>0:
  65.                         for ap in branching(t,tbl):
  66.                             yield p + ap
  67.                     else:
  68.                         yield p
  69.             yield list(branching(chunk[0],tbl)),chunk[1]
  70.     def apply_filter(target,func):
  71.         valid={}
  72.         for words,sep in target:
  73.             good=[]
  74.             for word in words:
  75.                 if word not in valid:
  76.                     valid[word]=func(word)
  77.                 if valid[word]:
  78.                     good.append(word)
  79.             yield good,sep
  80.     chunks=get_chunks(code,lookup)
  81.     chunks=list(possible_chunks(chunks,lookup))
  82.     if filter is not None:
  83.         chunks=list(apply_filter(chunks,filter))
  84.     def build_whole(l):
  85.         a,b = l[0],l[1:] if len(l)>1 else None
  86.         chunks,sep = a
  87.         for chunk in chunks:
  88.             if b is not None:
  89.                 for bchunk in build_whole(b):
  90.                     yield chunk + sep + bchunk
  91.             else:
  92.                 yield chunk + sep
  93.     for val in build_whole(list(chunks)):
  94.         yield val
  95.  
  96. phone_table = {
  97.         "2":"ABC",
  98.         "3":"DEF",
  99.         "4":"GHI",
  100.         "5":"JKL",
  101.         "6":"MNO",
  102.         "7":"PQRS",
  103.         "8":"TUV",
  104.         "9":"WXYZ"
  105. }
  106.  
  107. if __name__=="__main__":
  108.     from sys import argv
  109.     msg = " ".join(argv[1:])
  110.     def load_words(*fs):
  111.         def sub(f):
  112.             with open(f) as fi:
  113.                 return list(map(lambda s:s.strip().lower(),fi.readlines()))
  114.             return []
  115.         r=[]
  116.         for f in fs:r.extend(sub(f))
  117.         return r
  118.     sol,words = encode_solution(msg,phone_table),load_words("words.txt","names.txt")
  119.     print(sol)
  120.     for p in chunk_decode(sol,phone_table,create_engrish_filter(words)):
  121.         print(p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement